1. Input a welcome message and display it.
# Input a welcome message
welcome_message = input("Enter a welcome message: ")
# Display the welcome message
print("Your welcome message is:", welcome_message)
2. Input two numbers and display the larger / smaller number.
# Input two numbers
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
# Display the larger and smaller number
if num1 > num2:
print(f"The larger number is {num1} and the smaller number is {num2}.")
else:
print(f"The larger number is {num2} and the smaller number is {num1}.")
3. Input three numbers and display the largest / smallest number.
# Input three numbers
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
# Find and display the largest number
largest = max(num1, num2, num3)
print(f"The largest number is {largest}.")
# Find and display the smallest number
smallest = min(num1, num2, num3)
print(f"The smallest number is {smallest}.")
Pattern-1: Increasing stars
# Loop through each row
for i in range(1, 6): # 5 rows for the pattern
# For each row, print '*' i times
for j in range(i):
print('*', end='') # 'end' argument prevents new line after each print
print() # Move to the next line after each row
Pattern-2: Descending numbers
# Loop through each row
for i in range(5, 0, -1): # 5 rows, counting down to 1
# For each row, print numbers from 1 to i
for j in range(1, i + 1):
print(j, end='') # 'end' argument prevents new line after each print
print() # Move to the next line after each row
Pattern-3: Alphabet sequence
# Loop through each row
for i in range(1, 6): # 5 rows for the pattern
# For each row, print letters from A to the corresponding alphabet
for j in range(65, 65 + i): # 65 is ASCII value of 'A'
print(chr(j), end='') # Convert ASCII to character and print
print() # Move to the next line after each row
In above programs:
The outer loop (for i in range(...)) controls the number of rows.
The inner loop (for j in range(...)) controls the content of each row.
The print() function is used to display the pattern characters, and the end='' argument is
used to avoid moving to a new line after each character.
For Pattern-3, chr(j) is used to convert ASCII values to their corresponding characters,
starting from 65 (which is 'A').
Question: Write a program to determine whether a given number is a perfect number,
an Armstrong number, or a palindrome.