C Programming Examples – Averaging Using Loop Statement
Problem: Create a program that will accept ages of 10 people and display its average. Use any kind of iteration statement but do not use Arrays.
Solution:
First lets see an example output.
Enter age1: 5 Enter age2: 10 Enter age3:28 Enter age4: 7 Enter age5: 5 Enter age6: 18 Enter age7: 14 Enter age8: 16 Enter age9: 19 Enter age10: 22 The average age is: 14.4
Now we must consider which iteration statement we are going to use.
Because we have a fix number of times to repeat this process, the best loop statement to use is the For Loop Statement.
Now let’s take a look at the program flow using a flow chart.
As you see on our flow chart we have, we have declared the the variables age, sum, and average as float and ctr as integer.
Sum and ctr is defaulted to 0; this is to ensure that we’ll start from zero.
Ctr is the variable that we are going to use in counting the rotation/ repetition of the process. For this problem we have to repeat the process for 10 times.
int ctr; float ave, age, sum; //We used float for the ave because its value can contain decimal places. //initiate the value of sum sum = 0;
Now we must consider which iteration statement we are going to use.
Because we have a fix number of times to repeat this process, the best loop statement to use is the For Loop Statement.
#include<stdio.h> /* This source code is brought to you by ITechSociety.com */ main() { int ctr; float ave, age, sum; //We used float for the ave because its value can contain decimal places. //initiate the value of sum sum = 0; //start the loop for(ctr = 1; ctr <= 10; ctr++) { printf("Enter age %d: ", ctr); scanf("%f", &age); sum = sum + age; } //Calculate the average ave = sum / 10; printf("The average age is: %.2f", ave); getch(); return 0; }
Related posts:
If you have any questions please feel free to discuss it on our discussion board
www.itechsociety.com/forum/topic/c-programming-activity-averaging-using-loop
This entry was posted in C/C#/C++, Examples, Programming, Tutorials and tagged iteration, loop, looping. Bookmark the permalink.




This really helpful.
Thanks a lot!
I’m glad that I’m able to help.
Hope you enjoy!
Sometimes it is very useful and interesting to add a video to post, that makes your imagination process much more clear.
Yes Thanks a lot Lisa. That is actually what I’m planning to do.
salamat d2 keith… malaking tulong talaga toh.. d ba tau mahuli ni maam d2???ahahaha
Hi Dags! Im really glad that I can help you in my own way. There is nothing to be afraid of. This not illegal and everything is just for educational purposes.
thanks…. hm… something wrong with the code…. its not the same dun sa binigay mong unanng output nya,,,
ung may..
Enter age1: 5
Enter age2: 10
Enter age3:28
…
kapag ni run mo sya…
ung pinapakita nya ay:
Enter age: 5
Enter age: 10
Enter age: 28
Enter age: 7
…
How to write a small program in C that will request the user to input 5 integer numbers and compute the average of the numbers entered ??
Hello Avi!
just change this line
for(ctr = 1; ctr <= 10; ctr++)
to
for(ctr = 1; ctr <= 5; ctr++)
and this line
Ave = sum/10;
to
ave = sum/5;
I hope it helps.
i like this