C Programming Examples – Averaging Using Loop Statement


Difficulty: Beginner

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.

VN:F [1.9.11_1134]
Rating: 5.0/5 (2 votes cast)

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.

Figure 1.0 - Averaging 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;
}

C Programming Examples – Averaging Using Loop Statement, 5.0 out of 5 based on 2 ratings

Related posts:

  1. C Programming Examples – Simple Averaging
  2. C/C#/C++ Statements

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 , , . Bookmark the permalink.
This article is written by: chicoi08
I love making great things using my computer. From programming to designing to blogging. I enjoy doing them. I hope this website can help some people out there. View all posts by chicoi08 →

Discussion 10 Comments

  1. Dallas says:

    This really helpful.
    Thanks a lot!

    VA:F [1.9.11_1134]
    Rating: 0 (from 0 votes)
  2. Lisa Evancho says:

    Sometimes it is very useful and interesting to add a video to post, that makes your imagination process much more clear.

    VA:F [1.9.11_1134]
    Rating: 0 (from 0 votes)
  3. dagz says:

    salamat d2 keith… malaking tulong talaga toh.. d ba tau mahuli ni maam d2???ahahaha

    VA:F [1.9.11_1134]
    Rating: 0 (from 0 votes)
    • chicoi says:

      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.

      VN:F [1.9.11_1134]
      Rating: 0 (from 0 votes)
  4. Jusil says:

    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

    VA:F [1.9.11_1134]
    Rating: 0 (from 0 votes)
  5. avi says:

    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 ??

    VA:F [1.9.11_1134]
    Rating: 0 (from 0 votes)
    • chicoi08 says:

      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.

      VN:F [1.9.11_1134]
      Rating: 0 (from 0 votes)
  6. razen_24 says:

    i like this

    VA:F [1.9.11_1134]
    Rating: 0 (from 0 votes)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">