X1 Computer Science
List of Python Program – 30 Mark
1. Write a python program for Input a welcome message and display it.
CODE:
welcome_message = input("Please enter your welcome message: ")
# Display the welcome message
print("Your welcome message is:")
print(welcome_message)
OUTPUT
Please enter your welcome message: Hi good morning
Your welcome message is:
Hi good morning
--------------------------------------------------------------------------------------------------------
2.Write a python program for Input two numbers and display the larger / smaller
number.
CODE:
# Input two numbers from the user
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
# Determine the larger and smaller number
if num1 > num2:
larger = num1
smaller = num2
elif num2 > num1:
larger = num2
smaller = num1
else:
larger = smaller = num1 # Both numbers are equal
# Display the results
print(f"The larger number is: {larger}")
print(f"The smaller number is: {smaller}")
OUTPUT:
Enter the first number: 89
Enter the second number: 41
The larger number is: 89.0
The smaller number is: 41.0
----------------------------------------------------------------------------------------------------------------
3. Write a python program for Input three numbers and display the largest / smallest
number.
CODE:
# Input three numbers from the user
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
# Determine the largest and smallest number
largest = max(num1, num2, num3)
smallest = min(num1, num2, num3)
# Display the results
print(f"The largest number is: {largest}")
print(f"The smallest number is: {smallest}")
OUTPUT:
Enter the first number: 100
Enter the second number: 95
Enter the third number: 71
The largest number is: 100.0
The smallest number is: 71.0
___________________________________________________________________
4. Write a python program for Generate the following patterns using nested loops:
Pattern-1 Pattern-2 Pattern-3
* 12345 A
** 1234 AB
*** 123 ABC
**** 12 ABCD
***** 1 ABCDE
CODE:
# Pattern 1
print("Pattern 1:")
for i in range(1, 6):
print('*' * i)
print() # For spacing between patterns
# Pattern 2
print("Pattern 2:")
for i in range(5, 0, -1):
print(''.join(str(x) for x in range(1, i + 1)))
print() # For spacing between patterns
# Pattern 3
print("Pattern 3:")
for i in range(1, 6):
print(''.join(chr(65 + j) for j in range(i)))
OUTPUT:
Pattern 1:
*
**
***
****
*****
Pattern 2:
12345
1234
123
12
1
Pattern 3:
A
AB
ABC
ABCD
ABCDE
________________________________________________________________
5. Write a python program to input the value of x and n and print the sum of the
following series:
1+𝑥+𝑥 +𝑥 +𝑥 +⋯𝑥n
CODE:
# Input values for x and n
x = float(input("Enter the value of x: "))
n = int(input("Enter the value of n: "))
# Initialize the sum
sum_series = 0
# Calculate the sum of the series
for i in range(n + 1):
sum_series += x ** i
# Include the first term (1)
sum_series += 1
# Display the result
print(f"The sum of the series 1 + x + x^2 + ... + x^{n} is: {sum_series}")
OUTPUT:
Enter the value of x: 2
Enter the value of n: 10
The sum of the series 1 + x + x2 + ... + x10 is: 2048.0
___________________________________________________________________
Write a python program to input the value of x and n and print the sum of the
following series:
1−𝑥+𝑥2 –𝑥3 +𝑥4 −⋯𝑥N
CODE:
# Input values for x and n
x = float (input("Enter the value of x: "))
n = int (input("Enter the value of n: "))
# Initialize the sum
sum_series = 0
# Calculate the sum of the series
for i in range(n + 1):
term = (-1) ** i * (x ** i) # Alternate signs using (-1)^i
sum_series += term
# Display the result
print(f"The sum of the series 1 - x + x^2 - x^3 + ... + (-1)^{n} x^{n} is: {sum_series}")
OUTPUT:
Enter the value of x: 5
Enter the value of n: 20
The sum of the series 1 - x + x2 - x3 + ... + (-1)20 x20 is: 79472859700521.0
__________________________________________________________________
6. Write a python program for Determine whether a number is a perfect number, an
Armstrong number or a palindrome.
CODE:
def is_perfect_number(num):
"""Check if a number is a perfect number."""
divisors_sum = sum(i for i in range(1, num) if num % i == 0)
return divisors_sum == num
def is_armstrong_number(num):
"""Check if a number is an Armstrong number."""
digits = str(num)
power = len(digits)
return sum(int(digit) ** power for digit in digits) == num
def is_palindrome(num):
"""Check if a number is a palindrome."""
return str(num) == str(num)[::-1]
# Input a number from the user
number = int(input("Enter a number: "))
# Check for each condition
if is_perfect_number(number):
print(f"{number} is a perfect number.")
else:
print(f"{number} is not a perfect number.")
if is_armstrong_number(number):
print(f"{number} is an Armstrong number.")
else:
print(f"{number} is not an Armstrong number.")
if is_palindrome(number):
print(f"{number} is a palindrome.")
else:
print(f"{number} is not a palindrome.")
OUTPUT:
Enter a number: 9
9 is not a perfect number.
9 is an Armstrong number.
9 is a palindrome.
___________________________________________________________________