python programs
Sum of N Natural Numbers
# Program to calculate the sum of first N natural numbers
n = int(input("Enter a number: "))
sum_n = (n * (n + 1)) // 2 # Formula for the sum of the first N natural
numbers
print(f"The sum of first {n} natural numbers is {sum_n}")
Factorial of a Number
# Program to calculate the factorial of a number
n = int(input("Enter a number: "))
factorial = 1
for i in range(1, n + 1):
factorial *= i
print(f"The factorial of {n} is {factorial}")
Check Prime Number
# Program to check whether a number is prime
n = int(input("Enter a number: "))
is_prime = True
for i in range(2, int(n/2) + 1):
if n % i == 0:
is_prime = False
break
if is_prime:
print(f"{n} is a prime number")
else:
print(f"{n} is not a prime number")
Multiplication Table
# Program to print the multiplication table of a number
n = int(input("Enter a number: "))
for i in range(1, 11):
print(f"{n} x {i} = {n*i}")
Even or Odd
# Program to check whether a number is even or odd
n = int(input("Enter a number: "))
if n % 2 == 0:
print(f"{n} is an even number")
else:
print(f"{n} is an odd number")
Sum of Digits of a Number
# Program to calculate the sum of digits of a number
n = int(input("Enter a number: "))
sum_digits = 0
while n > 0:
sum_digits += n % 10
n = n // 10
print(f"The sum of the digits is {sum_digits}")
Count Vowels in a String
# Program to count the number of vowels in a string
string = input("Enter a string: ")
vowels = 'aeiou'
count = 0
for char in string:
if char.lower() in vowels:
count += 1
print(f"The number of vowels in the string is {count}")
Key Concepts Covered:
1. Loops: Using for and while loops.
2. Conditionals: if, else, elif.
3. Functions: Simple functions (though none of these explicitly use functions, some
could be converted to function-based programs).
4. String Manipulation: Reversing strings, slicing, and checking for palindromes.
5. Mathematical Computations: Factorials, prime number checking, Fibonacci series,
Armstrong numbers.
6. Basic Input/Output: Getting user input and displaying results.