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)
#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;
}
Comments
Leave a comment