Python While Loop Assignment with Solutions

Python while loop assignment with solutions. Learn more about Python While Loop in detail.

Find out the output of the following program segments

Q1.

 a = 110 
     while a > 100: 
      print(a) 
      a -= 2

Q2.

x=10
y =0
while x>y :
 	x= x-4
y +=4
print(x,end=” “)

Q3.

i=0
while i <10 :
	i = i+1
if I ==5: 
	Break
print(I, end=’  ‘)

Q4.

i = 0; sum = 0 
while i < 9: 
if i % 4 == 0:
     sum = sum + i
      i = i + 2
                print (sum)

Q5.

i =19
while  true
	if i %3==0:
		break
		print(‘a’)

Q6.

	
var = 7 
while var > 0: 
  print ('Current variable value: ', var) 
  var = var -1 
  if var == 3:
         break 
  else: 
       if var == 6: var = var -1 
              continue 
print ("Good bye!")

Q7. Write a program to print the table of a given number. The number has to be entered by the user.
solution

Q8. Write a program to generate the sequence: –5, 10, –15, 20, –25….. up to n, where n is an integer input by the user.

Q9. Write a program to find the sum of 1+ 1/8 + 1/27……1/n3, where n is the number input by the user.

Q10. Write a python program to find out reverse of any digit ( Example n = 125 than output is : 521 )
solution

n = int(input('Enter any number :'))
rev=0
while n!=0:
      rem = n % 10
      rev = rev*10+rem
      n = n //10

print('Reverse Number :',rev)

Q11. Write a function that checks whether an input number is a palindrome or not.

solution

n = int(input('Enter any number :'))
m = n
rev = 0
while n!=0:
      rem = n %10
      rev = rev*10+rem
      n = n//10

if(n==rev):
  print(' A palindrome number ')
else:
  print('not a palindrome number')

Q12. Program to find the factors of a whole number using while loop
solution

n = int(input('Enter any number n :'))
x=0
while x

Q13. Find the sum of all the positive numbers entered by the user. As soon as the user enters a negative number, stop taking in any further input from the user, and display the sum.

Q14. Program to check if the input number is prime or not.
solution

n= int(input('Enter any number :'))
x=1
found =0
while x

Q15. Program to find prime numbers between 100 to 500 using nested while loop only.

Solution

n = 99
while n<500:
      n= n+1
      x = 1
      found =0
      while x

Q16. Write a program to calculate the factorial of a given number.
soution

n = int(input('Enter value of n :'))
x = 0
s = 1
while x < n:
      x = x+1
      s = s*x
print('Factorial is  :', s)

Q17. Write a program to print the following pattern

*
* *
* * *
* * * * 
* * * * *
* * * * * *
* * * * * * *

Solution

row =0
while row<7 :
      row = row+1
      col=0
      while col < row :
        col = col+1
        print('*',end=" ")
      print()

Q18.Print this pattern on the screen

1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

solution

row =8
while row>1 :
      row = row-1
      col=0
      while col < row:
        col = col+1
        print(col,end=" ")
      print()

Q19. Print the following pattern on the screen using while loop only

              1
            1 2
          1 2 3
        1 2 3 4
      1 2 3 4 5
    1 2 3 4 5 6
  1 2 3 4 5 6 7

Solution

row =0
while row<7 :
      row = row+1
      sp=0
      while sp < 7-row:
        sp = sp+1
        print(" ",end=" ")
      
      col=0
      while col < row:
        col = col+1
        print(col,end=" ")
        
      print()

Q20. Print the following pattern on the screen using while loop only.

1
2  3
4  5  6
7  8  9  10
11 12 13 14 15
16 17 18 19 20 21

solution

row =0
no =0
while row<6:
     row = row+1
     col=0
     while col < row:
       col = col+1
       no = no+1
       print(no,end=" ")
     print()
    

Q21. Print following pattern on the screen using while loop only.

               1
            2  3
         4  5  6
     7  8  9  10
   11 12 13 14 15
16 17 18 19 20 21 

solution

row =0
no =0
while row<6:
     row = row+1
     sp=0
     while sp <6-row:
         sp = sp+1
         print(" ",end=" ")
     col=0
     while col < row:
       col = col+1
       no = no+1
       print(no,end=" ")
     print()

Q21. Print the following pattern using while loop only.

               1
            1  2  1  
         1  2  3  2  1
      1  2  3  4  3  2  1
    1 2  3  4  5  4  3  2  1 

solution

row=0
while row<5:
   row = row+1
   sp =0
   while sp<5-row:
     sp = sp+1
     print(" ",end="")
   left=0
   while left1:
     right = right-1
     print(right,end="")
   print()

Q22. Print the following pattern on the screen

1
1 0
1 0 1
1 0 1 0
1 0 1 0 1
1 0 1 0 1 0
1 0 1 0 1 0 1

solutions

row = 0
while row < 7:
      row = row+1
      col = 0
      while col < row:
        col = col+1
        if(col%2==0):
          print('0', end=" ")
        else:
          print('1', end=" ")
      print()

Q19. Write a program to display the sum of odd numbers and even numbers that fall between 12 and 37 (including both numbers)

Q20. Write a program to display all the numbers which are divisible by 11 but not by 2 between 100 and 500.

Q21. Write a program that keeps on accepting numbers from the user until the user enters Zero. Display the sum and average of all the numbers.

Q22. convert the following while loop into for loop. Assignment on Python For loop

x=15
while(x==15):
  print("hello")
  x=x-3

Q23. Write a Python program to accept a number from the keyboard. Find out the sum of its digits and display the result on the screen.

n = int(input('Enter any number :'))
sum1=0
while n!=0:
      rem = n % 10
      sum1 = sum1+rem
      n = n //10

print('Sum of digits :',sum1)

Q24. Create a simple menu-driven calculator that add, subtract, multiply and divide two numbers.

'''
  menu drivan program
  made  by    : rakesh
  '''
while True:
    print('\n MAIN MENU')
    print('\n1. ADD')
    print('\n2. SUB')
    print('\n3. MULTIPLY')
    print('\n4. DIVIDE')
    print('\n5. EXIT')
    choice= int(input('Enter your choice'))
    if choice==1:
       a= int(input('Enter A :'))
       b= int(input('Enter B :'))
       c = a+b
       print(c)
       wait=input('Press any key to continue.....')
    if choice == 2:
       a = int(input('Enter A :'))
       b = int(input('Enter B :'))
       c = a-b
       print(c)
    if choice == 3:
       a = int(input('Enter A :'))
       b = int(input('Enter B :'))
       c = a*b
       print(c)

    if choice == 4:
       a = int(input('Enter A :'))
       b = int(input('Enter B :'))
       c = a//b
       print(c)
    if choice==5:
       break
    if choice>5 or choice<1:
       print('Not a proper choice...Try again')

Q25. Write a python program to read a number from the keyboard. Check whether this number is perfect or not. ( Sum of factor of that number regenerate the number itself)
Example 6 = 1+2+3 => 6

n = int(input('Enter any number n :'))
x = 0
s=0
while x < n-1:
  x = x+1
  rem = n % x
  if(rem == 0):
    s =s+x

if(s==n):
  print('perfect numner')
else:
  print('not an perfect number')
If you like CBSEToaday and would like to contribute, you can also write an article using submit article or mail your article to contribute@cbsetoday.com See your article appearing on the cbsetoday.com main page and help other students/teachers.