Python Programs Collection
1. Program to add two numbers
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
sum = a + b
print("Sum =", sum)
2. Program to check whether a number is even or odd
n = int(input("Enter number: "))
if n % 2 == 0:
print("Even")
else:
print("Odd")
3. Program to check whether a number is positive, negative or zero
n = int(input("Enter number: "))
if n > 0:
print("Positive")
elif n < 0:
print("Negative")
else:
print("Zero")
4. Program to generate random number between 1 to 100
import random
print("Random number:", random.randint(1, 100))
5. Program to calculate factorial
n = int(input("Enter number: "))
fact = 1
for i in range(1, n + 1):
fact = fact * i
print("Factorial =", fact)
6. Program to calculate sum of n natural numbers
n = int(input("Enter number: "))
sum = 0
for i in range(1, n + 1):
sum = sum + i
print("Sum =", sum)
7. Program to calculate average of three numbers
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
avg = (a + b + c) / 3
print("Average =", avg)
8. Program to calculate sum of digits
n = int(input("Enter number: "))
sum = 0
while n > 0:
r = n % 10
sum = sum + r
n = n // 10
print("Sum of digits =", sum)
9. Program to calculate product of digits
n = int(input("Enter number: "))
product = 1
while n > 0:
r = n % 10
product = product * r
n = n // 10
print("Product of digits =", product)
10. Program to check palindrome number
n = int(input("Enter number: "))
temp = n
rev = 0
while n > 0:
r = n % 10
rev = rev * 10 + r
n = n // 10
if temp == rev:
print("Palindrome")
else:
print("Not Palindrome")
11. Program to check Armstrong number
n = int(input("Enter number: "))
temp = n
sum = 0
while n > 0:
r = n % 10
sum = sum + (r ** 3)
n = n // 10
if temp == sum:
print("Armstrong number")
else:
print("Not Armstrong number")
12. Program to check prime or composite number
n = int(input("Enter number: "))
count = 0
for i in range(1, n + 1):
if n % i == 0:
count = count + 1
if count == 2:
print("Prime number")
else:
print("Composite number")
13. Program to find sum of even digits
n = int(input("Enter number: "))
sum = 0
while n > 0:
r = n % 10
if r % 2 == 0:
sum = sum + r
n = n // 10
print("Sum of even digits =", sum)
14. Program to find product of odd digits
n = int(input("Enter number: "))
product = 1
while n > 0:
r = n % 10
if r % 2 != 0:
product = product * r
n = n // 10
print("Product of odd digits =", product)
15. Program to display consonants from a string
s = input("Enter string: ")
for ch in s:
ch = ch.lower()
if ch not in "aeiou" and ch.isalpha():
print(ch, end=" ")
16. Program to count vowels in a string
s = input("Enter string: ")
count = 0
for ch in s:
if ch.lower() in "aeiou":
count = count + 1
print("Total vowels =", count)
17. Program to count vowels and consonants
s = input("Enter string: ")
vowels = 0
consonants = 0
for ch in s:
if ch.isalpha():
if ch.lower() in "aeiou":
vowels = vowels + 1
else:
consonants = consonants + 1
print("Vowels =", vowels)
print("Consonants =", consonants)
18. Program to count first letter repetition
s = input("Enter string: ")
first = s[0]
count = 0
for ch in s:
if ch == first:
count = count + 1
print("First letter repeats =", count)
19. Program to check palindrome string
s = input("Enter string: ")
rev = ""
for ch in s:
rev = ch + rev
if s == rev:
print("Palindrome")
else:
print("Not Palindrome")
20. Program to display alternate capital and small letters
s = input("Enter string: ")
result = ""
for i in range(len(s)):
if i % 2 == 0:
result = result + s[i].upper()
else:
result = result + s[i].lower()
print("Result =", result)
No comments:
Post a Comment