Monday, June 12, 2023

#PYTHON(DAY46)Example Programs

                                     #PYTHON(DAY46)

Example Programs

1) Python Program to Reverse a Number

num = 1234
reversed_num = 0

while num != 0:
digit = num % 10
reversed_num = reversed_num * 10 + digit
num //= 10

print("Reversed Number: " + str(reversed_num))

And the output is:

4321

2) Python Program to Compute the Power of a Number

base = 3
exponent = 4

result = 1

while exponent != 0:
result *= base
exponent-=1

print("Answer = " + str(result))

And the output is:

Answer = 81

3) Python Program to Count the Number of Digits Present In a Number

num = 3452
count = 0

while num != 0:
num //= 10
count += 1

print("Number of digits: " + str(count))

And the output is:

Number of digits: 4

No comments:

Post a Comment

Building Static Website(part6) HTML Lists

  Building Static Website (part6) HTML Lists Today, let us add some lists to our detailed view section by using html lists. Lists: List is a...