From this code, I wonder what algorithm in this programing?
#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=""> cout <> while (1) // infinite loop unless we exit from the program { cout < "\n1.="" calculate="" n!="" (n=""> cout < "\n2.="" calculate="" n="" to="" the="" m=""> cout < "\n3.="" exit=""> int choice; cout < "\nplease="" enter="" your="" selection:="" ";="" enter=""> 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=""> 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=""> 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=""> } } return 0;}
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=""> cout <> while (1) // infinite loop unless we exit from the program { cout < "\n1.="" calculate="" n!="" (n=""> cout < "\n2.="" calculate="" n="" to="" the="" m=""> cout < "\n3.="" exit=""> int choice; cout < "\nplease="" enter="" your="" selection:="" ";="" enter=""> 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=""> 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=""> 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=""> } } return 0;}
Already registered? Login
Not Account? Sign up
Enter your email address to reset your password
Back to Login? Click here