You're online now.

Hurray! you are online now.

# Program to calculate the power of given numbers by user in c and cpp

In this program, we will take two inputs from the users, one will be the base and another one is exponent.

For Example : 3
3 is the base number.
2 is the exponent.
power is equal to 3 * 3.

## Write a c program to calculate the power of given number without using the pow() function

``````#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;
}``````

## Write a c++ program to caculate the power of given number without using the pow() function

``````#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;
}``````

## Algorithm to calculate power given number

1. START
3. SET POW = 1
4. START for loop, when condition (i ≤ exp) is true then go to STEP 5.
5. Calculate pow = pow * base.
6. Every iteration increment i by 1.
7. PRINT pow
8. EXIT

## Working of calculate of power.

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.

## Calculate power with pow() function in c programming

``````#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;
}``````

## Calculate power with pow() function in c++ programming

``````#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;
}``````
🖤 0