Q.1 Write a program to create a simple calculator.
Program to create a simple calculator
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
operator = input("Enter operation (+, -, *, /): ")
if operator == '+':
print("Result: ",num1 + num2)
elif operator == '-':
print(f"Result: {num1 - num2}")
elif operator == '*':
print(f"Result: {num1 * num2}")
elif operator == '/':
print(f"Result: {num1 / num2}")
else:
print("Invalid operator")
Q.2 Write a program to calculate the factorial of a number.
num = int(input("Enter a number: "))
factorial = 1
for i in range(1, num + 1):
factorial = factorial* i
print(f"Factorial of {num} is {factorial}")
Q.3 Write a program to calculate the sum of first n natural numbers.
num = int(input("How many natural numbers do you want to add? "))
sum = 0
for i in range(num+1):
sum+=i
print(sum)
Q.4 Write a program to calculate the simple interest.
Program to calculate simple interest
P = float(input("Enter the principal amount: "))
R = float(input("Enter the rate of interest: "))
T = float(input("Enter the time (in years): "))
SI = (P * R * T) / 100
print(“Simple Interest is: SI")
Q. 5 Write a program to swap two numbers with and without using a third
variable.
# Program to swap two numbers without using a third variable
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c=a
a=b
b=c
print(f"After swapping, first number: {a}, second number: {b}")
# Program to swap two numbers without using a third variable
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
a, b = b, a
print(f"After swapping, first number: {a}, second number: {b}")
Q6. Write a program to check the largest of the 3 numbers
Program
a=int(input("enter a number A"))
b=int(input("enter a number B"))
c=int(input("enter a number C"))
if(a>=b) and (a>=c):
largest=a
elif(b>=a) and (b>=c):
largest=b
else:
largest=c
print('the largest number is',largest)
Q.7 Write a program to generate the first n terms of Fibonacci series
0,1,1,2,3,5,8,13,21…. .
n= input(“How many terms to be printed?”)
a=0
b=1
print(a , end=" ")
print(b , end=" ")
# first two terms already printed. So Start with 3rd term
for i in range(3,n+1):
c=a+b
print(c , end=" ")
a=b
b=c
Q.8 Write a program to check whether its a prime number
a=int(input('enter a number A:'))
if a > 1:
for i in range(2, (a//2)+1):
if (a % i) == 0:
print(a, "is not a prime number")
break
else:
print(a, "is a prime number")
else:
print(a, "is not a prime number")
Q.9 WAP to Input the elements of a list from the user and find the maximum
value stored in a list
a = []
# Get the number of elements
n = int(input("Enter the number of elements: "))
# Append elements to the list
for i in range(n):
element = int(input("Enter element : "))
[Link](element)
print("List:", a)
# Assuming first element is largest.
largest = a[0]
# Iterate through list and find largest
for val in a:
if val > largest:
# If current element is greater than largest
# update it
largest = val
print(“the largest number is:” largest)
Q.10 Write a program to search a value in the list.
lst=[12,4,10,9,7]
length=len(lst)
element=int(input(“Enter element to be searched:”))
for i in range(0,length):
if element= =lst[i]:
print(element,”found at index”,i)
break
else:
print(element,”not found in given list”)
Q.11 Write a program to add the elements of two list.
list1 = [12, 9, 4, 7, 8]
list2 = [3, 5, 6, 12, 10]
new_list = []
for i in range(len(list1)):
new_list.append(list1[i] + list2[i])
# printing resultant list
print ("Resultant list is:", new_list)
[Link] a program to calculate mean ,median , mode using NUMPY.
import numpy as np
a = [1,2,2,4,5,6]
print([Link](a))
import numpy as np
a = [1,2,2,4,5,6]
print([Link](a))
import numpy as np
a = [1,2,2,2,4,5,6,6]
values,counts = [Link](a, return_counts=True)
# unique method provides the unique values and their count from array .
# [Link](a) can also be used .
mode = values[[Link](counts)]
print(mode)
Q.13 Create a 2 Dimensional array and perform following tasks.
a. Multiply each element of array by 2.
b. Display the maximum value from the array.
c. Display row and column wise maximum value in the array.
d. Display the sum of all elements of the array.
e. Display the shape (rows,columns) and size of array.
import numpy as np
arr= [Link]([[2,5,4,6],[3,8,4,5]])
new=(arr*2)
print([Link]())
print([Link](axis=1)) # 1 is x axis
print([Link](axis=0)) # 0 is y axis
print([Link])
print([Link])
Q.14 Write a program to display a line chart with labeled x axis and y axis.
Also use proper chart title.
import [Link] as plt
x=[1,2,3]
y=[5,7,4]
[Link](x,y,label="(x,y)",marker="*")
[Link]("numbers")
[Link]("values")
[Link]("LINE CHART")
[Link]()
Q.15 Display an orange coloured bar graph showing the choice of favourite
book among the readers. Use appropriate chart title and labels.
import [Link] as plt
Books=["Python","c++","Java","Perl"]
popularity=[20,18,14,16]
[Link](x,y,color="orange")
[Link]("Books")
[Link]("popularity (No. of readers)")
[Link]("FAVOURITE BOOK")
[Link]()
Q.16 Write a program in python to display image using opencv.
import cv2
import [Link] as plt
import numpy as np
img = [Link]('C:\Users\jsspsn\Desktop\[Link]')
[Link](img)
[Link]('SCHOOL')
[Link]('off')
[Link]()