1
INDEX
No. Program Title
1. Program to Find Factorial of a Number
2. Program to Check if a Number is Prime
3. Fibonacci Sequence Generator
4. Program to Reverse a String
5. Program to Check if a String is a Palindrome
6. Program to Sort a List Using Bubble Sort
7. Program to Implement Linear Search
8. Program to Find Maximum and Minimum in a
List
9. Program to Implement Binary Search
10. Program to Calculate the Sum of a List
11. Program to Count Vowels in a String
12. Program to Remove Duplicates from a List
13. Program to Check Armstrong Number
14. Program to Convert Temperature from
Celsius to Fahrenheit
15. Program to Create a Simple Calculator
3
16. Program to Count Words in a String
17. Program to Print Multiplication Table
18. Program to Find the Length of a List
19. Program to Check if a Year is a Leap Year
20. Program to Count Lines, Words, and
Characters in a Text File
4
1. Program to Find Factorial of a Number
Problem Statement: Write a Python
program to calculate the factorial of a
number.
Code:-
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
num = int(input("Enter a number: "))
print(f"Factorial of {num} is
{factorial(num)}")
Output:-
5
Enter a number: 5
Factorial of 5 is 120
6
2. Program to Check if a Number is Prime
Problem Statement: Write a Python
program to check whether a number is
prime.
Code:-
def is_prime(num):
if num < 2:
return False
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
return False
return True
num = int(input("Enter a number: "))
if is_prime(num):
print(f"{num} is prime.")
else:
print(f"{num} is not prime.")
7
Output:-
Enter a number: 7
7 is prime.
8
3. Fibonacci Sequence Generator
Problem Statement: Write a Python
program to generate the Fibonacci
sequence up to a given number.
CODE:-
def fibonacci(n):
sequence = [0, 1]
for i in range(2, n):
sequence.append(sequence[-1] +
sequence[-2])
return sequence
n = int(input("Enter the length of Fibonacci
sequence: "))
print(fibonacci(n))
9
OUTPUT:-
Enter the length of Fibonacci sequence:
10
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
10
4. Program to Reverse a String
Problem Statement: Write a Python
program to reverse a given string.
CODE:-
def reverse_string(s):
return s[::-1]
string = input("Enter a string: ")
print(f"Reversed string:
{reverse_string(string)}")
OUTPUT:-
Enter a string: hello
Reversed string: olleh
11
5. Program to Check if a String is a
Palindrome
Problem Statement: Write a Python
program to check if a string is a
palindrome.
CODE:-
def is_palindrome(s):
return s == s[::-1]
string = input("Enter a string: ")
if is_palindrome(string):
print(f"{string} is a palindrome.")
else:
print(f"{string} is not a palindrome.")
OUTPUT:-
Enter a string: madam
madam is a palindrome.
12
6. Program to Sort a List Using Bubble
Sort
Problem Statement: Write a Python
program to sort a list of numbers using
bubble sort.
CODE:-
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
return arr
arr = [64, 34, 25, 12, 22, 11, 90]
print("Sorted list:", bubble_sort(arr))
OUTPUT:-
Sorted list: [11, 12, 22, 25, 34, 64, 90]
13
7. Program to Implement Linear Search
Problem Statement: Write a Python
program to implement linear search.
CODE:-
def linear_search(arr, target):
for i, num in enumerate(arr):
if num == target:
return i
return -1
arr = [10, 20, 30, 40, 50]
target = int(input("Enter a number to
search: "))
result = linear_search(arr, target)
if result != -1:
print(f"Element found at index {result}")
else:
print("Element not found")
14
OUTPUT:-
Enter a number to search: 30
Element found at index 2
15
8. Program to Find Maximum and
Minimum in a List
Problem Statement: Write a Python
program to find the maximum and
minimum values in a list.
CODE:-
def find_max_min(arr):
return max(arr), min(arr)
arr = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3]
max_num, min_num = find_max_min(arr)
print(f"Maximum: {max_num}, Minimum:
{min_num}")
OUTPUT:-
Maximum: 9, Minimum: 1
16
9. Program to Implement Binary Search
Problem Statement: Write a Python
program to implement binary search.
CODE:-
def binary_search(arr, target):
low, high = 0, len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
target = int(input("Enter a number to
search: "))
17
result = binary_search(arr, target)
if result != -1:
print(f"Element found at index {result}")
else:
print("Element not found")
OUTPUT:-
Enter a number to search: 6
Element found at index 5
18
10. Program to Calculate the Sum of a
List
Problem Statement: Write a Python
program to calculate the sum of all
elements in a list.
CODE:-
def sum_of_list(arr):
return sum(arr)
arr = [10, 20, 30, 40]
print(f"Sum of list: {sum_of_list(arr)}")
OUTPUT:-
Sum of list: 100
19
11. Program to Count Vowels in a String
Problem Statement: Write a Python
program to count the number of vowels in
a string.
CODE:-
def count_vowels(s):
vowels = "aeiou"
count = 0
for char in s:
if char.lower() in vowels:
count += 1
return count
string = input("Enter a string: ")
print(f"Number of vowels:
{count_vowels(string)}")
OUTPUT:-
Enter a string: Hello World
Number of vowels: 3
20
12. Program to Remove Duplicates from a
List
Problem Statement: Write a Python
program to remove duplicates from a list.
CODE:-
def remove_duplicates(arr):
return list(set(arr))
arr = [1, 2, 2, 3, 4, 4, 5]
print(f"List after removing duplicates:
{remove_duplicates(arr)}")
OUTPUT:-
List after removing duplicates: [1, 2, 3, 4,
5]
21
13. Program to Check Armstrong Number
Problem Statement: Write a Python
program to check if a number is an
Armstrong number.
CODE:-
def is_armstrong(num):
digits = [int(digit) for digit in str(num)]
return num == sum([digit ** len(digits)
for digit in digits])
num = int(input("Enter a number: "))
if is_armstrong(num):
print(f"{num} is an Armstrong number.")
else:
print(f"{num} is not an Armstrong
number.")
OUTPUT:-
Enter a number: 153
153 is an Armstrong number.
22
14. Program to Convert Temperature from
Celsius to Fahrenheit
Problem Statement: Write a Python
program to convert temperature from
Celsius to Fahrenheit.
CODE:-
def celsius_to_fahrenheit(celsius):
return (celsius * 9/5) + 32
celsius = float(input("Enter temperature in
Celsius: "))
print(f"{celsius} Celsius is
{celsius_to_fahrenheit(celsius)}
Fahrenheit.")
OUTPUT:-
Enter temperature in Celsius: 25
25.0 Celsius is 77.0 Fahrenheit.
23
15. Program to Create a Simple
Calculator
Problem Statement: Write a Python
program to create a simple calculator.
CODE:-
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(a, b):
return a / b
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
24
operation = input("Enter operation (+, -, *,
/): ")
if operation == "+":
print(f"Result: {add(a, b)}")
elif operation == "-":
print(f"Result: {subtract(a, b)}")
elif operation == "*":
print(f"Result: {multiply(a, b)}")
elif operation == "/":
print(f"Result: {divide(a, b)}")
else:
print("Invalid operation.")
OUTPUT:-
Enter first number: 10
Enter second number: 5
Enter operation (+, -, *, /): *
Result: 50.0
25
16. Program to Count Words in a String
Problem Statement: Write a Python
program to count the number of words in
a string.
CODE:-
def count_words(s):
words = s.split()
return len(words)
string = input("Enter a string: ")
print(f"Number of words:
{count_words(string)}")
OUTPUT:-
Enter a string: Hello World from Python
Number of words: 4
26
17. Program to Print Multiplication Table
Problem Statement: Write a Python
program to print the multiplication table of
a given number
CODE:-
def multiplication_table(num):
for i in range(1, 11):
print(f"{num} x {i} = {num * i}")
num = int(input("Enter a number: "))
multiplication_table(num)
27
OUTPUT:-
Enter a number: 5
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
28
18. Program to Find the Length of a List
Problem Statement: Write a Python
program to find the length of a list.
CODE:-
def list_length(arr):
return len(arr)
arr = [10, 20, 30, 40, 50]
print(f"Length of the list: {list_length(arr)}")
OUTPUT:-
Length of the list: 5
29
19. Program to Check if a Year is a Leap
Year
Problem Statement: Write a Python
program to check if a year is a leap year.
CODE:-
def is_leap_year(year):
if (year % 4 == 0 and year % 100 != 0)
or (year % 400 == 0):
return True
else:
return False
year = int(input("Enter a year: "))
if is_leap_year(year):
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")
30
OUTPUT:-
Enter a year: 2020
2020 is a leap year.
31
20. Program to Count Lines, Words, and
Characters in a Text File
Problem Statement:
Write a Python program that takes a file
name as input and then counts the
number of lines, words, and characters in
that file.
CODE:-
def count_file_contents(filename):
try:
with open(filename, 'r') as file:
lines = file.readlines()
num_lines = len(lines)
num_words = sum(len(line.split())
for line in lines)
num_chars = sum(len(line) for line
in lines)
32
return num_lines, num_words,
num_chars
except FileNotFoundError:
return "File not found!"
# Input file name
filename = input("Enter the file name: ")
lines, words, chars =
count_file_contents(filename)
if isinstance(lines, int): # To check if the
file was found and processed
print(f"Lines: {lines}")
print(f"Words: {words}")
print(f"Characters: {chars}")
else:
print(lines) # This will print "File not
found!" in case of error
OUTPUT:-
33
OUTPUT:-
Sample File (example.txt):
Hello, this is a sample file.
It contains multiple lines and words.
Python makes file operations easy
Program Execution:-
Enter the file name: example.txt
Lines: 10
Words: 50
Characters: 300