0% found this document useful (0 votes)
11 views21 pages

Chapter 1 - Simple Python Programs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views21 pages

Chapter 1 - Simple Python Programs

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Leela Soft 1000 MCQ Programs Madhu

1. Simple Python Programs


Python Program to Calculate the Average of Numbers in a Given List
Python Program to Exchange the Values of Two Numbers Without Using a Temporary Variable
Python Program to Read a Number n and Compute n+nn+nnn
Python Program to Reverse a Given Number
Python Program to Check Whether a Number is Positive or Negative
Python Program to Take in the Marks of 5 Subjects and Display the Grade
Python Program to Print all Numbers in a Range Divisible by a Given Number
Python Program to Read Two Numbers and Print Their Quotient and Remainder
Python Program to Accept Three Digits and Print all Possible Combinations from the Digits
Python Program to Print Odd Numbers Within a Given Range
Python Program to Find the Sum of Digits in a Number
Python Program to Find the Smallest Divisor of an Integer
Python Program to Count the Number of Digits in a Number
Python Program to Check if a Number is a Palindrome
Python Program to Print all Integers that Aren't Divisible by Either 2 or 3 and Lie between 1 and
50.
Python Program to Read a Number n And Print the Series "1+2+…..+n= "
Python Program to Read a Number n and Print the Natural Numbers Summation Pattern
Python Program to Print an Identity Matrix
Python Program to Print an Inverted Star Pattern
Python Program to Read Print Prime Numbers in a Range using Sieve of Eratosthenes

2. Python Programming Examples on Mathematical Functions


Python Program to Check if a Date is Valid and Print the Incremented Date if it is
Python Program to Compute Simple Interest Given all the Required Values
Python Program to Check Whether a Given Year is a Leap Year
Python Program to Read Height in Centimeters and then Convert the Height to Feet and Inches
Python Program to Take the Temperature in Celcius and Covert it to Farenheit
Python Program to Compute Prime Factors of an Integer
Python Program to Generate all the Divisors of an Integer
Python Program to Print Table of a Given Number
Python Program to Print Sum of Negative Numbers, Positive Even Numbers and Positive Odd
numbers in a List
Python Program to Print Largest Even and Largest Odd Number in a List
Python Program to Form an Integer that has the Number of Digits at Ten's Place and the Least
Significant Digit of the Entered Integer at One's Place
Python Program to Find Those Numbers which are Divisible by 7 and Multiple of 5 in a Given
Range of Numbers
Python Program to Check if a Number is an Armstrong Number

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

3. Python Programming Examples on Lists


Python Program to Find the Largest Number in a List
Python Program to Find the Second Largest Number in a List
Python Program to Put Even and Odd elements in a List into Two Different Lists
Python Program to Merge Two Lists and Sort it
Python Program to Sort the List According to the Second Element in Sublist
Python Program to Find the Second Largest Number in a List Using Bubble Sort
Python Program to Sort a List According to the Length of the Elements
Python Program to Find the Union of two Lists
Python Program to Find the Intersection of Two Lists

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

4. Python Programming Examples on Strings


Python Program to Replace all Occurrences of ‘a’ with $ in a String
Python Program to Remove the nth Index Character from a Non-Empty String
Python Program to Detect if Two Strings are Anagrams
Python Program to Form a New String where the First Character and the Last Character have
been Exchanged
Python Program to Count the Number of Vowels in a String
Python Program to Take in a String and Replace Every Blank Space with Hyphen
Python Program to Calculate the Length of a String Without Using a Library Function
Python Program to Remove the Characters of Odd Index Values in a String
Python Program to Calculate the Number of Words and the Number of Characters Present in a
String
Python Program to Take in Two Strings and Display the Larger String without Using Built-in
Functions
Python Program to Count Number of Lowercase Characters in a String
Python Program to Check if a String is a Palindrome or Not
Python Program to Calculate the Number of Upper Case Letters and Lower Case Letters in a String
Python Program to Check if a String is a Pangram or Not
Python Program to Accept a Hyphen Separated Sequence of Words as Input and Print the Words
in a Hyphen-Separated Sequence after Sorting them Alphabetically
Python Program to Calculate the Number of Digits and Letters in a String

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

5. Python Programming Examples on Dictionary


Python Program to Add a Key-Value Pair to the Dictionary
Python Program to Concatenate Two Dictionaries Into One
Python Program to Check if a Given Key Exists in a Dictionary or Not
Python Program to Generate a Dictionary that Contains Numbers (between 1 and n) in the Form
(x,x*x).
Python Program to Sum All the Items in a Dictionary
Python Program to Multiply All the Items in a Dictionary
Python Program to Remove the Given Key from a Dictionary
Python Program to Form a Dictionary from an Object of a Class
Python Program to Map Two Lists into a Dictionary
Python Program to Count the Frequency of Words Appearing in a String Using a Dictionary
Python Program to Create a Dictionary with Key as First Character and Value as Words Starting
with that Character

6. Python Programming Examples on Sets


Python Program to Count the Number of Vowels Present in a String using Sets
Python Program to Check Common Letters in Two Input Strings
Python Program that Displays which Letters are in the First String but not in the Second
Python Program that Displays which Letters are Present in Both the Strings
Python Program that Displays which Letters are in the Two Strings but not in Both

www.leelasoft.com Cell: 78 42 66 47 66
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: "))
a.append(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)

www.leelasoft.com 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

www.leelasoft.com 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")

www.leelasoft.com 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

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

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

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.

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

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.

a = int(input("Enter first number:"))


b = int(input("Enter second number:"))
c = int(input("Enter third number:"))
d = []
d.append(a)
d.append(b)
d.append(c)
for i in range(0, 3):
for j in range(0, 3):
for k in range(0, 3):
if(i != j & j != k & k != i):
print(d[i], d[j], d[k])

Runtime Test Cases


Case 1:
Enter first number:1
Enter second number:2
Enter third number:3
123
132
213
231

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

Python Program to Print Odd Numbers Within a Given Range


Problem Description
The program takes the upper and lower limit and prints all odd numbers within a given range.

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.

lower = int(input("Enter the lower limit for the range:"))


upper = int(input("Enter the upper limit for the range:"))
for i in range(lower, upper + 1):
if(i % 2 != 0):
print(i)

Runtime Test Cases


Case 1:
Enter the lower limit for the range:1
Enter the upper limit for the range:16
1
3
5
7
9
11

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

Python Program to Find the Sum of Digits in a Number


Problem Description
The program takes in a number and finds the sum of digits in a number.

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

Runtime Test Cases


Case 1:
Enter a number:1892
The total sum of digits is: 20

Case 2:
Enter a number:157
The total sum of digits is: 13

Python Program to Find the Smallest Divisor of an Integer


Problem Description
The program takes in an integer and prints the smallest divisor of the integer.

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])

Runtime Test Cases


Case 1:
Enter an integer:75
Smallest divisor is: 3

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

Python Program to Count the Number of Digits in a Number


Problem Description
The program takes the number and prints the number of digits in the number.

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)

Runtime Test Cases


Case 1:
Enter number:123
The number of digits in the number are: 3

Case 2:
Enter number:1892
The number of digits in the number are: 4

Python Program to Check if a Number is a Palindrome


Problem Description
The program takes a number and checks whether it is a palindrome or not.

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!")

Runtime Test Cases


Case 1
Enter number:121
The number is 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.

for i in range(0, 51):


if(i % 2 != 0 and i % 3 != 0):
print(i)

Runtime Test Cases


Case 1:
1
5
7
11
13

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.

n = int(input("Enter a number: "))


a = []
for i in range(1, n + 1):
print(i, sep=" ", end=" ")
if(i < n):
print("+", sep=" ", end=" ")
a.append(i)
print("=", sum(a))
print()

Runtime Test Cases


Case 1:
Enter a number: 4
1 + 2 + 3 + 4 = 10

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.

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=" ")
a.append(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

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

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

www.leelasoft.com 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

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

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

www.leelasoft.com Cell: 78 42 66 47 66

You might also like