Answer to Question #154853 in C++ for zain ul abdeen

Question #154853

Write a program which takes two integers (num1 and num2) as input from the user. The program should display the result of num1 to the power num2. i) The program can use only for loops. ii) The program can use only while loops.


1
Expert's answer
2021-01-11T05:02:21-0500

1)

#include <iostream>
#include <cmath>
using namespace std;
int main () {
    int num1, num2;
    cout << "Enter number1 and number2: " << endl;
    cin >> num1 >> num2;
    int result = 1;
    for (int i = 0; i < num2; i++) {
        result *= num1;
    }
    cout << num1 << "^" << num2 << " = " << result;
}


2)

#include <iostream>
#include <cmath>
using namespace std;
int main () {
    int num1, num2;
    cout << "Enter number1 and number2: " << endl;
    cin >> num1 >> num2;
    int result = 1, temp = num2;
    while (num2--) {
        result *= num1;
    }
    cout << num1 << "^" << temp << " = " << result;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog