*
Here in this post, we have explained how to raise any number x to a Positive Power n. So let us get started.
*

##
**
Problem Statement
**

We need to write a Script in C, C++, and Python, that accepts two integer values from the user
```
x
```

and
```
n
```

. And return an output value of
```
x
```

raise to the power
```
n
```

. Here x would be the number whose power to be raised, n will be the number representing the value of power. This article will help you to Write
a Program
to raise any number x to a Positive Power n For example, if
```
```

is 2 and
**
x
**
```
```

is 4 the output should be (2)
**
n
**
^{
4
}
=2*2*2*2 = 16

###
**
Approach 1
**

In our first approach, we will use the Inbuilt
```
pow()
```

function provided almost every programming language. Using the
```
pow()
```

function we can compute any power of the specified number.

###
**
C Program to raise any number x to a Positive Power n (using pow function)
**

```
#include <stdio.h>
#include <math.h>
int main()
{
int x, n, result;
printf("Enter the value of x: ");
scanf("%d", &x);
printf("Enter the value of n: ");
scanf("%d", &n);
// compute the power of x raise to n
result = pow(x,n);
printf("The %d raise to the power %d is: %d ", x, n, result);
return 0;
}
```

**
Output
**

```
Enter the value of x: 4
Enter the value of n: 5
The 4 raise to the power 5 is: 1024
```

###
**
C++ Program to raise any number x to a Positive Power n using pow function
**

```
#include<iostream.h>
#include< conio.h>
#include<math.h>
void main()
{
clrscr();
int x, n, result;
cout<<"Enter the value of x: ";
cin>>x;
cout<<"Enter the value of n: ";
cin>>n;
// compute the power of x raise to n
result = pow(x,n);
cout<<"The "<<x<<" raise to the power "<<n<<" is: "<<result;
getch();
}
```

**
Output:
**

```
Enter the value of x: 4
Enter the value of n: 6
The 4 raise to the power 6 is: 4096
```

###
**
Python Program to raise any number x to a Positive Power n using pow function
**

```
import math
x= int(input("Enter the value of x: "))
n= int(input("Enter the value of n: "))
# compute the power of x raise to n
result = math.pow(x, n)
print("The",x,"raise to the power",n,"is:",int(result))
```

**
Output:
**

```
Enter the value of x: 4
Enter the value of n: 6
The 4 raise to the power 6 is: 4096
```

###
**
Approach 2
**

In the second approach, rather than using the inbuilt
```
pow()
```

function, we will use the for loop to calculate the power of a number raised to a specified
positive integer value
. The logic for this approach is very simple, all we need to do is keep multiple the number
```
x
```

to itself up to
```
n
```

times. So we will create a loop from range
```
1 to n
```

, and inside that, we will multiple
```
x
```

to itself and store the result in a
```
result
```

variable.

###
**
C Program to raise any number x to a Positive Power n using for loop)
**

```
#include <stdio.h>
int main()
{
//initialize the result value with 1
int x, n, result=1, i;
printf("Enter the value of x: ");
scanf("%d", &x);
printf("Enter the value of n: ");
scanf("%d",&n);
// compute the power of x raise to n
for(i=1;i<=n; i++)
{
result *=x;
}
printf("The %d raise to the power %d is: %d ", x, n, result);
return 0;
}
```

**
Output
**

```
Enter the value of x: 5
Enter the value of n: 3
The 5 raise to the power 3 is: 125
```

###
**
C++ Program to raise any number x to a Positive Power n using for loop
**

```
#include<iostream.h>
using namespace std;
int main()
{
//initialize the result value with 1
int x, n, result=1, i;
cout<<"Enter the value of x: ";
cin>>x;
cout<<"Enter the value of n: ";
cin>>n;
// compute the power of x raise to n
for(i=1;i<=n; i++)
{
result *=x;
}
cout<<"The "<<x<<" raise to the power "<<n<<" is: "<<result;
return 0;
}
```

**
Output
**

```
Enter the value of x: 5
Enter the value of n: 7
The 5 raise to the power 7 is: 78125
```

###
**
Python
**
**
Program to raise any number x to a Positive Power n
**
**
(using for loop)
**

```
x= int(input("Enter the value of x: "))
n= int(input("Enter the value of n: "))
# initialize result with value 1
result =1
# compute the power of x raise to n
for i in range(n):
result *= x
print("The",x,"raise to the power",n,"is:",int(result))
```

**
Output
**

```
Enter the value of x: 3
Enter the value of n: 5
The 3 raise to the power 5 is: 243
```

##
**
Wrapping Up!
**

In this Programming tutorial, we discussed how to write a script or program to raise any number x to a Positive Power n. To solve this problem we have used two approaches, in the first approach, we take the help of inbuilt function pow(), and in the second approach, we used the
```
for
```

loop. When using the for loop make sure that initialize the value of
```
result
```

with 1, because
many programming languages
store a default value of 0 or garbage value during declaration.

**
People are also reading:
**

- WAP to display a message on screen
- Python Examples
- WAP to Calculate Simple Interest
- Python RegEx
- WAP to Display Fibonacci Series
- How many Centimeters in a Foot through Python?
- WAP to check whether the entered number is prime or not
- C Program to Design Love Calculator
- WAP to find the divisors of a positive Integer
- Rectangle & Pyramids Pattern in C++

## Leave a Comment on this Post