33.9k views
0 votes
Write a function that asks the user to enter integer odd values and prints out on the screen their count, sum, max, min, and average. The user keep entering odd values till he enters the first even value.

User Bluegene
by
5.1k points

1 Answer

0 votes

Answer:

#include<iostream>

using namespace std;

//main function

int main(){

//initialization

int counts_Values=0, sum_all_Values=0;

int max_Value,min_Value;

float averge_Value;

int num1;

//display

cout<<"Enter the odd values; ";

cin>>num1; //store the number

max_Value=num1;

min_Value=num1;

//while loop for run continuously until odd numbers

while(num1%2 != 0){

counts_Values++; //increment count

sum_all_Values = sum_all_Values + num1; //calculate sum

//calculate the maximum value

if(num1>max_Value){

max_Value=num1;

}

//calculate the minimum value

if(num1<min_Value){

min_Value=num1;

}

cout<<"Enter the odd values; ";

cin>>num1;

}

//starting value is even, then store average to zero

if(counts_Values == 0){

averge_Value=0;

}else{

averge_Value = float(sum_all_Values)/counts_Values;

}

//display the result

cout<<"The count is:"<<counts_Values<<endl;

cout<<"The sum is:"<<sum_all_Values<<endl;

cout<<"The Max value is:"<<max_Value<<endl;

cout<<"The Min value is:"<<min_Value<<endl;

cout<<"The Average value is:"<<averge_Value<<endl;

return 0;

}

Step-by-step explanation:

Include the libraries iostream for using the input/output instruction.

Create the main function and declare the variables.

Print the message on the screen using cout instruction and then store the input enter by the user in the variable using the cin instruction.

then, check the number is even using the if-else statement.

if the number is even, then store zero value stored in the max and min value.

otherwise, store that number in min and max value.

take the while loop with a condition, num1%2 != 0 this condition check the number is not even. if the condition true execute the loop other terminate the loop.

After that, increment the count by 1 and take the sum of value.

then, check for maximum if a new number is greater than the number stored in the max_Value then update the max_Value.

same for min value but the condition will change. Check for the minimum if the new number is less than the number store in the min_Value then update the min_Value.

then, again ask the value from the user.

The above process continues again and again until the user does not enter the even value. if the user enters the even value, the loop terminates and then program checks the count value is zero. if zero then the average is zero other calculate the average.

the above check is used to remove the infinite value. because if we divided the sum with zero it becomes infinite.

and finally, display the output on the screen.

User Johnwow
by
5.6k points