Python Programs
1. Check if a Number is Positive, Negative, or Zero
# Program to check if a number is positive, negative, or zero
number = float(input("Enter a number: "))
if number > 0:
print("The number is positive.")
elif number < 0:
print("The number is negative.")
else:
print("The number is zero.")
2. Check if a Number is Even or Odd
# Program to check if a number is even or odd
number = int(input("Enter an integer: "))
if number % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")
3. Check if a Person is Eligible to Vote
# Program to check if a person is eligible to vote
age = int(input("Enter your age: "))
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")
4. Find the Largest of Three Numbers
# Program to find the largest of three numbers
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
if num1 >= num2 and num1 >= num3:
print("The largest number is:", num1)
elif num2 >= num1 and num2 >= num3:
print("The largest number is:", num2)
else:
print("The largest number is:", num3)
5. Check if a Year is a Leap Year
# Program to check if a year is a leap year
year = int(input("Enter a year: "))
if (year % 4 == 0 and year % 100 != 0):
print(year, "is a leap year.")
else:
print(year, "is not a leap year.")
6. Print Numbers from 1 to 10
# Program to print numbers from 1 to 10
for i in range(1, 11):
print(i)
7. Calculate the Sum of All Numbers in a List
# Program to calculate the sum of all numbers in a list
numbers = [10, 20, 30, 40, 50]
total = 0
for number in numbers:
total += number
print("The sum of all numbers in the list is:", total)
8. Print Each Character of a String
# Program to print each character of a string
text = "Python"
for char in text:
print(char)
9. Find the Factorial of a Number
(for example Factorial of 5 is = 1X2X3X4X5=120)
# Program to find the factorial of a number
num = int(input("Enter a number: "))
factorial = 1
for i in range(1, num + 1):
factorial *= i
print("The factorial of", num, "is", factorial)
10. Print the Multiplication Table of a Given Number
# Program to print the multiplication table of a given number
num = int(input("Enter a number: "))
print("Multiplication Table of”, num")
for i in range(1, 11):
print(num,” x “,I,” = “,num * i")
11. Print Even Numbers Between 1 and 20
# Program to print even numbers between 1 and 20
for i in range(1, 21):
if i % 2 == 0:
print(i)
12. Count the Number of Vowels in a String
# Program to count the number of vowels in a string
text = "Hello, how are you?"
vowels = "aeiouAEIOU"
count = 0
for char in text:
if char in vowels:
count += 1
print("The number of vowels in the string is:", count)
13. Sum of all digits of a given number
# Program to get the sum of all digits of a number
number = input("Enter a number: ")
sum_of_digits = 0
for digit in number:
sum_of_digits += int(digit)
print("The sum of all digits is:", sum_of_digits)
14. Given number is Armstrong number or not.
An Armstrong number (or Narcissistic number) is a number that is equal to the
sum of its own digits each raised to the power of the number of digits. For
example, 153 is an Armstrong number because 13+53+33=153. The Armstrong
numbers of first kind up to 5 digits are 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371,
407, 1634, 8208, 9474, 54748, 92727, 93084.
# Program to check if a number is an Armstrong number
number = int(input("Enter a number: "))
num_str = str(number)
num_digits = len(num_str)
sum_of_powers = sum(int(digit) ** num_digits for digit in num_str)
if sum_of_powers == number:
print(f"{number} is an Armstrong number.")
else:
print(f"{number} is not an Armstrong number.")
14. Display all Armstrong number from 1 to 99999.
# Program to display all Armstrong number from 1 to 99999.
for number in range(1,100000):
num_str = str(number)
num_digits = len(num_str)
sum_of_powers = sum(int(digit) ** num_digits for digit in num_str)
if sum_of_powers == number:
print(number)
15. Program to Print Numbers from 1 to 10
i=1
while i <= 10:
print(i)
i += 1
16. Program to Calculate the Sum of Natural Numbers
n = 10
sum = 0
i=1
while i <= n:
sum += i
i += 1
print(f"The sum of the first {n} natural numbers is: {sum}")
17. Program to Find the Factorial of a Number
num = 5
factorial = 1
i=1
while i <= num:
factorial *= i
i += 1
print(f"The factorial of {num} is: {factorial}")
18. Program to Reverse a Given Number
num = 12345
reversed_num = 0
while num > 0:
digit = num % 10
reversed_num = reversed_num * 10 + digit
num //= 10
print(f"The reversed number is: {reversed_num}")
19. Program to check if a Number is a Palindrome (a word, phrase, or
sequence that reads the same backwards as forwards, e.g. madam)
num = 121
original_num = num
reversed_num = 0
while num > 0:
digit = num % 10
reversed_num = reversed_num * 10 + digit
num //= 10
if original_num == reversed_num:
print(f"{original_num} is a palindrome.")
else:
print(f"{original_num} is not a palindrome.")
20. Program to Generate Fibonacci sequence (0,1,1,2,3,5,8,11)
F(x)=F(x-1)+F(x-2)
n = 10
a, b = 0, 1
count = 0
while count < n:
print(a)
nth = a + b
a=b
b = nth
count += 1