;

Python Program to Find the Power of a Number


Tutorialsrack 22/04/2020 Python

In this Python Program, we will learn how to calculate the Nth power of a number without using math.pow() function or using math.pow() function. 

Here is the code of the program to calculate the Nth power of a number without using math.pow() function or using math.pow() function.

Program 1: Python Program to Find the Power of a Number Using For Loop

Python Program to Find the Power of a Number Using For Loop
# Python Program to Find the Power of a Number Using For Loop
 
# To Take Input From the User
number = int(input("Enter any Positive Number: "))
exponent = int(input("Enter Exponent Value: "))
power = 1

for i in range(1, exponent + 1):
    power = power * number
    
print("The Result of {0} Power {1} = {2}".format(number, exponent, power))
Output

Enter any Positive Number: 5

Enter Exponent Value: 3

The Result of 5 Power 3 = 125

Program 2: Python Program to Find the Power of a Number Using While Loop

Python Program to Find the Power of a Number Using While Loop
# Python Program to Find the Power of a Number Using While Loop

# To Take Input From the User
number = int(input("Enter any Positive Number : "))
exponent = int(input("Enter Exponent Value : "))

power = 1
i = 1

while(i <= exponent):
    power = power * number
    i = i + 1
    
print("The Result of {0} Power {1} = {2}".format(number, exponent, power))

Output

Enter any Positive Number : 5

Enter Exponent Value : 3

The Result of 5 Power 3 = 125.0

Program 3: Python Program to Find the Power of a Number Using pow() Method

Python Program to Find the Power of a Number Using pow() Method
# Python Program to Find the Power of a Number Using pow() Method

# Import Module
import math

# To Take Input From the User
number = int(input("Enter any Positive Number : "))
exponent = int(input("Enter Exponent Value : "))

power = math.pow(number, exponent)
    
print("The Result of {0} Power {1} = {2}".format(number, exponent, power))

 

Output

Enter any Positive Number : 5

Enter Exponent Value : 3

The Result of 5 Power 3 = 125.0


Related Posts



Comments

Recent Posts
Tags