Thursday, June 5, 2008

Average of several integers

/*Write a prog that calculates and prints the avg of several integers.
Assume the last value read with scanf is sentinel 9999.A typical input
sequence might be :

10 8 11 7 9 9999
indicating that the avg of all values preceding 9999 is to be calculated
*/

#include

int main(void)
{
int i,count = 0,temp = 0 ;
char ans ;
int avg;
for ( i = 1 ; i <= 9999 ; )
{
printf("Enter number between 1 & 9999 : ");
scanf("%d",&i);
count = (count + 1) ;
temp = (temp + i);
printf("Do you want to enter another integer for avg count (y/n) : ");
scanf(" %c",&ans);
if ( ans == 'y')
continue;
else
break;
}
avg = (temp/count);
printf("Average is %d",avg);
}

No comments: