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.
#include <iostream>
using namespace std;
int main()
{
int num1,num2, answer=1;/*variables are declared and answer is initialized with a value of 1*/
cout << "Enter first number==> " << endl;
cin >> num1;/*user input is requested and stored as num1*/
cout << "Enter second number==> " << endl;
cin >> num2;/*user input is requested and stored as num2*/
for(int i=1;i<=num2;i++)/*loop runs from 1 to num2, the user declared power*/
{
answer=answer*num1;/*value of answer is calculated by multiplying num1 by itself until loop ends*/
}
cout << num1<<" raised to "<<num2<<" is==> "<<answer;/*prints out answer*/
return 0;
}
Comments
Leave a comment