QB Python Loops
QB Python Loops
10. Write a program to display first n natural numbers, where n is to be input from the user.
11. display even natural numbers from 2 to n, where n is to be input from the user. For example,
if the value of n is 13, the output should be 2 4 6 8 10 12.
12. Write a program to find the sum of first n natural numbers, where n is to be input from the
user.
13. To find the sum of first n even natural numbers, where n is to be input from the user.
14. To input an integer and find its factorial. Factorial of an integer n (n>=1), is defined as the
product of all natural numbers from 1 to n, and is represented by n!. For example, factorial
of 5 is represented by 5! and is equal to 120 (=1x2x3x4x5).
Factorial of 0 is defined as 1 and factorial of negative integers is not defined.
15. script to input 10 numbers and then display the largest of the numbers entered.
16. Write a program to input 10 numbers and then display their sum and average. Also display
the largest and the smallest of the numbers entered.
17. input a number and display its first 10 multiples.
18. Write a program to input two numbers m and n. Then display first m multiples of n.
19. Write a program to input a number. If the number is negative, then input the number again.
Keep on doing so until the user enters a positive number or zero.
20. script to input 10 numbers and then display the smallest number entered. If the smallest
number is an integer, then find the sum of its digits, otherwise display the message “smallest
number is not an integer”.
21. Write a program to input two numbers and find their LCM and HCF.
22. Write a program to input a number and check whether it is a prime or not.
23. Write a program to display all the prime numbers between m and n, where m and n have to
be input from the user.
24. Write a program to input a list of n number and count how many of the entered numbers were
prime.
25. input an integer and find the sum of its digits.
26. input an integer and find the product of its odd digits. If the number does not contain any odd
digit, then the program should display an appropriate message instead of the product.
27. input 10 integers and find the sum of the digits of each integer.
28. Armstrong number is an integer which is equal to the sum of the cubes of its digits. For
example 153 is an Armstrong number because 153 = 13+53+33.
Write script to input an integer and check whether it is an Armstrong number or not.
29. Write a program to input a number and check whether it is palindrome or not.
30. Write a program to input a number and check whether it is a perfect number or not. (A number
is a perfect number if it is equal to the sum of its own factors, except itself. For example, 6 is
a perfect number as 6=1+2+3, and 8 is not a perfect number as 8 != 1+2+4 31. Write script
to display first n terms of Fibonacci series. The Fibonacci series is: 0 1 1 2 3 5 8 13 . . .
First and Second terms of the Fibonacci series are 0 and 1 resepectively, and after that any
term is the sum of its previous two terms.
32. Write a program to input a number and check whether it is a Fibonacci number or not. A
number is a Fibonacci number if it is a term in the Fibonacci series.
33. Write a menu driven program to calculate the total surface area and volume of a cube,
cuboid, or sphere depending upon user’s choice. The program should continue until the user
selects the option to exit the program.
34. Write a program to find the sum of first n terms of the following series without using inbuilt
function pow() and math.factorial():
(i) x + x2 + x3 + . . . (ii)
x - x2 + x3 - . . .
x2 x3
(iii) 1+ x + + +...
2 3
x2 x3
(iv) 1+ x + + +...
2! 3!
x2 x3
(v) 1- x + - +...
2! 3!
x3 x5 x7 (vi) 1+ + +
...
3! 5! 7!
x3 x5 x7
(vii) 1- + - ...
3! 5! 7!
35. Write a program to generate n lines of the following pattern on the computer screen:
(i) 1
12
123
. .
(ii) 1
12
123
.
(iii) 1
121
12321
1234321 .
.
(iv) *
**
***
****
*****
.
.
(v) *
**
***
****
*****
.
.
36. Write a program to generate 2n+1 lines of the following pattern on the computer screen:
(i) *
***
*****
*******
*****
***
*
(ii) *
@@@
*****
@@@@@@@
*****
@@@
*
(iii) *
* *
* *
* *
*********
*******
*****
***
*
(iv) *
* *
* *
* *
* *
* *
*
ANSWERS
1. Loop is repetition of a set of statements.
There are 2 looping constructs available in Python. These are:
while and for
2. for loop iterates over the elements of an iterable whereas a while loop iterates while a
specified condition is true.
3. else clause of a loop gets executed after the normal completion of the loop. else clause is
not mandatory in a loop.
4. The break statement prematurely ends execution of the current while or for loop. It
brings the control to the statement immediately following the current control structure,
skipping the optional "else" clause if the loop has one.
The continue statement is used to skip the execution of the current iteration of a loop, and
continue with the next. continue does not terminate the loop, but continues with the next
iteration of the loop. Example:
#break and continue for i in
range(1,11): if (i%3 == 0):
continue; if (i*3 > 25):
break; print(i,end=' ')
else: print("Else part of
loop") print()
print(i);
5. A loop which never terminates is called an infinite loop. Example: i=1 while i<5:
print(i)
6. Nesting of loops means creating a loop within the body within the body of another loop. The
contained loop is called the nested loop or inner loop and the container loop is called the
outer loop. Example::
7.
(i) #display "Hello" 10 times for i in
range(1, 1011): OR range(1, 10):
print("Hello")
(ii) #To print first 10 natural numbers i=1
while (i<=10): print(i) i+=1
(iii) #To display first 10 even natural numbers
num=2 while (num<=20):
print(num)
num+=2 #Indentation
(iv) #To display first 10 odd natural numbers for i in
range (10): print(2*i+1)
8.
(i) a=int(input("Enter a number:
")) c==1 for b in range
(a,a*10.0,a):
c*=b
print(a,b,c)
(ii) a=int(input("Enter a number:
")) b=int(input("Enter another
number: ")) start,end=a,b if
a>b: start,end=b,a i=start
while i<=end: if
(i%start==0):
print(i,start,end='--')
i+=1 print(i)
(iii) a=int(input("Enter a number:
")) b=int(input("Enter another
number: ")) if a>b: step=-1
else: step=1 while a!=b: if
(a+b%2>0):
print(a+b,sep="--")
a=+step
(iv) a=int(input("Enter a number:
")) while a%5!=0: if
(a%2>0):
print(a,sep="--")
a+=1 else: print(a)
(v) x=123045 while x%10: x//=10
print(x,sep="--") else:
print(a) #Indentation
(vi) x=123045 while x%10: d=x%10
for i in range(d):
print(i)
else: print (d) #Indentation
print(x,sep='--')
x/=10 else: print(a)
9.
(i) 4 4 4
4 8 12
4 12 24
(ii) 4--8--11 #
for a=4, b=10
-4--0--3 # for a=2, b=-4
(iii) 14
13
12
11
10
9
(vi) 0 1
2
3
4
5
123045
0
1
2
3
4
12304
OR
OR
34.
(i) n=int(input("Enter the number of terms: "))
x=float(input("Enter the value of x: "))
sum=0 for i in range(1,n+1):
sum+=x**i print(sum)
35.
(i) n=int(input("Enter the number of lines: "))
for i in range(1,n+1): for j in
range(1,i+1): print(j,end='')
print()
36.
(i) n=int(input("Enter the value of n: ")) for i in
range(1,n+2): print(' '*(n+1-i),end='') #For
leading space in each line print('*'*(2*i-1))
for i in range(n,0,-1): print(' '*(n+1-
i),end='') #For leading space in each line
print('*'*(2*i-1))