1
#program in python to convert the the temperature from Fahrenheit
to Celsius.
#Name: Anand Thakur Roll no: 241612044013
def fahrenheit_to_celsius(fahrenheit):
celsius = (fahrenheit - 32) * 5 / 9
print("Temperature in Celsius:", round(celsius, 3))
# Example usage
fahrenheit = 40
fahrenheit_to_celsius(fahrenheit)
________________________________________________________
output:
Temperature in Celsius: 4.444
2
#program in python to convert the the weight kilogram into pound
#Name: Anand Thakur Roll no: 241612044013
kg = int(input("Enter weight in KG: "))
p = 2.2*kg
print("Answer in Pounds is ", p)
________________________________________________________
output:
Enter weight in KG: 45
Answer in pound is 99
3
#program in python to find largest of three numbers.
#Name: Anand Thakur Roll no: 241612044013
def find_largest(n1, n2, n3):
return max(n1, n2, n3)
#Example usage of the function
largest = find_largest(5, 14, 9)
print("The largest_number is:", largest)
________________________________________________________
output:
The largest number is: 14
4
#program in python to check whether the given year is leap year or
not.
#Name: Anand Thakur Roll no: 241612044013
while True:
year = int(input("Enter a year: "))
if (year % 400 == 0) and (year % 100 == 0):
print(year,"is a leap year")
elif (year % 4 ==0) and (year % 100 != 0):
print(year,"is a leap year")
# if not divided by both 400 (century year) and 4 (not century year)
# year is not leap year
else:
print(year,"is not a leap year")
________________________________________________________
output:
Enter a year: 456
456 is a leap year
Enter a year: 6747
6747 is not a leap year
Enter a year: 4544
4544 is a leap year
Enter a year:
5
#program in python to create a function to find sum of digit of
number.
#Name: Anand Thakur Roll no: 241612044013
while True:
def sum_of_digit(n):
if n< 10:
return n
else:
return n%10 + n/10
# Read number
number = int(input("Enter number: "))
digit_sum = sum_of_digit(number)
print("Sum of digit of number’’,number,’’is’’,digit_sum)
________________________________________________________
Output
Enter number: 46
Sum of digit of number 46 is 10.
Enter number: 7
Sum of digit of number 7 is 7.
6
#program in python to reverse the number.
#Name: Anand Thakur Roll no: 241612044013
num = 1234
reversed_num = 0
while num != 0:
digit = num % 10
reversed_num = reversed_num * 10 + digit
num //= 10
print("Reversed Number: " ,reversed_num)
________________________________________________________
output:
Reversed Number: 543
7
#program in python to print the day of week week using match case
statement
#Name: Anand Thakur Roll no: 241612044013
def print_day_of_week(day_number):
match day_number:
case 1:
print("Monday")
case 2:
print("Tuesday")
case 3:
print("Wednesday")
case 4:
print("Thursday")
case 5:
print("Friday")
case 6:
print("Saturday")
case 7:
print("Sunday")
case _:
print("Invalid day number")
# Example usage
day = int(input("Enter a number between 1 and 7: "))
print_day_of_week(day)
________________________________________________________
output:
Enter a number between 1 and 7: 3
Wednesday
8
#program in python to find sum of n natural numbers.
#Name: Anand Thakur Roll no:241612044013
num = int(input('Enter number : '))
if num < 0:
print("Enter a positive number")
else:
sum = 0
# use while loop to iterate until zero
while(num > 0):
sum += num
num -= 1
print("The sum is", sum)
________________________________________________________
output:
Enter number : 56675
The sum is 1606056150
9
#program in python to find factorial of a number using recursion
#Name: Anand Thakur Roll no: 241612044013
def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)
num = int(input('Enter the number : '))
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of", num, "is", recur_factorial(num))
________________________________________________________
output:
Enter the number : 6
The factorial of 6 is 720
10
# Program to check if a number is prime or not.
#Name: Anand Thakur Roll no:241612044013
num = int(input("Enter a number: "))
if num == 0 or num == 1:
print(num, "is not a prime number")
elif num > 1:
# check for factors
for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
print(i,"times",num//i,"is",num)
break
else:
print(num,"is a prime number")
# if input number is less than or equal to 1, it is not prime
else:
print(num,"is not a prime number")
________________________________________________________
output:
Enter a number: 54
54 is not a prime number
11
# Program to print first N prime number.
#Name: Anand Thakur Roll no:241612044013
def isPrime(n):
if(n==1 or n==0): return False
#Run a loop from 2 to n-1
for i in range(2,n):
#if the number is divisible by i, then n is not a prime number.
if(n%i==0): return False
#otherwise, n is prime number.
return True
N =int(input('Enter number: ')) #Drive code
#check for every number from 1 to N
for i in range(1,N+1):
#check if current number is prime
if(isPrime(i)):
print(i,end=" ")
________________________________________________________
output:
Enter number: 44
2 3 5 7 11 13 17 19 23 29 31 37 41 43
12
# Program in python that defines functions to find the maximum and
minimum numbers in a list.
#Name: Anand Thakur Roll no:241612044013
def find_maximum(numbers):
if not numbers: return None
maximum = numbers[0]
for number in numbers:
if number > maximum:
maximum = number
return maximum
def find_minimum(numbers):
if not numbers: return None
minimum = numbers[0]
for number in numbers:
if number < minimum:
minimum = number
return minimum
if __name__ == "__main__":
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
maximum = find_maximum(numbers)
minimum = find_minimum(numbers)
print(f"Maximum number: {maximum}")
print(f"Minimum number: {minimum}")
________________________________________________________
output:
Maximum number: 9
Minimum number: 1
13
#a program in Python to find the sum of digits of a number
#Name: Anand Thakur Roll no: 241612044013
n = 12345
sum = 0
while n > 0:
sum += n % 10 # extract last digit
n //= 10 # remove last digit
print('sum of digit of number is ' , sum)
________________________________________________________
output:
sum of digit of number is 15
14
#program in python to create a file, enter the roll no, and name into
the file, read the file and print the records of the file.
#Name: Anand Thakur Roll no:241612044013
# Step 1: Create a file and write data to it
def write_to_file(filename):
with open(filename, 'w') as file:
n = int(input("Enter number of students: "))
for i in range(n):
roll_no = input(f"Enter roll number for student {i+1}: ")
name = input(f"Enter name for student {i+1}: ")
file.write(f"{roll_no},{name}\n")
print("Data written to file successfully.\n")
# Step 2: Read the file and display its contents
def read_from_file(filename):
print("Reading records from file:")
with open(filename, 'r') as file:
for line in file:
roll_no, name = line.strip().split(',')
print(f"Roll No: {roll_no}, Name: {name}")
# Main program
filename = "students.txt"
write_to_file(filename)
read_from_file(filename)
________________________________________________________
output:
Enter number of students: 2
15
Enter roll number for student 1: 101
Enter name for student 1: Alice
Enter roll number for student 2: 102
Enter name for student 2: Bob
Reading records from file:
Roll No: 101, Name: Alice
Roll No: 102, Name: Bob
16
#program in python to enter a string, to find the length of entered
string and to reverse the entered string.
#Name: Anand Thakur Roll no: 241612044013
user_ string = input("Enter a string: ")
length = len(user_string) # Find the length of the string
reversed_string = user_string[::-1] #Reverse the string
#Display the results
print(f"Length of the entered string: {length}")
print(f"Reversed string: {reversed_string}")
________________________________________________________
output:
Enter a string: Python
Length of the entered string: 6
Reversed string: nohtyP
17
#program to create a function to reverse the digits of a number
#Name: Anand Thakur Roll no:241612044013
def reverse_number(n):
if n == 0:
return 0
else:
return (n % 10) * 10 ** (len(str(n)) - 1) + reverse_number(n //
10)
num = 987654
orig_num = num
rev_num = reverse_number(num)
print(orig_num)
print(rev_num)
________________________________________________________
output:
987654
456789
18
#program in Python to find the factorial of a number
#Name: Anand Thakur Roll no:241612044013
num = int(input("Enter a number: "))
factorial = 1
# check if the number is negative, positive or zero
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)
________________________________________________________
output:
Enter a number: 4
The factorial of 4 is 24
19
#program in Python to create a function to checks whether the given
number is prime or not.
#Name: Anand Thakur Roll no:241612044013
def is_prime(number):
if number <= 1:
return False
for i in range(2, int(number**0.5) + 1):
if number % i == 0:
return False
return True
# Example usage
num =int(input('enter the number : '))
if is_prime(num):
print(f"{num} is a prime number")
else:
print(f"{num} is not a prime number")
________________________________________________________
output:
enter the number : 997
997 a prime number
20
#a program in python to print Python to create a class and attribute:
#Name: Anand Thakur Roll no:241612044013
class Student:
# Class attribute
branch = "Computer Science"
def __init__(self):
# Instance attributes
self.name = ""
self.roll_no = ""
def input_student(self): # Method to input student details
self.name = input("Enter student name: ")
self.roll_no = input("Enter roll number: ")
def display_student(self): # Method to display student details
print(f"Name: {self.name}")
print(f"Roll No: {self.roll_no}")
print(f"Branch: {Student.branch}")
student1 = Student() # Example usage
student1.input_student()
student1.display_student()
________________________________________________________
output:
Enter student name: Abhishek
Enter roll number: 101
Name: Abhishek
Roll No: 101
Branch: Computer Science
21
#a program to implement inheritance by defining a parent class and 2
child classes both inheriting the attributes of parent class.
#Name: Anand Thakur Roll no:241612044013
# Parent class
class Person:
def _init_(self, name, age):
self.name = name
self.age = age
def display_person(self):
print(f"Name: {self.name}")
print(f"Age: {self.age}")
class Student(Person): # First child class
def _init_(self, name, age, student_id):
super()._init_(name, age) # Call parent constructor
self.student_id = student_id
def display_student(self):
self.display_person()
print(f"Student ID: {self.student_id}")
class Teacher(Person): # Second child class
def _init_(self, name, age, subject):
super()._init_(name, age) # Call parent constructor
self.subject = subject
def display_teacher(self):
self.display_person()
print(f"Subject: {self.subject}")
22
student1 = Student("Abhishek", 20, "S101") # Example usage
teacher1 = Teacher("Mr. Sharma", 40, "Mathematics")
print("\nStudent Details:")
student1.display_student()
print("\nTeacher Details:")
teacher1.display_teacher()
________________________________________________________
output:
Abhishek 20 S1