Leela Soft 1000 MCQ Programs Madhu
17
19
23
25
29
31
35
37
41
43
47
49
Python Program to Read a Number n And Print the Series "1+2+…..+n= "
Problem Description
The program takes a number n and prints and computes the series “1+2+…+n=”.
Problem Solution
1. Take a value from the user and store it in a variable n.
2. Use a for loop where the value of i ranges between the values of 1 and n.
3. Print the value of i and ‘+’ operator while appending the value of i to a list.
4. Then find the sum of elements in the list.
5. Print ‘=’ followed by the total sum.
6. Exit.
n = int(input("Enter a number: "))
a = []
for i in range(1, n + 1):
print(i, sep=" ", end=" ")
if(i < n):
print("+", sep=" ", end=" ")
[Link](i)
print("=", sum(a))
print()
Runtime Test Cases
Case 1:
Enter a number: 4
1 + 2 + 3 + 4 = 10
Case 2:
[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
Enter a number: 5
1 + 2 + 3 + 4 + 5 = 15
Python Program to Read a Number n and Print the Natural Numbers Summation Pattern
Problem Description
The program takes a number n and prints the natural numbers summation pattern.
Problem Solution
1. Take a value from the user and store it in a variable n.
2. Use two for loop where the value of j ranges between the values of 1 and n and value of
i ranges between 1 and j.
3. Print the value of i and ‘+’ operator while appending the value of i to a list.
4. Then find the sum of elements in the list.
5. Print ‘=’ followed by the total sum.
6. Exit.
n = int(input("Enter a number: "))
for j in range(1, n + 1):
a = []
for i in range(1, j + 1):
print(i, sep=" ", end=" ")
if(i < j):
print("+", sep=" ", end=" ")
[Link](i)
print("=", sum(a))
print()
Runtime Test Cases
Case 1:
Enter a number: 4
1=1
1+2=3
1+2+3=6
1 + 2 + 3 + 4 = 10
Case 2:
Enter a number: 5
[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
1=1
1+2=3
1+2+3=6
1 + 2 + 3 + 4 = 10
1 + 2 + 3 + 4 + 5 = 15
Python Program to Print an Identity Matrix
Problem Description
The program takes a number n and prints an identity matrix of the desired size.
Problem Solution
1. Take a value from the user and store it in a variable n.
2. Use two for loop where the value of j ranges between the values of 0 and n-1 and value
of i also ranges between 0 and n-1.
3. Print the value of 1 when i is equal to j and 0 otherwise.
4. Exit.
n = int(input("Enter a number: "))
for i in range(0, n):
for j in range(0, n):
if(i == j):
print("1", sep=" ", end=" ")
else:
print("0", sep=" ", end=" ")
print()
Runtime Test Cases
Case 1:
Enter a number: 4
1000
0100
0010
0001
Case 2:
Enter a number: 5
10000
01000
00100
00010
00001
[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
Python Program to Print an Inverted Star Pattern
Problem Description
The program takes a number n and prints an inverted star pattern of the desired size.
Problem Solution
1. Take a value from the user and store it in a variable n.
2. Use a for loop where the value of i ranges between the values of n-1 and 0 with a
decrement of 1 with each iteration.
3. Multiply empty spaces with n-i and ‘*’ with i and print both of them.
4. Exit.
n = int(input("Enter number of rows: "))
for i in range (n, 0, -1):
print((n - i) * ' ' + i * '*')
Runtime Test Cases
Case 1:
Enter number of rows: 5
*****
****
***
**
*
Case 2:
Enter number of rows: 10
**********
*********
********
*******
******
*****
****
***
**
*
Python Program to Read Print Prime Numbers in a Range using Sieve of Eratosthenes
Problem Description
[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
The program takes a range and prints prime numbers in that range using Sieve of Eratosthenes.
Problem Solution
1. Take the value of n from the user.
2. Initialise the sieve with numbers from 2 to n.
3. Use a while loop with the condition that the sieve is not empty
4. Get the smallest number that is prime
5. Remove that number and it’s multiples
6. Continue till the sieve is empty
7. Exit
n = int(input("Enter upper limit of range: "))
sieve = set(range(2, n + 1))
while sieve:
prime = min(sieve)
print(prime, end="\t")
sieve -= set(range(prime, n + 1, prime))
print()
Runtime Test Cases
Case 1:
Enter upper limit of range: 10
2 3 5 7
Case 2:
Enter upper limit of range: 15
2 3 5 7 11 13
[Link] Cell: 78 42 66 47 66