# Thread: Average using for loop

1. ## Average using for loop

[FONT="Comic Sans MS"][/FONT]

Hi..

Pls help me wid solving this program :

WAP using a for loop to read in a set of numbers and print out their average.
The program will start by prompting the user for the number of numbers to be read in and will then prompt for the individual numbers with a prompt such as

enter number 2 ..to indicate to the uset which data item is currently being entered.Do something special when prompting for the last number.
Note that there is no need to store all the individual numbers , it is sufficient to maintain a running total.

Thanks in advance for your help and time
funs

2. Please read "Forum Guidelines" and "Homework policy".

3. Originally Posted by funskoolz
[FONT="Comic Sans MS"][/FONT]
You lost me there.

4. Code:
```

#include<stdio.h>
#include<conio.h>
main()
{
int n,i,k,sum=0,ave=0;
printf("Number of numbers to be read: \n");
scanf("%d", &n);
for(i=0;i<n;i++)
{
if(i+1==n)
{
printf("you are about to enter the last number\n");
}
printf("enter number %d\n", i+1);
scanf("%d", &k);
sum=sum+k;
}
ave=sum/n;
printf("ave=%d",ave);

}```

5. u can modify ur code by declaring ave as float and also calculate ave as follows so that it can take float values.
Code:
```ave=1.0*sum/n;
printf("ave=%f",ave);```

6. 1.0 is double - 1.0f is float

Popular pages Recent additions