C++ program Write a function that reads positive numbers from the user, and retu
ID: 641320 • Letter: C
Question
C++ program
Write a function that reads positive numbers from the user, and returns the largest number and outputs it. The function signature should be
int largest();
The function does not take any parameters. So, you can read all your input in the function. It returns an int, which is the largest number to main. Use a loop to read the numbers until the user enters -1 to quit. As you read, keep track of the largest number. When the user enters -1, stop reading and return the largest number to the main function. Do NOT use an array for this program.
Write a function that takes as a parameter an integer, and returns the number of odd, even and zero digits. This should be a void function since a function cannot return 3 values. Here is your function signature:
void numberCount(int number, int &oddCount, int &evenCount, int &zeroCount);
So, if the user enters 34873045, the number of odd digits is 4, even digits is 3, and
zeros is 1. You must use integers, do not use strings.
The main function must display a simple menu as follows:
Welcome to my program! Please choose one of the following:
(a) Find the largest of a list of positive numbers (-1 to quit).
(b) Given a positive number, display the number of even, odd and zero digits in
the number.
(c) quit this program.
Based on what the user picks, use a switch or an if statement and output the corresponding answer. This menu should be in a loop so it continues to display after each turn until the user enters
Explanation / Answer
#include <iostream>
using namespace std;
int largest(){
int max = -55555;
int val;
cout << "Enter positive numbers" << endl;
while(true){
cin >> val;
if(val == -1){
break;
}
if(val > max){
max = val;
}
}
return max;
}
void numberCount(int number, int &oddCount, int &evenCount, int &zeroCount){
oddCount = 0;
evenCount = 0;
zeroCount = 0;
int rem;
while(number != 0){
rem = number % 10;
if(rem == 0){
zeroCount++;
}
else{
if(rem % 2 == 0){
evenCount++;
}
else{
oddCount++;
}
}
number = number / 10;
}
}
int main(){
char ch;
int number, oddCount, evenCount, zeroCount;
while(true){
cout << "(a) Find the largest of a list of positive numbers (-1 to quit)." << endl;
cout << "(b) Given a positive number, display the number of even ";
cout << " , odd and zero digits in the number." << endl;
cout << "(c) quit this program" << endl;
cout << "Choose any option: ";
cin >> ch;
switch(ch){
case 'a':
case 'A':
cout << "Largest Value is " << largest() << endl;
break;
case 'b':
case 'B':
cout << "Enter any number: ";
cin >> number;
numberCount(number, oddCount, evenCount, zeroCount);
cout << "Odd count is " << oddCount << endl;
cout << "Even count is " << evenCount << endl;
cout << "Zero count is " << zeroCount << endl;
break;
case 'c':
case 'C':
return 0;
default:
cout << "Please choose a valid option" << endl;
}
cout << endl << endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.