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; }
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
{
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;
}
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 4 images