Respuesta :
Answer:
Here is the C program. Let me know if you want the program in some other programming language.
#include <stdio.h> //to use input output functions
int main() { //start of main function
int num; // to store input numbers
int sum = 0; //to store the sum of numbers
scanf("%d", &num); //reads the numbers from user
int count = 0; //to count the total numbers entered by user
int max = 0; //to store the maximum of numbers
while(num >= 0) { //continues to execute until user enters negative number
sum+=num; //adds the numbers
scanf("%d", &num); //reads the numbers from user
count++; //counts the numbers entered
if(num>=max){ //if number is greater than numbers stored in max
max = num; } } //assigns that maximum number to max
printf("%d %d", sum/count,max); } //displays the average of numbers and maximum of numbers
Explanation:
I will explain the program with an example. Lets say the first number input by user is 15. Then the loop works as follows:
At first iteration:
num >= 0 is true because 15 is greater than 0 so program moves to the body of loop
sum+=num; becomes:
sum = sum + num
sum = 0 + 15
sum = 15
count++; becomes:
count = count + 1
count = 0 + 1
count = 1
if(num>=max) means if(15>=0) this is true so
max = num; this becomes:
max = 15
Lets say the next value entered by user is 20. It is stored in num variable
At second iteration:
num >= 0 is true because 20 is greater than 0 so program moves to the body of loop
sum+=num; becomes:
sum = sum + num
sum = 15 + 20
sum = 35
count++; becomes:
count = count + 1
count = 1 + 1
count = 2
if(num>=max) means if(20>=15) this is true so
max = num; this becomes:
max = 20
Lets say the next value entered by user is 0. It is stored in num variable
At third iteration:
num >= 0 is true because 0 is equal to 0 so program moves to the body of loop
sum+=num; becomes:
sum = 35 + num
sum = 35 + 0
sum = 35
count++; becomes:
count = count + 1
count = 2 + 1
count = 3
if(num>=max) means if(0>=20) this is false so
max = num; this remains:
max = 20
Lets say the next value entered by user is 5. It is stored in num variable
At fourth iteration:
num >= 0 is true because 5 is greater than 0 so program moves to the body of loop
sum+=num; becomes:
sum = 35 + 5
sum = 35 + 5
sum = 40
count++; becomes:
count = count + 1
count = 3 + 1
count = 4
if(num>=max) means if(5>=20) this is false so
max = num; this remains:
max = 20
Lets say the next value entered by user is -1. It is stored in num variable
Now the loop breaks because num >= 0 is false because -1 is less than 0 so program moves to the statement:
printf("%d %d", sum/count,max);
This has two parts to print on output screen:
sum/count which is 40/4 = 10
max which is 20
So the output of the entire program is:
10 20
The screenshot of program along with its output is attached.
