Write a program that calculates and prints the average of several integers. Assume the last value read with scanf is the sentinel 9999. A typical input sequence might be 10 8 11 7 9 9999

Answer :

Answer:

Following are the program in C programming language

#include<stdio.h> // header file

int main() // main function  

{

int n1;// variable declaration

int c=0; // variable declaration

float sum=0; // variable declaration

float avg1;

printf("Enter Numbers :\n");

while(1) // iterating the loop

{

scanf("%d",&n1); // Read the value by user

if(n1==9999) // check condition  

break; // break the loop

else

sum=sum+n1; // calculating sum

c++; // increment the value of c

}

avg1=sum/c;

printf("Average= %f",avg1);

return 0;

}

Output:

Enter Numbers:

10  

8  

11

7  

9  

9999

Average= 9.000000

Explanation:

Following are the description of program

  • Declared a variable "n1" as int type
  • Read the value by the user in the "n1" variable by using scanf statement in the while loop .
  • The while loop is iterated infinite time until user not entered the 9999 number .
  • if user enter 9999 it break the loop otherwise it taking the input from the user .
  • We calculating the sum in the "sum " variable .
  • avg1 variable is used for calculating the average .
  • Finally we print the value of average .
fichoh

The program takes in several value numeric values until 9999 is inputed, the program calculates the average of these values. The program is written in python 3 thus :

n = 0

#initialize the value of n to 0

cnt = 0

#initialize number of counts to 0

sum = 0

#initialize sum of values to 0

while n < 9999 :

#set a condition which breaks the program when 9999 is inputed

n = eval(input('Enter a value : '))

#accepts user inputs

cnt += 1

#increases the number of counts

sum+=n

#takes the Cummulative sum

print(sum/cnt)

#display the average.

A sample run of the program is attached.

Learn more : https://brainly.com/question/14506469

${teks-lihat-gambar} fichoh

Other Questions