Chapter 1 - Simple Python Programs
Chapter 1 - Simple Python Programs
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
Python Program to Print the Pascal's triangle for n number of rows given by the user
Python Program to Check if a Number is a Perfect Number
Python Program to Check if a Number is a Strong Number
Python Program to Find the LCM of Two Numbers
Python Program to Find the GCD of Two Numbers
Python Program to Compute a Polynomial Equation given that the Coefficients of the Polynomial
are stored in a List
Python Program to Check If Two Numbers are Amicable Numbers
Python Program to Find the Area of a Triangle Given All Three Sides
Python Program to Find the Gravitational Force Acting Between Two Objects
Python Program to Check if a Number is a Prime Number
Python Program to Print all the Prime Numbers within a Given Range
Python Program to Print Numbers in a Range (1,upper) Without Using any Loops
Python Program to Find the Sum of Sine Series
Python Program to Find the Sum of Cosine Series
Python Program to Find the Sum of First N Natural Numbers
Python Program to Find the Sum of the Series: 1 + 1/2 + 1/3 + ….. + 1/N
Python Program to Find the Sum of the Series: 1 + x^2/2 + x^3/3 + … x^n/n
Python Program to Compute the Value of Euler's Number e. Use the Formula: e = 1 + 1/1! + 1/2!
+ …… 1/n!
Python Program to Determine all Pythagorean Triplets in the Range
Python Program to Search the Number of Times a Particular Number Occurs in a List
Python Program to test Collatz Conjecture for a Given Number
Python Program to Count Set Bits in a Number
Python Program to Find Whether a Number is a Power of Two
Python Program to Clear the Rightmost Set Bit of a Number
Python Program to Generate Gray Codes using Recursion
Python Program to Convert Gray Code to Binary
Python Program to Convert Binary to Gray Code
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
Python Program to Create a List of Tuples with the First Element as the Number and Second
Element as the Square of the Number
Python Program to Find all Numbers in a Range which are Perfect Squares and Sum of all Digits
in the Number is Less than 10
Python Program to Find the Cumulative Sum of a List where the ith Element is the Sum of the
First i+1 Elements From The Original List
Python Program to Generate Random Numbers from 1 to 20 and Append Them to the List
Python program to Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple
Python Program to Swap the First and Last Value of a List
Python Program to Remove the Duplicate Items from a List
Python Program to Read a List of Words and Return the Length of the Longest One
Python Program to Remove the ith Occurrence of the Given Word in a List where Words can
Repeat
Python Program to Remove All Tuples in a List of Tuples with the USN Outside the Given Range
Python Program to solve Maximum Subarray Problem using Divide and Conquer
Python Program to solve Maximum Subarray Problem using Kadane’s Algorithm
Python Program to Find Element Occurring Odd Number of Times in a List
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
Python Program to Form a New String Made of the First 2 and Last 2 characters From a Given
String
Python Program to Count the Occurrences of Each Word in a Given String Sentence
Python Program to Check if a Substring is Present in a Given String
Python Program to Print All Permutations of a String in Lexicographic Order without Recursion
Python Program to Print All Permutations of a String in Lexicographic Order using Recursion
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
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.
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.
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
Case 2
Enter value of first variable: 56
Enter value of second variable: 25
a is: 25 b is: 56
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.
Case 2:
Enter a number n: 20
The value is: 204060
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
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.
Case 2:
Enter number: 4538
Reverse of the number: 8354
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.
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
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.
www.leelasoft.com 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
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.
www.leelasoft.com 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.
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
Case 2:
Enter the first number: 125
Enter the second number: 7
Quotient is: 17
Remainder is: 6
Python Program to Accept Three Digits and Print all Possible Combinations from the Digits
Problem Description
The program takes three distinct numbers and prints all possible combinations from the digits.
Problem Solution
1. Take in the first, second and third number and store it in separate variables.
2. Then append all the three numbers to the list.
3. Use three for loops and print the digits in the list if none of their indexes are equal to each
other.
4. Exit.
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
312
321
Case 2:
Enter first number:5
Enter second number:7
Enter third number:3
573
537
753
735
357
375
Problem Solution
1. Take in the upper range limit and the lower range limit and store it in separate variables.
2. Use a for-loop ranging from the lower range to the upper range limit.
3. Then use an if statement if check whether the number is odd or not and print the number.
4. Exit.
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
13
15
Case 2:
Enter the lower limit for the range:150
Enter the upper limit for the range:180
151
153
155
157
159
161
163
165
167
169
171
173
175
177
179
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 add the digits to a variable.
3. Print the sum of the digits of the number.
4. Exit.
n = int(input("Enter a number:"))
tot = 0
while(n > 0):
dig = n % 10
tot = tot + dig
n = n // 10
print("The total sum of digits is:", tot)
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
Case 2:
Enter a number:157
The total sum of digits is: 13
Problem Solution
1. Take in an integer from the user.
2. Use a for loop where the value of i ranges from 2 to the integer.
3. If the number is divisible by i, the value of i is appended to the list.
4. The list is then sorted and the smallest element is printed.
5. Exit.
n = int(input("Enter an integer:"))
a = []
for i in range(2, n + 1):
if(n % i == 0):
a.append(i)
a.sort()
print("Smallest divisor is:", a[0])
Case 2:
Enter an integer:64
Smallest divisor is: 2
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
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 increment the count each time a
digit is obtained.
3. Print the number of digits in the given integer.
4. Exit.
n = int(input("Enter number:"))
count = 0
while(n > 0):
count = count + 1
n = n // 10
print("The number of digits in the number are:", count)
Case 2:
Enter number:1892
The number of digits in the number are: 4
Problem Solution
1. Take the value of the integer and store in a variable.
2. Transfer the value of the integer into another temporary variable.
3. Using a while loop, get each digit of the number and store the reversed number in another
variable.
4. Check if the reverse of the number is equal to the one in the temporary variable.
5. Print the final result.
6. Exit.
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
n = int(input("Enter number:"))
temp = n
rev = 0
while(n > 0):
dig = n % 10
rev = rev * 10 + dig
n = n // 10
if(temp == rev):
print("The number is a palindrome!")
else:
print("The number isn't a palindrome!")
Case 2
Enter number:567
The number isn't a palindrome!
Python Program to Print all Integers that Aren't Divisible by Either 2 or 3 and Lie between 1 and
50.
Problem Description
The program prints all integers that aren’t divisible by either 2 or 3 and lies between 1 and 50.
Problem Solution
1. Use a for-loop ranging from 0 to 51.
2. Then use an if statement to check if the number isn’t divisible by both 2 and 3.
3. Print the numbers satisfying the condition.
4. Exit.
www.leelasoft.com Cell: 78 42 66 47 66
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.
Case 2:
www.leelasoft.com 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.
print()
Case 2:
Enter a number: 5
www.leelasoft.com 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
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.
Case 2:
Enter a number: 5
10000
01000
00100
00010
00001
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
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.
Case 2:
Enter number of rows: 10
**********
*********
********
*******
******
*****
****
***
**
*
Python Program to Read Print Prime Numbers in a Range using Sieve of Eratosthenes
Problem Description
www.leelasoft.com 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
print()
Case 2:
Enter upper limit of range: 15
2 3 5 7 11 13
www.leelasoft.com Cell: 78 42 66 47 66