Question #31117

write a C++ program which will raise any number x to a positive power n using a for loop?

Expert's answer

#include <iostream>
#include <conio.h>

using namespace std;
//main function
int main()
{
int x,n,raise=1;//declare variable
cout<<"Enter x: ";
cin>>x;//read x
cout<<"Enter positive power n: ";
cin>>n;//read n
//raise any number x to a positive power n using a for loop
for(int i=0;i<n;i++){
raise*=x;
}
//show result
cout<<x<<"^"<<n<<" =
"<<raise;
getch();
return 0;
}

LATEST TUTORIALS
APPROVED BY CLIENTS