0% found this document useful (0 votes)
2 views47 pages

Computer Practical

The document contains a collection of Python programs that perform various mathematical and logical operations, including calculating sums, areas, volumes, averages, and conversions. It also includes functions for checking properties of numbers, generating patterns, and determining eligibility for voting. Each program is structured with user input prompts and outputs the results accordingly.

Uploaded by

Mansi Malhotra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views47 pages

Computer Practical

The document contains a collection of Python programs that perform various mathematical and logical operations, including calculating sums, areas, volumes, averages, and conversions. It also includes functions for checking properties of numbers, generating patterns, and determining eligibility for voting. Each program is structured with user input prompts and outputs the results accordingly.

Uploaded by

Mansi Malhotra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 47

1. Sum of 2 numbers.

#to find the sum of two numbers


a=int(input("Enter First number : "))
b=int(input("Enter Second number : "))
sum=a+b
print(" The sum of two numbers entered is ", sum)

Pooja’s Study Material Contact: 9871160650


1
2. Area of Rectangle.

#to find the area of a rectangle


l=int(input("Enter Length of the Rectangle : "))
b=int(input("Enter Breadth of the Rectangle : "))
area=l*b
print(" The area of the rectangle is ", area)

3. Volume of Cuboid.
Pooja’s Study Material Contact: 9871160650
2
#to find the volume of a cuboid
l=int(input("Enter Length of the cuboid : "))
b=int(input("Enter Breadth of the cuboid : "))
h=int(input(" Enter height of the cuboid: "))
Volume=l*b*h
print(" The volume of the cuboid is ", Volume)

4. Average marks in Five subjects.


#to find the average marks in five subjects
Pooja’s Study Material Contact: 9871160650
3
s1=int(input("Enter the first number : "))
s2=int(input("Enter the second number : "))
s3=int(input(" Enter the third number: "))
s4=int(input(" Enter the fourth number: "))
s5=int(input(" Enter the fifth number: "))
average= (s1+s2+s3+s4+s5) / 5
print(" The average marks in five subjects is ", average)

5. Area of a Triangle
#to find the area of a triangle
b=int(input("Enter the base of the triangle : "))
h=int(input("Enter the height of the triangle : "))
Pooja’s Study Material Contact: 9871160650
4
area= (1/2) *b * h
print(" The area of a triangle is ", area)

6. Simple Interest
#to find the simple interest
p=int(input("Enter the Principal : "))
t=int(input("Enter the time in years : "))
r=int(input("Enter the rate of interest per annum : "))
Pooja’s Study Material Contact: 9871160650
5
simpleinterest= (p*t*r)/100
print(" The simple interest is ", simpleinterest)

7. Celsius to Fahrenheit Temperature


#to convert the temperature from Celsius to Fahrenheit
c=int(input("Enter the tempertaure in celsius : "))
tempF= (c*(9/5))+32
print(" The tempertaure in Fahrenheit is ", tempF)

Pooja’s Study Material Contact: 9871160650


6
8. to print the sum of the squares of first n natural numbers

# to find the sum of the squares of first n natural numbers


def SumofSquares(n):
s=0
for i in range(n+1):
s+=i**2

Pooja’s Study Material Contact: 9871160650


7
return s

#input
n=int(input("enter n: "))
print("sum of squares of first {} natural numbers: ".format(n),SumofSquares(n))

9. Print the table of a number

1. number = int(input ("Enter the number of which the user wants to print the multi
plication table: "))
2. # We are using "for loop" to iterate the multiplication 10 times

Pooja’s Study Material Contact: 9871160650


8
3. print ("The Multiplication Table of: ", number)
4. for count in range(1, 11):
5. print (number, 'x', count, '=', number * count)

10.To print even numbers from 10 to 25

#to print even numbers from 10 to 25


for i in range(10,25):
if i % 2 == 0:
print(i, end=",")

Pooja’s Study Material Contact: 9871160650


9
11. Print the Fibonacci series

# Program to display the Fibonacci sequence up to n-th term

nterms = int(input("How many terms? "))

# first two terms


n1, n2 = 0, 1
count = 0

# check if the number of terms is valid


if nterms <= 0:
print("Please enter a positive integer")
Pooja’s Study Material Contact: 9871160650
10
# if there is only one term, return n1
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1

Pooja’s Study Material Contact: 9871160650


11
12. Print the Factorial of a number

# Python program to find the factorial of a number provided by the user.


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)

Pooja’s Study Material Contact: 9871160650


12
13. Print the sum of the series:
1+x+x2/2+x3/3+x4/4…. Upto n terms.

# Python program to find the sum of a series


n=int(input("Enter the number of terms:"))
x=int(input("Enter the value of x:"))
sum1=1
for i in range(2,n+1):
sum1=sum1+((x**i)/i)
print("The sum of series is",round(sum1,2))

Pooja’s Study Material Contact: 9871160650


13
14.Print the pattern:
A
B B
C C C
D D D D
E E E E E
….. up to n lines….

# Python program to print an alphabetical series in triangle


n=int(input("Enter the number of rows: "))
for i in range(1,n+1):
print((str(chr(64+i)+" "))*(2*i-1))

Pooja’s Study Material Contact: 9871160650


14
15.Print the pattern:
1 2 3 4 5 ..upto n lines..
1 2 3 4
1 2 3
1 2
1
A

# Python program to print a numerical pattern in inverted triangle


rows = int(input("Enter number of rows: "))

for i in range(rows, 0, -1):


for j in range(1, i+1):
print(j, end=" ")
Pooja’s Study Material Contact: 9871160650
15
print()

16.Check whether the number is a Palindrome or not .

# Python program to check if a number is in palindrome


n=int(input("Enter number:"))
temp=n
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
if(temp==rev):
print("The number is a palindrome!")
else:
print("The number isn't a palindrome!")

Pooja’s Study Material Contact: 9871160650


16
17.Check whether the number is prime number or not .

# Python program to check if a number is prime or notdef is_prime(num):


def is_prime_using_while(num):
# Prime numbers are greater than 1
if num <= 1:
return False

# Start checking from 2, since 1 is not a valid divisor for prime checking
i=2

# Loop until i is less than or equal to the square root of num


# This is because a larger factor must be a multiple of a smaller factor
while i * i <= num:
# If num is divisible by i, it is not a prime number
Pooja’s Study Material Contact: 9871160650
17
if num % i == 0:
return False
i += 1 # Increment i by 1 for the next iteration

# If no divisors are found, num is a prime number


return True

# Example usage
number = 67
if is_prime_using_while(number):
print(f"{number} is a prime number")
else:
print(f"{number} is not a prime number")
print(f"{num} is not a prime number")

Pooja’s Study Material Contact: 9871160650


18
18.Print the sum of the series:
1-x+x2/2-x3/3+x4/4…. Upto n terms

x = int(input("Enter the value of x: "))


n = int(input("Enter the value of n: "))

sum = 0

for i in range(1, n + 1) :
term = x ** i / i
sum += term

print("Sum =", sum)

Pooja’s Study Material Contact: 9871160650


19
19.Area of a triangle

1. # Three sides of the triangle is a, b and c:


2. a = float(input('Enter first side: '))
3. b = float(input('Enter second side: '))
4. c = float(input('Enter third side: '))
5.
6. # calculate the semi-perimeter
7. s = (a + b + c) / 2
8.
9. # calculate the area
10.area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
11.print('The area of the triangle is %0.2f' %area)

Pooja’s Study Material Contact: 9871160650


20
20.Check the Eligibility to vote

def voter_eligibility(age):
return age >= 18

def main():
try:
age = int(input("Input your age: "))

if age < 0:
print("Age cannot be negative.")
elif voter_eligibility(age):
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")
except ValueError:
print("Invalid input. Please input a valid age.")

if __name__ == "__main__":
main()

Pooja’s Study Material Contact: 9871160650


21
Pooja’s Study Material Contact: 9871160650
22
21.Check the number is Odd number or even number

# Python program to check if the input number is odd or even.


# A number is even if division by 2 gives a remainder of 0.
# If the remainder is 1, it is an odd number.

num = int(input("Enter a number: "))


if (num % 2) == 0:
print("{0} is Even".format(num))
else:
print("{0} is Odd".format(num))

Pooja’s Study Material Contact: 9871160650


23
22.Check the number is Positive or negative number
num = float(input("Enter a number: "))
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")

23.Check the student is Pass or fail


Pooja’s Study Material Contact: 9871160650
24
print('Enter marks obtained in 3 subjects')
s1=int(input())
s2=int(input())
s3=int(input())
avg=(s1+s2+s3)/3

if s1>40 and s2>40:


print('Pass')
elif s2>40 and s3>40:
print('Pass')
elif s3>40 and s1>40:
print('Pass')
elif avg>40:
print('Pass')
else:
print('Fail')

24, Print the greater of two numbers

Pooja’s Study Material Contact: 9871160650


25
def FindLargOfTwo(x, y):
if x>y:
return 1
elif x<y:
return 2

print("Enter Two Numbers: ", end="")


numOne = int(input())
numTwo = int(input())

res = FindLargOfTwo(numOne, numTwo)


if res==1:
print("\nLargest Number =", numOne)
elif res==2:
print("\nLargest Number =", numTwo)
else:
print("\nBoth Numbers are Equal")

25.Print the Grade based on the percentage

Pooja’s Study Material Contact: 9871160650


26
print("Enter Marks Obtained in 5 Subjects: ")
markOne = int(input())
markTwo = int(input())
markThree = int(input())
markFour = int(input())
markFive = int(input())

tot = markOne+markTwo+markThree+markFour+markFive
avg = tot/5

if avg>=91 and avg<=100:


print("Your Grade is A1")
elif avg>=81 and avg<91:
print("Your Grade is A2")
elif avg>=71 and avg<81:
print("Your Grade is B1")
elif avg>=61 and avg<71:
print("Your Grade is B2")
elif avg>=51 and avg<61:
print("Your Grade is C1")
elif avg>=41 and avg<51:
print("Your Grade is C2")
elif avg>=33 and avg<41:
print("Your Grade is D")
elif avg>=21 and avg<33:
print("Your Grade is E1")
elif avg>=0 and avg<21:
print("Your Grade is E2")
else:
print("Invalid Input!")

Pooja’s Study Material Contact: 9871160650


27
26.Perform the operations of an Arithmetic Calculator
# This function adds two numbers
def add(x, y):

Pooja’s Study Material Contact: 9871160650


28
return x + y

# This function subtracts two numbers


def subtract(x, y):
return x - y

# This function multiplies two numbers


def multiply(x, y):
return x * y

# This function divides two numbers


def divide(x, y):
return x / y

print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")

while True:
# take input from the user
choice = input("Enter choice(1/2/3/4): ")

# check if choice is one of the four options


if choice in ('1', '2', '3', '4'):
try:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
except ValueError:
print("Invalid input. Please enter a number.")
Pooja’s Study Material Contact: 9871160650
29
continue

if choice == '1':
print(num1, "+", num2, "=", add(num1, num2))

elif choice == '2':


print(num1, "-", num2, "=", subtract(num1, num2))

elif choice == '3':


print(num1, "*", num2, "=", multiply(num1, num2))

elif choice == '4':


print(num1, "/", num2, "=", divide(num1, num2))

# check if user wants another calculation


# break the while loop if answer is no
next_calculation = input("Let's do next calculation? (yes/no): ")
if next_calculation == "no":
break
else:
print("Invalid Input")

Pooja’s Study Material Contact: 9871160650


30
Pooja’s Study Material Contact: 9871160650
31
27. Print the greater of three numbers
Pooja’s Study Material Contact: 9871160650
32
# Python program to find the largest number among the three input numbers

# change the values of num1, num2 and num3


# for a different result
num1 = 10
num2 = 14
num3 = 12

# uncomment following lines to take three numbers from user


#num1 = float(input("Enter first number: "))
#num2 = float(input("Enter second number: "))
#num3 = float(input("Enter third number: "))

if (num1 >= num2) and (num1 >= num3):


largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3

print("The largest number is", largest)

Pooja’s Study Material Contact: 9871160650


33
28.Check the character entered is Vowel or Consonant
c = input(" enter a letter ")

Pooja’s Study Material Contact: 9871160650


34
# checking for vowels
if c == 'a' or c == 'e' or c == 'i' or c == 'o' or c == 'u' or c == 'A' or c == 'E' or c == 'I'
or c == 'O' or c == 'U':
print(c, "is a vowel") # condition true input is vowel
else:
print(c, "is a consonant") # condition true input is consonant

29.Check whether the year entered is Leap year or not

1. # Default function to implement conditions to check leap year


Pooja’s Study Material Contact: 9871160650
35
2. def CheckLeap(Year):
3. # Checking if the given year is leap year
4. if((Year % 400 == 0) or
5. (Year % 100 != 0) and
6. (Year % 4 == 0)):
7. print("Given Year is a leap Year");
8. # Else it is not a leap year
9. else:
10. print ("Given Year is not a leap Year")
11.# Taking an input year from user
12.Year = int(input("Enter the number: "))
13.# Printing result
14.CheckLeap(Year)

30.Area of Circle

Pooja’s Study Material Contact: 9871160650


36
#to find the area of a circle
def area_of_circle(r):
pi=3.147
area= pi*r*r
return area

radius=float(input("Enter Radius: "))


print("Area: ",area_of_circle(radius))

31.Area of Rectangle

# to find the area of a rectangle


print("Enter Length of Rectangle: ")
l = float(input())
print("Enter Breadth of Rectangle: ")
Pooja’s Study Material Contact: 9871160650
37
b = float(input())
a = l*b
print("\nArea = ", a)

32.Circumference of Circle

def findCircum(rad):
return 2 * 3.14 * rad

print("Enter Radius of Circle: ", end="")


r = float(input())

Pooja’s Study Material Contact: 9871160650


38
c = findCircum(r)
print("\nCircumference = {:.2f}".format(c))

33.Perimeter of Rectangle

def area(a, b):


return (a * b)

def perimeter(a, b):


Pooja’s Study Material Contact: 9871160650
39
return (2 * (a + b))

l = float(input("Enter length: "))


b = float(input("Enter breadth: "))
print ("Area = ", area(l, b))
print ("Perimeter = ", perimeter(l, b))

34. Print the table of a number

n=int(input("Enter the number to print the tables for:"))


for i in range(1,11):
print(n,"x",i,"=",n*i)

Pooja’s Study Material Contact: 9871160650


40
35. Print even numbers from 10 to 25.

# Python program to print all even numbers in range


for even_numbers in range(10,25,2):
#here inside range function first no denotes starting,
#second denotes end and
#third denotes the interval
print(even_numbers,end=' ')

Pooja’s Study Material Contact: 9871160650


41
36. Print the Fibonacci series

n = int(input("Enter the no of terms"))


num1 = 0
num2 = 1
next_number = num2
count = 1

while count <= n:


print(next_number, end=" ")
count += 1
num1, num2 = num2, next_number
next_number = num1 + num2
Pooja’s Study Material Contact: 9871160650
42
print()

37.Print the Factorial of a number


# Python program to find the factorial of a number provided by the user
# using recursion

def factorial(x):
"""This is a recursive function
to find the factorial of an integer"""

if x == 1 or x == 0:

Pooja’s Study Material Contact: 9871160650


43
return 1
else:
# recursive call to the function
return (x * factorial(x-1))

# change the value for a different result


num = 7

# to take input from the user


# num = int(input("Enter a number: "))

# call the factorial function


result = factorial(num)
print("The factorial of", num, "is", result)

Pooja’s Study Material Contact: 9871160650


44
38.Check whether the number is a Palindrome or not

def is_palindrome(n, temp, rev=0):


if n == 0:
if temp == rev:
return "The number is a palindrome!"
else:
return "The number isn't a palindrome!"
else:
dig = n % 10
rev = rev * 10 + dig
n = n // 10
return is_palindrome(n, temp, rev)

n = int(input("Enter number:"))
result = is_palindrome(n, n)
print(result)

Pooja’s Study Material Contact: 9871160650


45
39. Check whether the number is prime number or not

num = int(input("Enter a No "))


# Negative numbers, 0 and 1 are not primes
if num > 1:

# Iterate from 2 to n // 2
for i in range(2, (num//2)+1):

# If num is divisible by any number between


# 2 and n / 2, it is not prime
if (num % i) == 0:
print(num, "is not a prime number")
break
else:
print(num, "is a prime number")
else:
print(num, "is not a prime number")

Pooja’s Study Material Contact: 9871160650


46
Pooja’s Study Material Contact: 9871160650
47

You might also like