Last active
April 13, 2024 11:59
-
-
Save ninjadynamics/8eaf5a51c00126ad3d7654fdf5e38b70 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> // Include standard input/output header | |
int main() { | |
char name[50]; // Declare an array of chars to store the name | |
int age; // Declare an integer to store the age | |
printf("Enter your name: "); // Prompt the user to enter their name | |
scanf("%49[^\n]", name); // Read a string from the user input including spaces | |
getchar(); // Consume the newline character to avoid issues with subsequent inputs | |
printf("Enter your age: "); // Prompt the user to enter their age | |
scanf("%d", &age); // Read an integer from the user input | |
printf("Hello, %s!\n", name); // Print greeting message with the name | |
// Use if statement to give feedback about the age | |
if (age < 18) { | |
printf("You are a minor.\n"); | |
} else if (age >= 18 && age < 65) { | |
printf("You are an adult.\n"); | |
} else { | |
printf("You are a senior.\n"); | |
} | |
// Example of a switch case based on age group | |
switch (age / 10) { | |
case 0: | |
case 1: | |
printf("You are a child or a teenager.\n"); | |
break; | |
case 2: | |
case 3: | |
case 4: | |
printf("You are in your youth or middle-aged.\n"); | |
break; | |
default: | |
printf("You are in your golden years.\n"); | |
break; | |
} | |
// Example of a for loop that prints numbers from 1 to age | |
printf("Counting from 1 to your age:\n"); | |
for (int i = 1; i <= age; i++) { | |
printf("%d ", i); | |
if (i % 10 == 0) // Print a newline every 10 numbers | |
printf("\n"); | |
} | |
printf("\n"); | |
return 0; // Return 0 to indicate the program finished successfully | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment