Python Programs on Iterative Statements

Pogram 1: Write a Python program to reverse the digits of a given number.

Logic:

  • Extracts the last digit using num % 10
  • Builds the reversed number by multiplying the current reversed number by 10 and adding the extracted digit
  • Removes the last digit from the original number using num // 10

Example: Take 4568

  • last_digit = x % 10 = 4568 % 10 = 8
    reversed_num = reversed_num * 10 + digit = 0 * 10 + 8 = 8
    x = x // 10 = 4568 // 10 = 456
  • last_digit = x % 10 = 456 % 10 = 6
    reversed_num * 10 + digit = 8 * 10 + 6 = 80 + 6 = 86
    x = x // 10 = 456 // 10 = 45
  • last_digit = x % 10 = 86 % 10 = 6
    reversed_num = reversed_num * 10 + digit = 86 * 10 + 5 = 860 + 5 = 865
    x = x // 10 = 45 // 10 = 4
  • last_digit = x % 10 = 4 % 10 = 4
    reversed_num = reversed_num * 10 + digit = 865 * 10 + 4 = 8650 + 4 = 8654
    x = x// 10 = 4 // 10 = 0

Program:



#Get input from user
number = int(input('Enter the number: '))
reversed_num = 0
x = abs(number)
while x > 0:
    last_digit = x % 10  # Get the last digit
    reversed_num = reversed_num * 10 + last_digit  # Append the digit
    x = x // 10  # Remove the last digit

if (number<0):
    reversed_num = -reversed_num
else:
    reversed_num
print('The reversed number is:', reversed_num)

Output:

Enter the number: 5784
The reversed number is: 4875

Enter the number: -5784
The reversed number is: -4875

Pogram 2: Write a Python program to find the factorial of a given number.

Logic:

  • Compare the first number (x1) with the other two (x2 and x3).
  • If x1 is greater than or equal to both x2 and x3, then x1 is the greatest.
  • If not, check if x2 is greater than or equal to both x1 and x3.
  • If neither x1 nor x2 is the greatest, then x3 must be the greatest.

Program:



n = int(input("Enter a number: "))
if n < 0:
    print("Factorial is not defined for negative numbers")
else:        
    result = 1
    for i in range(1, n + 1):
        result *= i
    print("Factorial of "+ str(n) + " is ", result)

Output:

Enter a number: 5
Factorial of 5 is 120

Enter a number: -5
Factorial is not defined for negative numbers

Pogram 3: Write a python program to display factors of a given integer number.

Factors of a number are integers that divide it exactly without leaving a remainder.

Logic:

  • Check all numbers from 1 to n (where n is the input number).
  • If n % i == 0, then i is a factor of n.

Program:



######## Without List ########## 
x = int(input("Enter a number: "))
n = abs(x) #to handle negative numbers
for i in range(1, n+1):
    if n%i==0:
        print(i)

####### With List  #############
x = int(input("Enter a number: "))
n = abs(x) #to handle negative numbers
factors = []  #Creating empty list
for i in range(1, n+1):
    if n%i==0:
        print(i)
        factors.append(i) #Adding every factor the list
print("The factors of the " + str(n)+" is", factors)

Output:

Enter a number: 20
1
2
4
5
10
20

Enter a number: 20
The factors of the 20 is [1,2,4,5,10,20]

Pogram 4: Write a python program to print Fibonacci numbers.

Logic:

  • The Fibonacci sequence starts with 0 and 1.
  • Each subsequent number is the sum of the previous two numbers
  • Sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

Program:



x = int(input("Enter a number: "))
n = abs(x)
a, b = 0, 1
for i in range(3, n + 1):
    c = a + b
    print(c, end=" ") #The end=" " parameter ensures a space is added after b instead of a newline
    a, b = b, c

Output:

Enter a number: 10
1 2 3 5 8 13 21 34

Pogram 5: Write a python program to display all prime numbers between 0 to n.

A prime number is a natural number greater than 1 that is divisible only by 1 and itself.

Logic:

  • For each number from 2 to n, check if it’s prime by testing divisibility from 2 to its square root.
  • Use loops and conditionals to iterate and verify primality
  • Print each prime number found

Program:



x = int(input("Enter a number: "))
n = abs(x)
print("Prime numbers between", 0, "and", n, "are:")
for i in range(2, n):
    for j in range(2, i):
        if i % j == 0:
            break
    else:          #The else block of a loop is executed only if the loop completes fully without encountering a break statement.
        print(i)

Output:

Enter a number: 11
Prime numbers between 0 and 11 are:
2
3
5
7