From this code, I wonder how each code process? #include using namespace std;  int factorial(int n) // function to calculate factorial {         int i, ans = 1;         for(i = n; i > 0; i--)                 ans *= i;         return ans; } long int power(int n, int m) // function to calculate power {         int i;         long int ans = 1;         for(i = m; i > 0; i--)                 ans *= n;         return ans; } int main()  {         cout << "MATH MENU"; // display math menu         cout << "\n----------------------------------------";         while (1) // infinite loop unless we exit from the program         {                 cout << "\n1. Calculate n! (n factorial).";                 cout << "\n2. Calculate n to the m power.";                 cout << "\n3. Exit Program.\n";                 int choice;                 cout << "\nPlease enter your selection: "; // enter selection                 cin >> choice;                 if (choice == 1) // for factorial                 {                         float nf;                         cout << "\nEnter an integer value for n (1-9): ";                         cin >> nf;                         while (nf != (int)nf || nf < 1 || nf > 9) // exceptional cases handled                         { // check for integers and check for numbers in between 1-9                                 cout << "\nInvalid option. Please re-enter.\n";                                 cout << "\nEnter an integer value for n (1-9): ";                                 cin >> nf;                         }                         cout << "\n" << (int)nf << "! = " << factorial((int)nf) << endl; // display factorial                 }                 else if (choice == 2)                 {                         float nf, mf;                         cout << "\nEnter an integer value for n (1-9): ";                         cin >> nf;                         cout << "\nEnter an integer value for m (1-9): ";                         cin >> mf;                         while (nf != (int)nf || nf < 1 || nf > 9 || mf != (int)mf || mf < 1 || mf > 9)                         { // check as for integers and range in 1-9                                 cout << "\nInvalid option. Please re-enter.\n";                                 cout << "\nEnter an integer value for n (1-9): ";                                 cin >> nf;                                 cout << "\nEnter an integer value for m (1-9): ";                                 cin >> mf;                         }                         cout << "\n" << (int)nf << "^" << (int)mf << " = " << power((int)nf, (int)mf) << endl;                 }                 else if (choice == 3)                 {                         break; // exit the program                 }                 else // retry the loop if invalid option is entered                 {                         cout << "\nInvalid option. Please re-enter.\n";                 }         }         return 0;  }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

From this code, I wonder how each code process?

#include <iostream>
using namespace std; 

int factorial(int n) // function to calculate factorial
{
        int i, ans = 1;
        for(i = n; i > 0; i--)
                ans *= i;
        return ans;
}

long int power(int n, int m) // function to calculate power
{
        int i;
        long int ans = 1;
        for(i = m; i > 0; i--)
                ans *= n;
        return ans;
}

int main() 
{
        cout << "MATH MENU"; // display math menu
        cout << "\n----------------------------------------";
        while (1) // infinite loop unless we exit from the program
        {
                cout << "\n1. Calculate n! (n factorial).";
                cout << "\n2. Calculate n to the m power.";
                cout << "\n3. Exit Program.\n";
                int choice;
                cout << "\nPlease enter your selection: "; // enter selection
                cin >> choice;
                if (choice == 1) // for factorial
                {
                        float nf;
                        cout << "\nEnter an integer value for n (1-9): ";
                        cin >> nf;
                        while (nf != (int)nf || nf < 1 || nf > 9) // exceptional cases handled
                        { // check for integers and check for numbers in between 1-9
                                cout << "\nInvalid option. Please re-enter.\n";
                                cout << "\nEnter an integer value for n (1-9): ";
                                cin >> nf;
                        }
                        cout << "\n" << (int)nf << "! = " << factorial((int)nf) << endl; // display factorial
                }
                else if (choice == 2)
                {
                        float nf, mf;
                        cout << "\nEnter an integer value for n (1-9): ";
                        cin >> nf;
                        cout << "\nEnter an integer value for m (1-9): ";
                        cin >> mf;
                        while (nf != (int)nf || nf < 1 || nf > 9 || mf != (int)mf || mf < 1 || mf > 9)
                        { // check as for integers and range in 1-9
                                cout << "\nInvalid option. Please re-enter.\n";
                                cout << "\nEnter an integer value for n (1-9): ";
                                cin >> nf;
                                cout << "\nEnter an integer value for m (1-9): ";
                                cin >> mf;
                        }
                        cout << "\n" << (int)nf << "^" << (int)mf << " = " << power((int)nf, (int)mf) << endl;
                }
                else if (choice == 3)
                {
                        break; // exit the program
                }
                else // retry the loop if invalid option is entered
                {
                        cout << "\nInvalid option. Please re-enter.\n";
                }
        }
        return 0; 
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 4 images

Blurred answer
Knowledge Booster
Returning value from Function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education