Write a Program That Takes a Number N as Input And Prints The Nth Power of 2

Hi in this article you will learn about how to find the nth power of 2 using the Python program.  Python provides a double star operator **  to raise any value to the power of 2.

For example if you want to raise n to the power of 2  then you can simply use the below statement to execute in Python program




Mathematical  representation
==============================

2 ^ n  ====>>  let's take n as 5
2 ^ 5  ====>> 32

Python representation
============================
2 ** n =====>>  let's assume n value as 5
2 ** 5 =====>> 32

Let's try to solve this using a Python program with only two lines of code.

 

 

Write a Program That Takes a Number N as Input And Prints The Nth Power of 2

 

 


Write a Program That Takes a Number N as Input And Prints The Nth Power of 2

Take a integer variable n and ask a user to enter the value of 10 using the input function call and then convert it into an integer type using the type conversion

n = int(input("Enter a Integer Number : "))





Just use the print statement to print the nth power of 2 as shown below below, use the print formatting to raise 2 to power of n.

print(f"The {n}th Power of 2 i.e, 2 ^ {n} is = {2 ** n}")





Complete Python Program
=======================

n = int(input("Enter a Integer Number : "))

print(f"The {n}th Power of 2 i.e, 2 ^ {n} is = {2 ** n}")




Output 1
======================

Enter a Integer Number : 6
The 6th Power of 2 i.e, 2 ^ 6 is = 64




Output 2
=================

Enter a Integer Number : 0
The 0th Power of 2 i.e, 2 ^ 0 is = 1



Output 3
=============

Enter a Integer Number : 12
The 12th Power of 2 i.e, 2 ^ 12 is = 4096





Output 4
===================
Enter a Integer Number : 1024
The 1024th Power of 2 i.e, 2 ^ 1024 is = 179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216




Conclusion
===============================
Execute the above Python program to find the nth power of 2 by providing a random integer value as an input to the script.

Comment it down below if you have any queries related to the above Python program

Post a Comment

0 Comments