Saturday, 22 July 2017

Write a program that calculates & prints the average of several integers

Write a program that calculates & prints the average of several integers.

Assume the last value read is sentinel   9999. A typical input sequence  might be. 10 8 6 7 2  9999 indicating that the average of all the values preceding   9999 is to be calculated. (using while and do-while).

Code:

#include<stdio.h>
#include<conio.h>
void main()
{
 int n;
 int a=0,b=0;
 printf("Enter The Integer (9999 To End): ");
 scanf("%d",&n);
 while(n !=9999)
 {
 b +=n;
 ++a;
 printf("Enter Next Integer (9999 To End): ");
 scanf("%d",&n);
 }
 if(a !=0)
 {
 printf("\nThe Average Is: %2f\n",(double)b/a);
 }
 else
 {
 printf("\nNo Value Enterd.\n");
 }
 getch();
 clrscr();
}

Output:


Try it yourself.
If you have any problem you can tell me on comment box i will help you.
thanks.


Viral Programs

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation.

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Very Good Article ...
    Keep it up...

    ReplyDelete
    Replies
    1. Thanks to support me.
      i will try my best to improve the quality of content.

      Delete