Skip to content

Instantly share code, notes, and snippets.

@SemanticallyNull
Created November 7, 2011 11:22
Show Gist options
  • Save SemanticallyNull/1344692 to your computer and use it in GitHub Desktop.
Save SemanticallyNull/1344692 to your computer and use it in GitHub Desktop.
More C... when will it end?
#include <stdio.h>
int main() {
int age;
int sum = 0;
float avrg;
int x;
printf("I'm going to ask 10 people for their age and then find the average\n");
printf("of their ages at the end, just for fun!\n\n");
for(x = 0; x < 10; x++){
printf("What is your age? ");
scanf("%d", &age); // Accept a numeric input and push it to age (by ref)
if(age < 5) {
printf("Wow, you're very young!\n");
} else if(age < 12) {
printf("Oh, older now!\n");
} else if(age < 20) {
printf("A teenager? Party everyday!\n");
} else {
printf("Sorry, you're an adult now. No more partying for you!\n");
}
printf("\n");
sum += age;
}
avrg = sum / 10;
printf("The average age was: %f\n", avrg);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment