Practice session WEEK #11
1. Write a program to identify all prime numbers within a user-defined range. The
program should:
a. List all the prime numbers within the range.
b. Calculate and display the total count of prime numbers found.
c. Compute and display the sum of these prime numbers.
# Accept range from user
start = int(input("Enter the starting number: "))
end = int(input("Enter the ending number: "))
count = 0
sum = 0
print("Prime numbers in the given range are:")
for num in range(start, end + 1):
if num > 1:
prime = True
for i in range(2, num):
if num % i == 0:
prime = False
break
if prime:
print(num)
count= count + 1
sum= sum + num
print("Total number of prime numbers:", count)
print("Sum of prime numbers:", sum)
output:
Enter the starting number: 2
Enter the ending number: 9
Prime numbers in the given range are:
2
3
5
7
Total number of prime numbers: 4
Sum of prime numbers: 17
Dept of CSE, SPT, Tumkur 1
2. Write a Python program to perform the following tasks for a user-defined range
of integers:
a) Identify and list all palindrome numbers within the specified range.
b) Count the total number of palindrome numbers found.
c) Display the results in a user-friendly format.
# Input range from user
start = int(input("Enter starting number: "))
end = int(input("Enter ending number: "))
count = 0
print("Palindrome numbers in the given range:")
for num in range(start, end + 1):
if str(num) == str(num)[::-1]: # Check palindrome
print(num, end=" ")
count += 1
print("\nTotal palindrome numbers:", count)
output:
Enter starting number: 10
Enter ending number: 150
Palindrome numbers in the given range:
11 22 33 44 55 66 77 88 99 101 111 121 131 141
Total palindrome numbers: 14
3. Write a program to generate and display a multiplication table for numbers 1 to 10.
# Program to display multiplication tables from 1 to 10
for i in range(1, 11):
print("Multiplication Table of", i)
for j in range(1, 11):
print(i, "x", j, "=", i * j)
print() # blank line after each table
Dept of CSE, SPT, Tumkur 2
4. Generate star or number patterns like a pyramid or diamond shape.
# Pyramid pattern
rows = int(input("Enter number of rows: "))
for i in range(1, rows + 1):
print(" " * (rows - i), end=" ")
print("* " * i)
output:
Enter number of rows: 5
*
**
***
****
*****
Dept of CSE, SPT, Tumkur 3