Challenge 2: Remember a Friend's Name! 🚀
Welcome to this challenge! Work with Robo 🤖 to solve this problem step-by-step using algorithms and flowcharts.
🎬 The Story
Robo meets a new user. Instead of saying a generic hello, help Robo ask for their name, remember it, and say a personalized "Hello [Name]!".
📝 The Algorithm
- Start (Begin the program)
- Ask the user's name (Output prompt)
- Read the name and save it in a box called
name(Input) - Display "Hello " + name (Combined Output)
- Stop (End the program)
:::note What is a Variable?
A Variable is like a labeled storage box in the computer's memory. In this challenge, we created a box labeled name to store whatever name the user types in.
:::
📊 The Flowchart
Here, the parallelogram is used twice: once to read the input name, and once to output the combined message.
💻 From Flowchart to Code
Here is how to read input and combine it with a greeting in Python, Java, and C:
- Python
- Java
- C
# Ask and read name
name = input("What's your name? ")
# Greet by name
print("Hello " + name + "!")
import java.util.Scanner;
public class GreetByName {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("What's your name? ");
// Read name input
String name = sc.nextLine();
// Greet by name
System.out.println("Hello " + name + "!");
}
}
#include <stdio.h>
int main() {
char name[50];
printf("What's your name? ");
// Read name input (up to 49 chars, no spaces)
scanf("%49s", name);
// Greet by name
printf("Hello %s!\n", name);
return 0;
}