Q1. Unit Conversion
Write a Python program that accepts a distance in meters from the user and converts it into centimeters.
- Expected Input:
2.5 - Expected Output:
2.5 meters is equal to 250.0 cm
Q2. Area Calculation
Write a program to calculate and display the area of a rectangle. The length and breadth should be provided as user inputs.
- Expected Input:
Length = 5, Breadth = 4 - Expected Output:
Area of rectangle = 20
Q3. Parity Check
Write a program to input an integer from the user and check whether it is an Odd number or an Even number using conditional statements.
- Expected Input:
17 - Expected Output:
17 is an Odd number
Loop Control Structures
Q4. Mathematical Factorial
Write a program to calculate the factorial of a given positive integer using a loop. (Note: Factorial of 5 is 5 x 4 x 3 x 2 x 1 = 120)
- Expected Input:
5 - Expected Output:
Factorial of 5 is 120
Q5. Sentinel-Controlled Summation
Write a program that repeatedly accepts numbers from the user and calculates their running total. The loop should stop taking inputs and display the final sum as soon as the user enters 0.
- Expected Input Sequence:
10, 5, 20, 0 - Expected Output:
Total Sum: 35
Q6. Prime Number Verification
Write a program to check whether a given positive integer is a Prime number or not.
- Expected Input:
13 - Expected Output:
13 is a Prime number
Q7. Fibonacci Sequence Generation
Write a program to display the Fibonacci series up to $n$ terms, where $n$ is entered by the user.
- Expected Input:
6 - Expected Output:
0 1 1 2 3 5
Number Property Analysis
Q8. Armstrong Number Check
Write a Python program to determine if an input 3-digit number is an Armstrong number. (A number is an Armstrong number if the sum of the cubes of its digits equals the number itself, e.g., $1^3 + 5^3 + 3^3 = 153$)
- Expected Input:
153 - Expected Output:
153 is an Armstrong number
Nested Loops & Pattern Printing
Q9. Right-Angled Triangle Pattern
Write a program using nested loops to display the following right-angled triangle pattern for $n$ rows (where $n$ is input by the user):
Plaintext
*
* *
* * *
* * * *
=================================================================
Answers:
1.1. Convert Meter to Centimeters:
Python
# Program to convert meters to centimeters
meters = float(input("Enter distance in meters: "))
centimeters = meters * 100
print(meters, "meters is equal to", centimeters, "cm")
2.2. Area of a Rectangle:
Python
# Program to calculate the area of a rectangle
length = float(input("Enter length: "))
breadth = float(input("Enter breadth: "))
area = length * breadth
print("Area of rectangle =", area)
3.3. Odd or Even Check:
Python
# Program to check if a number is odd or even
num = int(input("Enter a number: "))
if num % 2 == 0:
print(num, "is an Even number")
else:
print(num, "is an Odd number")
4.4. Factorial of a Number:
Python
# Program to find the factorial of a number
num = int(input("Enter a number: "))
factorial = 1
if num < 0:
print("Factorial does not exist for negative numbers")
else:
for i in range(1, num + 1):
factorial = factorial * i
print("Factorial of", num, "is", factorial)
5.5. Sum of Inputted Numbers Until 0:
Python
# Program to sum numbers until 0 is entered
total_sum = 0
while True:
num = float(input("Enter a number (0 to stop): "))
if num == 0:
break
total_sum = total_sum + num
print("Total Sum:", total_sum)
6.6. Prime Number Check:
Python
# Program to check if a number is prime
num = int(input("Enter a number: "))
is_prime = True
if num <= 1:
is_prime = False
else:
for i in range(2, num):
if num % i == 0:
is_prime = False
break
if is_prime:
print(num, "is a Prime number")
else:
print(num, "is not a Prime number")
7.7. Fibonacci Series:
Python
# Program to print Fibonacci series up to n terms
n = int(input("Enter number of terms: "))
a = 0
b = 1
if n <= 0:
print("Please enter a positive integer")
elif n == 1:
print(a)
else:
print("Fibonacci series:")
for i in range(n):
print(a, end=" ")
c = a + b
a = b
b = c
print()
8.8. Armstrong Number Check (3-Digit):
Python
# Program to check if a 3-digit number is an Armstrong number
num = int(input("Enter a 3-digit number: "))
temp = num
sum_cubes = 0
while temp > 0:
digit = temp % 10
sum_cubes = sum_cubes + (digit ** 3)
temp = temp // 10
if num == sum_cubes:
print(num, "is an Armstrong number")
else:
print(num, "is not an Armstrong number")
9.9. Star Pattern Printing:
Python
# Program to print right-angled star pattern
rows = int(input("Enter number of rows: "))
for i in range(1, rows + 1):
for j in range(1, i + 1):
print("*", end=" ")
print()