Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions 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
Check Mark
Knowledge Booster
Background pattern image
Computer Science
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
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education