Monday, 10 October 2011

10-10-2011 for loop (average)



program calculates average of 100 random values entered by user and if -1 is entered then program terminates at once and shows the average of values, entered until then.
(Uses for loop).
#include <stdio.h>
#include <conio.h>
int main()
{
    int val = 0, count = 0; 
    float sum = 0, avg = 0;
    for ( count = 0 ; count <= 100 ; count++)
    {
        if(val != (-1))
        {
       
        printf("Please enter the %d digit: ",count +1);
        scanf("%d",&val);
                 if(val != (-1))
                 {
                         sum = sum + val;
                 }
                 else
                 break;
       }
       else
       break;
      
    }
    printf("\nSum of entered digits is: %f\n",sum);
    printf("\nTotal number of digits entered are: %d\n",count);
    avg = sum/count;
    printf("\nThe average of entered digits is: %f\n",avg);
    getche();
    return 0;
}

No comments:

Post a Comment