Iterative Statements(ADDITIONAL QUESTIONS)

M3-R5.1 · Chapter 4: Operators, Expressions and Python Statements · 7 min read

Q1: Write a Python program to print Hello Python 10 times.

i=1

while i<=10:

    print("Hello Python")

    i+=1

Q2: Write a Python program to print numbers from 1 to 10 horizontal.

i=1

while i<=10:

    print(i, end=' ')

    i+=1

Output: 1 2 3 4 5 6 7 8 9 10

Q3: Write a Python program to print sum of the first 15 natural numbers using a while loop.

num = 16

sum = 0

while(num > 0):

    sum += num

    num -= 1

print("The sum is", sum)

Q4: Write a Python program to print Square of the first 5 natural numbers using a while loop.

i = 1

while(i <= 5):

   print(i*i)

   i += 1

Q5: Write a Python program to print first 15 odd numbers using a while loop.

i = 1

while(i < 15):

    print(i,end=" ")

    i = i+2

Q6: Write a Python program to print first 15 even numbers using a while loop.

i = 2 

while i <=15: 

    print(i,end=" ") 

    i += 2

Q7: Write a Python program to print those numbers which is divisible by either 5 or 7 between 1 to 50 using a while loop.

i=1

while i<51:

    if i%5 == 0 or i%7==0 :

        print(i, end=' ')

    i+=1

Output  : 5 7 10 14 15 20 21 25 28 30 35 40 42 45 49 50

Q8: Write a Python program to get the number from keyboard and print the sum of numbers until user enters 0 using while Loop.

num = int(input('Enter a number: '))

sum = 0

while num != 0:

    sum += num

    num = int(input('Enter a number: '))

print('The sum is', sum)

Q9: Write a Python program to print first 15 odd and even numbers using a while loop.

n = 1

while (n <=15):

    if n % 2 == 0:

        print (n, "is even.")

    else:

        print (n, "is odd." )

    n += 1

Q10: Write a Python program to print sum of first 15 odd and even numbers using a while loop.

 n = 1

sumEv=0

sumodd=0

while (n <=15):

    if n % 2 == 0:

        sumEv=sumEv+n

    else:

        sumodd=sumodd+n

    n += 1

print("The sum of First 15 Odd Number is =",sumodd)

print("The sum of First 15 Even Number is =",sumEv)

Q11: Write a program to print factorial of a number using while loop

num = int(input("plz enter the number"))

fac = 1

i=1

while(i<=num):

    fac = fac*i

    i=i+1

print("The factorial of",num,"is",fac)

Q12: Write a program to Reverse a number using while loop

num = int(input("Enter the integer number: "))

revn = 0

while (num > 0):

   digit = num % 10

   revn = (revn * 10) + digit

   num = num // 10

print("The reverse number is ",revn)

                      OR

fact=1

num=int(input("enter the number"))

while(num>1):

      fact=fact*num

      num=num-1

print("the factorial is ",fact)

Q13: Write a program to check a number is palindrome or not using while loop

num = int(input("Enter a number: ")) 

num1 = num 

revnum = 0 

while num > 0: 

    dig = num % 10 

    revnum = revnum * 10 + dig 

    num = num // 10 

if num1== revnum:

    print(num1," is a Palindrome number.")

else:

    print(num1," is NOT a Palindrome number.")

Q14: Python program to check if the number is an Armstrong number or not

num = int(input("Enter a number: "))

sum = 0

temp = num

while temp > 0:

   digit = temp % 10

   sum += digit ** 3

   temp //= 10

if num == sum:

   print(num,"is an Armstrong number")

else:

   print(num,"is not an Armstrong  number")

Q15: Write a Python program to Printing numbers in descending order:

n = int(input("Enter a number: "))

while n > 0:

    print(n)

    n -= 1

Q16: Write a Python program to Printing the first 7 multiples of 7

count = 1

num = 7

while count <=7:

    print(num * count)

    count += 1

Q17: Write a Python program to play a game to guessing simple number between 1 To 10

se_num = 7

guess = None

while guess != se_num:

    guess = int(input("Guess the secret number (between 1 and 10): "))

    if guess < se_num:

        print("Too low, try again.")

    elif guess > se_num:

        print("Too high, try again.")

    else:

        print("Congratulations! You guessed the right number.")

print("Game over.")

Q18: Write a Python program to Converting numbers from decimal to binary:

num = int (input("Enter a number: "))

bin = 0

power = 1

while num > 0:

    rem = num % 2

    bin += rem * power

    power *= 10

    num //= 2

print("Binary value:", bin)

Q19: Write a Python program for Finding the average of 5 numbers entered by keyboard:

p = 5

total = 0

count = 0

while p > 0:

    count += 1

    num = int(input("Enter a number: "))

    total += num

    p -= 1

average = total / count

print("Average of given numbers:", average)

Q20: Printing Fibonacci series using while loop:

n = int(input("Enter the number of terms: "))

a, b = 0, 1

c = 0

while c< n:

    print(a, end=" ")

    a, b = b, a + b

    c += 1

Q21: Write a Python program for Finding the sum of digits of a number:

num = int(input("Enter a number: "))

sum_digits = 0

while num > 0:

    sum_digits += num % 10

    num //= 10

print("Sum of digits:", sum_digits)