Sunday 7 January 2018

Average of every third integer between 1 to 100

Q : Write and explain the action of WHILE statement. Develop a program in ‘C’ language to compute the average of every third integer number lying between 1 and 100. Include appropriate documentation.

Solution : 


Read While loop


Program :


#include<stdio.h>
#include<conio.h>
void main()
{
     int a,b,c;

     clrscr();
     for(a=1,b=0,c=0;a<=100;a+=3)
     {
          c+=a;
          b++;
     }
     printf("\n\tAverage of every third integer between 1 to 100 is %d",c/b);
     getch();
}

Documentation :

  • Variable a is used for count 1 to 100
  • variable c is sum of every third integer . get by a because a is incremented by 3 in each loop turn.
  • Variable b is used to trace how many third number are in range of 1 to 100.
  • As we know formula of calculating average is sum of value divided by number of element is perform while printing

1 comment:

  1. Why for loop is used instead of while ?
    I think it should be an example of while statement.

    ReplyDelete