Leela Soft 1000 MCQ Programs Madhu
Simple Python Programs
Python Program to Calculate the Average of Numbers in a Given List
Problem Description:
The program takes the elements of the list one by one and displays the average of the elements
of the list.
Problem Solution
1. Take the number of elements to be stored in the list as input.
2. Use a for loop to input elements into the list.
3. Calculate the total sum of elements in the list.
4. Divide the sum by total number of elements in the list.
5. Exit.
n = int(input("Enter the number of elements to be inserted: "))
a = []
for i in range(0, n):
elem = int(input("Enter element: "))
[Link](elem)
avg = sum(a) / n
print("Average of elements in the list", round(avg, 2))
Python Program to Exchange the Values of Two Numbers Without Using a Temporary Variable
Problem Description
The program takes both the values from the user and swaps them without using temporary
variable.
Problem Solution
1. Take the values of both the elements from the user.
2. Store the values in separate variables.
3. Add both the variables and store it in the first variable.
4. Subtract the second variable from the first and store it in the second variable.
5. Then, subtract the first variable from the second variable and store it in the first variable.
6. Print the swapped values.
7. Exit.
a = int(input("Enter value of first variable: "))
b = int(input("Enter value of second variable: "))
a = a + b
b = a - b
a = a - b
print("a is:", a, " b is:", b)
[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
Runtime Test Cases
Case 1
Enter value of first variable: 3
Enter value of second variable: 5
a is: 5 b is: 3
Case 2
Enter value of first variable: 56
Enter value of second variable: 25
a is: 25 b is: 56
Python Program to Read a Mumber n and Compute n+nn+nnn
Problem Description
The program takes a number n and computes n+nn+nnn.
Problem Solution
1. Take the value of a element and store in a variable n.
2. Convert the integer into string and store it in another variable.
3. Add the string twice so the string gets concatenated and store it in another variable.
4. Then add the string thrice and assign the value to the third variable.
5. Convert the strings in the second and third variables into integers.
6. Add the values in all the integers.
7. Print the total value of the expression.
8. Exit.
n = int(input("Enter a number n: "))
temp = str(n)
t1 = temp + temp
t2 = temp + temp + temp
comp = n + int(t1) + int(t2)
print("The value is:", comp)
Runtime Test Cases
Case 1:
Enter a number n: 5
The value is: 615
Case 2:
Enter a number n: 20
The value is: 204060
[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
Python Program to Reverse a Given Number
Problem Description
The program takes a number and reverses it.
Problem Solution
1. Take the value of the integer and store in a variable.
2. Using a while loop, get each digit of the number and store the reversed number in another
variable.
3. Print the reverse of the number.
4. Exit.
n = int(input("Enter number: "))
rev = 0
while(n > 0):
dig = n % 10
rev = rev * 10 + dig
n = n // 10
print("Reverse of the number:", rev)
Runtime Test Cases
Case 1:
Enter number: 124
Reverse of the number: 421
Case 2:
Enter number: 4538
Reverse of the number: 8354
Python Program to Check Whether a Number is Positive or Negative
Problem Description
The program takes a number and checks whether it is positive or negative.
Problem Solution
1. Take the value of the integer and store in a variable.
2. Use an if statement to determine whether the number is positive or negative.
3. Exit.
n = int(input("Enter number: "))
if(n > 0):
print("Number is positive")
else:
print("Number is negative")
[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
Runtime Test Cases
Case 1:
Enter number: 45
Number is positive
Case 2:
Enter number: -30
Number is negative
Python Program to Take in the Marks of 5 Subjects and Display the Grade
Problem Description
The program takes in the marks of 5 subjects and displays the grade.
Problem Solution
1. Take in the marks of 5 subjects from the user and store it in different variables.
2. Find the average of the marks.
3. Use an else condition to decide the grade based on the average of the marks.
4. Exit.
sub1 = int(input("Enter marks of the first subject: "))
sub2 = int(input("Enter marks of the second subject: "))
sub3 = int(input("Enter marks of the third subject: "))
sub4 = int(input("Enter marks of the fourth subject: "))
sub5 = int(input("Enter marks of the fifth subject: "))
avg = (sub1 + sub2 + sub3 + sub4 + sub4) / 5
if(avg >= 90):
print("Grade: A")
elif(avg >= 80 & avg < 90):
print("Grade: B")
elif(avg >= 70 & avg < 80):
print("Grade: C")
elif(avg >= 60 & avg < 70):
print("Grade: D")
else:
print("Grade: F")
Runtime Test Cases
Case 1:
Enter marks of the first subject: 85
Enter marks of the second subject: 95
Enter marks of the third subject: 99
Enter marks of the fourth subject: 93
Enter marks of the fifth subject: 100
[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
Grade: A
Case 2:
Enter marks of the first subject: 81
Enter marks of the second subject: 72
Enter marks of the third subject: 94
Enter marks of the fourth subject: 85
Enter marks of the fifth subject: 80
Grade: B
Python Program to Print all Numbers in a Range Divisible by a Given Number
Problem Description
The program prints all numbers in a range divisible by a given number.
Problem Solution
1. Take in the upper range and lower range limit from the user.
2. Take in the number to be divided by from the user.
3. Using a for loop, print all the factors which is divisible by the number.
4. Exit.
lower = int(input("Enter lower range limit:"))
upper = int(input("Enter upper range limit:"))
n = int(input("Enter the number to be divided by:"))
for i in range(lower, upper + 1):
if(i % n == 0):
print(i)
Runtime Test Cases
Case 1:
Enter lower range limit:1
Enter upper range limit:50
Enter the number to be divided by:5
5
10
15
20
25
30
[Link] Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
35
40
45
50
Case 2:
Enter lower range limit:50
Enter upper range limit:100
Enter the number to be divided by:7
56
63
70
77
84
91
98
Python Program to Read Two Numbers and Print Their Quotient and Remainder
Problem Description
The program takes two numbers and prints the quotient and remainder.
Problem Solution
1. Take in the first and second number and store it in separate variables.
2. Then obtain the quotient using division and the remainder using modulus operator.
3. Exit.
a = int(input("Enter the first number: "))
b = int(input("Enter the second number: "))
quotient = a // b
remainder = a % b
print("Quotient is:", quotient)
print("Remainder is:", remainder)
Runtime Test Cases
Case 1:
Enter the first number: 15
Enter the second number: 7
Quotient is: 2
Remainder is: 1
[Link] Cell: 78 42 66 47 66