Question

Write a C++ program to run a menu-driven program with the following choices: Count number of...

Write a C++ program to run a menu-driven program with the following choices:

  1. Count number of even digits in a number
  2. Compute the factorial of a number
  3. Quit

Make sure your program conforms to the following requirements:
1. This program should be called playingWithNumbers.cpp

2. Write a function called getValidUserInputPosNumGT0 that allows a user to enter an integer and validated that the number is > 0. It must have a pass by reference parameter that will store the value selected by the user.

3. Write a function called numEvenDigits that takes a number as a value parameter and returns the count of how many digits in the number are even.

4. Write a function called factorial that takes a number as a value parameter and returns the factorial of the number.

n! = n * (n-1) * (n-2) * ...1

5. Add comments wherever necessary.

Sample Runs:

NOTE: not all possible runs are shown below.

Sample Run

Welcome to the playing with numbers!
1) Count the even digits in a number
2) Compute the factorial of a number
3) Quit
Select an option (1..4)..9
Select an option (1..4)..-9
Select an option (1..4)..1
Enter in a positive number greater than 0...12
numEvenDigits(12)= 1
1) Count the even digits in a number
2) Compute the factorial of a number
3) Quit
Select an option (1..4)..1
Enter in a positive number greater than 0...22
numEvenDigits(22)= 2
1) Count the even digits in a number
2) Compute the factorial of a number
3) Quit
Select an option (1..4)..1
Enter in a positive number greater than 0...1345
numEvenDigits(1345)= 1
1) Count the even digits in a number
2) Compute the factorial of a number
3) Quit
Select an option (1..4)..1
Enter in a positive number greater than 0...2461
numEvenDigits(2461)= 3
1) Count the even digits in a number
2) Compute the factorial of a number
3) Quit
Select an option (1..4)..2
Enter in a positive number greater than 0...1
Factorial(1) = 1
1) Count the even digits in a number
2) Compute the factorial of a number
3) Quit
Select an option (1..4)..2
Enter in a positive number greater than 0...2
Factorial(2) = 2
1) Count the even digits in a number
2) Compute the factorial of a number
3) Quit
Select an option (1..4)..2
Enter in a positive number greater than 0...3
Factorial(3) = 6
1) Count the even digits in a number
2) Compute the factorial of a number
3) Quit
Select an option (1..4)..2
Enter in a positive number greater than 0...4
Factorial(4) = 24
1) Count the even digits in a number
2) Compute the factorial of a number
3) Quit
Select an option (1..4)..2
Enter in a positive number greater than 0...5
Factorial(5) = 120
1) Count the even digits in a number
2) Compute the factorial of a number
3) Quit
Select an option (1..4)..3

Process finished with exit code 0

0 0
Add a comment Improve this question Transcribed image text
Answer #1


ANSWER:-

#include <iostream>

using namespace std;

void getValidUserInputPosNumGT0(int *a){
    int num;
    // prompt user to enter number
    cout<<"Enter in a positive number greater than 0...";
    // store it by reference
    cin>>*a;
}
int numEvenDigits(int num){
    int noOfEvenDigits = 0;
    while(num > 0){
        // It gives remainder, i.e last of the number
        // if it is even then increase noOfEvenDigits by 1
        int digit = num%10;
        if(digit%2==0){
            noOfEvenDigits++;
        }
        num = num/10;
    }
    return noOfEvenDigits;
}
int factorial(int num){
    int fact = 1;
    while(num>1){
        fact *= num;
        num--;
    }
    return fact;
}
int main()
{   int ch,num;
    cout<<"Welcome to the playing with numbers!"<<endl;
    do{
        cout<<"1) Count the even digits in a number"<<endl;
        cout<<"2) Compute the factorial of a number"<<endl;
        cout<<"3) Quit"<<endl;
        while(true){
            cout<<"Select an option(1..4)..";
            cin>>ch;
            if(ch==1 || ch==2 || ch==3){
                break;
            }
        }
        switch(ch){
            case 1:
                getValidUserInputPosNumGT0(&num);
                cout<<"numEvenDigits("<<num<<")="<<numEvenDigits(num)<<endl;
                break;
            case 2:
                getValidUserInputPosNumGT0(&num);
                cout<<"Factorial("<<num<<")="<<factorial(num)<<endl;
                break;
        }
    }while(ch!=3);
    return 0;
}


OUTPUT:-

NOTE:- If you have any doubts, please comment below. Please give positive rating. THANK YOU!!!

Add a comment
Know the answer?
Add Answer to:
Write a C++ program to run a menu-driven program with the following choices: Count number of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Objectives: Integer arithmetic, Functions, menus. Write a C++ program that displays the following menu of choices....

    Objectives: Integer arithmetic, Functions, menus. Write a C++ program that displays the following menu of choices. 1. Find the number of digits in an integer. 2. Find the nth digit in an integer. 3. Find the sum of all digits of an integer. 4. Is the integer a palindrome? 5. Quit Enter a choice: Page 1 of 4 For each of the choices (1, 3, 4), Read a positive integer number and call a function that processes the menu choice...

  • Q#1: Write a C++ program to enter a number, compute and print the following: - Sum...

    Q#1: Write a C++ program to enter a number, compute and print the following: - Sum of even digits in the number - Count of even digits in the number - Count of digits below 5 For example, if the input is 3429, then Sum of even digits in the number = 6 Count of even digits in the number = 2 Count of digits below 5 = 3

  • ‘C’ programming language question Write a MENU DRIVEN program to A) Count the number of vowels...

    ‘C’ programming language question Write a MENU DRIVEN program to A) Count the number of vowels in the string B) Count the number of consonants in the string C) Convert the string to uppercase D) Convert the string to lowercase E) Display the current string X) Exit the program The program should start with a user prompt to enter a string, and let them type it in. Then the menu would be displayed. User may enter option in small case...

  • Part I. Functional Paradigm 30 pts. 1. (10)Write the definition of procedure "count-even-digits" ...

    using dr racket Part I. Functional Paradigm 30 pts. 1. (10)Write the definition of procedure "count-even-digits" where the parameter is any positive number (0 12345 .} the result is a positive number (0 123 4 Evaluates into the number of even digits in a given integer (count-even-digits 2) result is 1; there is only one even digit in number2 (count-even-digits 4) result is 2; there is two even digit in number 4 (count-even-digits 7) result is 3; there is only...

  • c++ please (1) Write a program that prompts the user to enter an integer value N...

    c++ please (1) Write a program that prompts the user to enter an integer value N which will rpresent the parameter to be sent to a function. Use the format below. Then write a function named CubicRoot The function will receive the parameter N and return'its cubic value. In the main program print the message: (30 Points) Enter an integer N: [. ..1 The cubic root of I.. ] is 2 update the code om part and write another function...

  • How to write this code in C++???? using fstream Write a program which: Asks the user...

    How to write this code in C++???? using fstream Write a program which: Asks the user to enter a positive integer greater than 0 Validates that the entry is a positive integer Outputs the digits in reverse order with a space separating the digits Outputs the even digits not in reverse order with a space separating the digits (consider zero to be even) If there are no even digits, the an appropriate message should be displayed: There are no even...

  • Write a C++ program that prompts the user with the following menu options: Erase-ArrayContent Count-Words Quit...

    Write a C++ program that prompts the user with the following menu options: Erase-ArrayContent Count-Words Quit 1. For Erase-ArrayContent option, write complete code for the C++ function Erase described below. The prototype for Erase function is as follow: void Erase( int a[ ], int * N, int * Search-Element) The function Erase should remove all occurrences of Search-Element from the array al). Note that array a[ ] is loaded with integer numbers entered by the user through the keyboard. N...

  • 1. Write a program that takes a number as input and check whether the number is...

    1. Write a program that takes a number as input and check whether the number is positive, negative or zero. 2. Write a C++ program that prompts the user to enter a number and checks whether the entered number is even or odd. 3.  Using switch-case statement write a C++ program that prompts the user to enter 1, 2 or 3 and display "Red" if selection is 1, "Yellow" if selection is 2, or "Green" if selection is 3. 4. Write...

  • C program--write a program to find whether the given number of three digits is an integer...

    C program--write a program to find whether the given number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is since 3**3 + 7**3 + 1**3 = 371. Output: Enter a number, or * to quit : 432 4**3 + 3**3 + 2**3 is not = 432 Enter a number, or * to quit : 371 3**3 + 7**3 + 1**3 is = 371...

  • Please solve the program in C++(Recursive) Write a function called factorialIterative(). This function should take a...

    Please solve the program in C++(Recursive) Write a function called factorialIterative(). This function should take a single BIG integer (bigint) as its parameter n, and it will compute the factorial n! of the value and return it as its result (as a bigint). Write this functions using a loop (do not use recursion). 2. Write the factorial function again, but using recursion. Call this function factorialRecursive(). The function takes the same parameters and returns the same result as described in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT