In this program, we will take two inputs from the users, one will be the base and another one is exponent.
For Example : 32
3 is the base number.
2 is the exponent.
power is equal to 3 * 3.
#include<stdio.h>
int main(){
int base, exp, pow = 1;
printf("Enter the base number : ");
scanf("%d", &base);
printf("Enter the exponent : ");
scanf("%d", &exp);
for(int i = 1; i <= exp; i++){
pow = pow * base;
}
printf("%d ^ %d is : %d", base, exp, pow);
return 0;
}
#include<iostream>
using namespace std;
int main(){
int base, exp, pow = 1;
cout<<"Enter the base number : ";
cin>>base;
cout<<"Enter the exponent : ";
cin>>exp;
for(int i = 1; i<=exp; i++){
pow = pow * base;
}
cout<<base<<"^"<<exp<<" = "<<pow;
return 0;
}
Initial i = 1, i ≤ 3 condition true then.
pow = 1 * 2 = 2.
pow = 2.
After this increment iteration i by 1.
Now i = 2
pow = 2 * 2 = 4
After this increment iteration i by 1.
Now i = 3.
pow 4 * 2 = 8
After this increment iteration i by 1.
Now i = 4.
Loop is terminated because the condition is false.
#include<stdio.h>
#include<math.h>
int main(){
int base, exp;
printf("Enter the base number : ");
scanf("%d", &base);
printf("Enter the exponent : ");
scanf("%d", &exp);
int power = pow(base, exp);
printf("%d", power);
return 0;
}
#include<iostream>
#include<math.h>
using namespace std;
int main(){
int base, exp;
cout<<"Enter the base number : ";
cin>>base;
cout<<"Enter the exponent : ";
cin>>exp;
int power = pow(base, exp);
cout<<power;
return 0;
}
Comments
Oops!