Cs practicall file
Cs practicall file
INDEX
No. Program Title
1. Program to Find Factorial of a Number
Code:-
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
Output:-
5
Enter a number: 5
Factorial of 5 is 120
6
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
Output:-
Enter a number: 7
7 is prime.
8
CODE:-
def fibonacci(n):
sequence = [0, 1]
for i in range(2, n):
sequence.append(sequence[-1] +
sequence[-2])
return sequence
print(fibonacci(n))
9
OUTPUT:-
CODE:-
def reverse_string(s):
return s[::-1]
print(f"Reversed string:
{reverse_string(string)}")
OUTPUT:-
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:-
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:-
CODE:-
OUTPUT:-
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
CODE:-
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
target = int(input("Enter a number to
search: "))
17
OUTPUT:-
CODE:-
def sum_of_list(arr):
return sum(arr)
OUTPUT:-
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
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:-
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
CODE:-
def celsius_to_fahrenheit(celsius):
return (celsius * 9/5) + 32
OUTPUT:-
CODE:-
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:-
CODE:-
def count_words(s):
words = s.split()
return len(words)
OUTPUT:-
CODE:-
def multiplication_table(num):
for i in range(1, 11):
print(f"{num} x {i} = {num * i}")
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
CODE:-
def list_length(arr):
return len(arr)
OUTPUT:-
CODE:-
def is_leap_year(year):
if (year % 4 == 0 and year % 100 != 0)
or (year % 400 == 0):
return True
else:
return False
OUTPUT:-
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
OUTPUT:-
33
OUTPUT:-
Program Execution:-