Sunday 17 September 2017

Average of every third number

Q : Write a ‘C’ program to compute the average of every third integer lying between 1 and 200? Include  appropriate documentation?

Solution :

#include<stdio.h>

void main()
{
     int i,sum,avg;
     for(i=1,sum=0;i<=200;i=i+3)

          sum=sum+i;
     avg=sum/i;
     printf("\n\tAverage of every third ineger till 200 is %d",avg);   

}

For calculate average of each third number till 200. We have to skip two numbers from current value.so variable I start with 1 and skip by 2 for sake of take each third number.

Sum is total of each third numbers from 1 to 200.

When loop stop. Variable I will have number count.

For calculate average, divide total from number count. Which is achieved through avg=sum/i

No comments:

Post a Comment