C Programming Tutorial - Calculating the Average Age


Quick and easy to assign a value for multiple variables.

#include<stdio.h>

int main() {
  int a;
  int b;
  int c;
   a = b = c = 100; {computer reads it from right to left} {take the value of 100 and assign it to c and take the value of c and assign it to b and take the value of b and assign it to a}

 printf("%d %d %d",a,b,c);
}


When you print this out, the output will be

100 100 100

Lets make a quick program that calculate the average age of three people.

#include<stdio.h>

int main() {
   float age1, age2, age3, average;
   age1 = age2 = 4.0; {These two person have the same age}
 
Lets get the age of user

   printf("Enter your age\n"); 
   scanf("%f", &age3); {Remember whenever you use a variable which is not an array we have to use ampersand}
   
   average = (age1 + age2 + age3) / 3;
   printf("\n The average age of the group is %f", average);
}


Comments