Write a program (Using C not C ++ ) to calculate the average of an arbitrary num
ID: 3787591 • Letter: W
Question
Write a program (Using C not C ++ ) to calculate the average of an arbitrary number of positive numbers that the user will enter. The program should stop collecting values from the user and calculate the average once a negative value is entered by the user. The negative value should not be included as part of the calculation.
Average Calculator Algorithm (using while loop) Start input number IS average number 20 total I count total output total total number count average count count 1 End input numberExplanation / Answer
// C program to find average
#include <stdio.h>
#include <stdlib.h>
int main(void) //main function begin
{
//varibles declaration
int n=0,i;
int digit,sum=0;
float avg;
//taking input values untill negative number enters
printf("Enter inputs ");
while(1) //loop repeats always with true conditon
{
scanf("%d",&digit);
if(digit<0) //if user input is negative then loop stops taking input
{
break;
}
n=n+1;
sum=sum+digit; //sum of inputs
}
avg=sum/n; //avg calculation
printf("Average of input values : %f ",avg); //display average
}
Output :
Enter inputs
1
2
3
4
5
-4
Average of input values : 3.000000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.