Question

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

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

  1. Compute the factorial of a number
  2. Compute the alternating factorial of a number
  3. Quit
#include <iostream>
using namespace std;
void getValidUserInputPosNumGT0 (int *a) {

    int num;
    cout << "Enter in a positive number greater than 0... ";
    cin >> *a;
}
long double factorial (int num) {
    int fact = 1;
    while (num > 1) {
        fact *= num;
        num--;
    }
    return fact;
}
long double AlternatingFactorial (int num) {
    int fact = 1;
    while (num < 1) {
        fact *=num;
        num++;
    }
}
int main (){
    int ch;
    int num;
    cout << "Welcome to the playing with numbers program!\n";
    do {
        cout << "1)Compute the factorial of a number\n";
        cout << "2) Compute the alternating factorial of a number\n";
        cout << "3)Quit\n";
        while (true) {
            cout << "Select an option (1..3)..";
            cin >> ch;
            if (ch == 1 || ch == 2 || ch == 3) {
                break;
            }
        }
        switch (ch) {
            case 1:
                getValidUserInputPosNumGT0(&num);
                cout << "Factorial("<<num<<")="<< factorial(num) << endl;
            case 2:
                long double AlternatingFactorial (int num);
                cout << "AlternatingFactorial" << num << AlternatingFactorial(num)<< endl;
        }
    }       while (ch!=3);
    return 0;


}

I've tried to run this and option one works, but the alternating factorial (option 2) won't run.

Homework Answers

Answer #1

#include <iostream>
using namespace std;
void getValidUserInputPosNumGT0 (int *a) {

int num;
cout << "Enter in a positive number greater than 0... ";
cin >> *a;
}
long double factorial (int num) {
int fact = 1;
while (num > 1) {
fact *= num;
num--;
}
return fact;
}
long double AlternatingFactorial (int num) {
int res=0;
for(int m=1;m<=num;m++){
if((num-m)%2==0){
res=res+factorial(m);
}
else{
res=res-factorial(m);
}
}
  
return res;
}
int main (){
int ch;
int num;
cout << "Welcome to the playing with numbers program!\n";
do {
cout << "1)Compute the factorial of a number\n";
cout << "2) Compute the alternating factorial of a number\n";
cout << "3)Quit\n";
while (true) {
cout << "Select an option (1..3)..";
cin >> ch;
if (ch == 1 || ch == 2 || ch == 3) {
break;
}
}
switch (ch) {
case 1:
getValidUserInputPosNumGT0(&num);
cout << "Factorial("<<num<<")="<< factorial(num) << endl;
break;
case 2:
getValidUserInputPosNumGT0(&num);
cout << "AlternatingFactorial(" << num <<")="<< AlternatingFactorial(num)<< endl;
}
} while (ch!=3);
return 0;


}

Note:

The return value is not provided in the function AlternatingFactorial(). So it is modified. And the errors in the switch case statements are removed.

Screenshots:

The screenshots are attached below for reference.

Please follow them for output.

Know the answer?
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
Similar Questions
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath>...
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath> using namespace std; void divisors(int num); int main () {    char repeat;    int num;       while (repeat !='n')    {        cout << "Enter a number: ";        cin >> num;        divisors(num);        cout << "Continue? (y or n): ";        cin >> repeat;    }    return 0; } void divisors(int num) {   ...
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment...
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment 2 (karatsuba.cpp) in Canvas (please do not rename file or use cout/cin statements in your solution). As a reminder, the algorithm uses recursion to produce the results, so make sure you implement it as a recursive function. Please develop your code in small The test program (karatsuba_test.cpp) is also given. PLEASE DO NOT MODIFY THE TEST FILE. KARATSUBA.CPP /* Karatsuba multiplication */ #include <iostream>...
1) Create a flowchart for the program below and Trace the program below with input of...
1) Create a flowchart for the program below and Trace the program below with input of 3 and then again with input of 5. #include <iostream> using namespace std; int Fall(int x, int m) { int Xm = 1, i=x; while(i>=x-m+1) { Xm = Xm*i; i=i-1; } return Xm; } int Delta(int x, int m) { int ans = 0; ans = Fall(x+1,m); ans = ans - Fall(x,m); return ans; } void main() { int x = 0, m =...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std;...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std; int fib(int n) { int temp; if (n <= 0)    return 0; else if (n <= 2)    return 1; else {    temp = fib(n – 1);    return temp + fib(n-2); } } int main() {    int num;    cout << "Which fibonacci number? ";    cin >> num;    cout << fib(num) << endl;    return 0; } You...
How to stop the program from exiting after display detail. When there is food detail, it...
How to stop the program from exiting after display detail. When there is food detail, it will display and exit the program. What can i do to make it not exit the program and back to main menu. #include <iostream> #include <iomanip> #include<string.h> using namespace std; struct food{ int order_id; string food_code,flavor,customer_id; string address,name; int weight,unit_price,qty,contact_number; struct food *next; };    class Foodsystem{ food *head,*temp,*temp2,*end; static int id;    public: Foodsystem(){ head=NULL;end=NULL;} void Place_Order(); void View_food_details(); void Modify_food_details(); void Delete_food_details();...
Functions displayGrades and addGrades must be rewritten so that the only parameters they take in are...
Functions displayGrades and addGrades must be rewritten so that the only parameters they take in are pointers or constant pointers. Directions: Using the following parallel array and array of vectors: // may be declared outside the main function const int NUM_STUDENTS = 3; // may only be declared within the main function string students[NUM_STUDENTS] = {"Tom","Jane","Jo"}; vector <int> grades[NUM_STUDENTS] {{78,98,88,99,77},{62,99,94,85,93}, {73,82,88,85,78}}; Be sure to compile using g++ -std=c++11 helpWithGradesPtr.cpp Write a C++ program to run a menu-driven program with the...
C++ program that Create a struct called car that has the following data members (variables): -...
C++ program that Create a struct called car that has the following data members (variables): - Color //color of the car - Model //model name of the car - Year //year the car was made - isElectric //whether the car is electric (true) or not (false) - topSpeed //top speed of the car, can be a decimal. code i have done struct not working properly. #include <iostream> using namespace std; struct Car { string color; string model_number; int year_model; bool...
Take the following program and translate it into PEP/9 assembly language: #include using namespace std; int...
Take the following program and translate it into PEP/9 assembly language: #include using namespace std; int fib(int n) { int temp; if (n <= 0)    return 0; else if (n <= 2)    return 1; else {    temp = fib(n – 1);    return temp + fib(n-2); } } int main() {    int num;    cout << "Which fibonacci number? ";    cin >> num;    cout << fib(num) << endl;    return 0; } You must...
Modify the sum_thread.cpp program to compute sum = 1 + 1/2 + 1/3 + 1/4 +...
Modify the sum_thread.cpp program to compute sum = 1 + 1/2 + 1/3 + 1/4 + … 1/n. Let’s estimate natural using n = 20. sum_thread.cpp #include <chrono> #include <iostream> #include <mutex> #include <random> #include <utility> #include <vector> #include <thread> using namespace std; constexpr long long size= 1000000; mutex myMutex; void sumUp(unsigned long long& sum, const vector<int>& val, unsigned long long beg, unsigned long long end){ long long localSum = 0; for (auto it= beg; it < end; ++it){ localSum+=...
Modify the sum_thread.cpp program to compute sum = 1 + 1/2 + 1/3 + 1/4 +...
Modify the sum_thread.cpp program to compute sum = 1 + 1/2 + 1/3 + 1/4 + … 1/n. Let’s estimate natural using n = 20. sum_thread.cpp #include <chrono> #include <iostream> #include <mutex> #include <random> #include <utility> #include <vector> #include <thread> using namespace std; constexpr long long size= 1000000; mutex myMutex; void sumUp(unsigned long long& sum, const vector<int>& val, unsigned long long beg, unsigned long long end){ long long localSum = 0; for (auto it= beg; it < end; ++it){ localSum+=...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT