0% found this document useful (0 votes)
44 views43 pages

Computer Science Practical Programs

The document contains a series of programming exercises and their solutions, primarily focused on basic Python programming concepts such as input/output, arithmetic operations, and conditional statements. Each exercise includes a problem statement, source code, and expected output. The exercises cover a range of topics, including displaying information, performing calculations, and using selection statements.

Uploaded by

atharvjain1308
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)
44 views43 pages

Computer Science Practical Programs

The document contains a series of programming exercises and their solutions, primarily focused on basic Python programming concepts such as input/output, arithmetic operations, and conditional statements. Each exercise includes a problem statement, source code, and expected output. The exercises cover a range of topics, including displaying information, performing calculations, and using selection statements.

Uploaded by

atharvjain1308
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

COMPUTER

SCIENCE
PRACTICAL FILE
|Atharv Jain |
|Roll no. 11 |
|XI-B |
SEQUENCE

Q1. Write program to display “Hello”.

SOURCE CODE

print("Hello")

OUTPUT:

Hello

------------------------------------------------------------------------------------------------------------
Q2. Write program to display your Name, Age, Address and Contact Number on
screen.

SOURCE CODE

N= "Atharv Jain"
A= "16"
AD= "A2251,Green Field Colony"
C= "9311599366"
print("Name= ",N)
print("Age= ",A)
print("Address= ",AD)
print("Contact Number= ",C)

OUTPUT:

Name= Atharv Jain


Age= 16
Address= A2251,Green Field Colony
Contact Number= 9311599366
------------------------------------------------------------------------------------------------------------

[Link] program to display the following:


Program = 2
Logic = 6
Documentation = 2

SOURCE CODE

P="2"

PAGE 1
L="6"
D="2"
print("Program= ",P)
print("Logic= ",L)

OUTPUT:

Program= 2
Logic= 6
Documentation= 2
------------------------------------------------------------------------------------------------------------

[Link] program to display the following:


Name Marks
Raj 76.5
Geeta 85.5
Simmy 93.5

SOURCE CODE

print("Name \t Marks")
print("Raj \t 76.5 \n Geeta \t 85.5 \n Simmy \t 93.5")

OUTPUT:
Name Marks
Raj 76.5
Geeta 85.5
Simmy 93.5
------------------------------------------------------------------------------------------------------------

[Link] program to find and display sum of 2 numbers entered by user

SOURCE CODE

a=int(input("Enter first number= "))


b=int(input("Enter second number= "))
s=a+b
print("Sum of the two numbers= ",s)

OUTPUT:
Enter first number= 2
Enter second number= 3
Sum of the two numbers= 5
------------------------------------------------------------------------------------------------------------

PAGE 2
[Link] a program to find and display total, average and product of 3 numbers
entered by user.

a=int(input("Enter first number= "))


b=int(input("Enter second number= "))
c=int(input("Enter third number= "))
t=a+b+c
average=t/3
p=a*b*c
print("Sum of the three numbers= ",t)
print("Average of the three numbers= ",average)
print("Product of three numbers= ",p)

OUTPUT:

Enter first number= 1


Enter second number= 2
Enter third number= 3
Sum of the three numbers= 6
Average of the three numbers= 2.0
Product of three numbers= 6
------------------------------------------------------------------------------------------------------------

[Link] a program to find and display Area and Perimeter of a square.


Area of Square = Side * Side
Perimeter of Square = 4 * Side

SOURCE CODE

a=int(input("Enter length of side= "))


area=a*a
p=a*4
print("Area of square= ",area)
print("Perimeter of square= ",p)

OUTPUT:

Enter length of side= 2


Area of square= 4
Perimeter of square= 8
------------------------------------------------------------------------------------------------------------

Q8. Write a program to find and display Simple Interest and Compound Interest.

PAGE 3
Simple Interest = (Principle * Rate of Interest * Time Period) / 100
Compound Interest = ((Principle * (1 + Rate of Interest) /100) Time Period) –
Principle Amount

SOURCE CODE

p=int(input("Enter principal amount= "))


r=int(input("Enter rate of interest= "))
t=int(input("Enter time period= "))
SI=p*r*t/100
CI=((p*(1+r)/100)*t)-p
print("Simple Interest= ",SI)
print("Compound Interest= ",CI)

OUTPUT:
Enter principal amount= 8925
Enter rate of interest= 9
Enter time period= 5
Simple Interest= 4016.25
Compound Interest= -4462.5
------------------------------------------------------------------------------------------------------------

[Link] a program to find and display Area, Perimeter and Diagonal of a


Rectangle.
Area = Length X Breadth
Perimeter = 2 (Length + Breadth)
Diagonal= square root of (Length2 + Breadth2)

SOURCE CODE

a=int(input("Enter length= "))


b=int(input("Enter breadth= "))
area=a*b
p=2*(a+b)
d=a**2+b**2
print("Area of rectangle= ",area)
print("Perimeter of rectangle= ",p)
print("Diagonal of rectangle= ",d)

OUTPUT:

Enter length= 5
Enter breadth= 2

PAGE 4
Area of rectangle= 10
Perimeter of rectangle= 14
Diagonal of rectangle= 29
------------------------------------------------------------------------------------------------------------

[Link] a program to find and display Area, Circumference and Diameter of a


Circle.
Area = 3.14 * r * r
Circumference = 2 * 3.14 * r
Diameter= 2 * r

SOURCE CODE

r=int(input("Enter radius= "))


a=3.14*r*r
c=2*3.14*r
d=2*r
print("Circumference= ",c)
print("Area= ",a)
print("Diameter= ",d)

OUTPUT:

Enter radius= 5
Circumference= 31.400000000000002
Area= 78.5
Diameter= 10
------------------------------------------------------------------------------------------------------------

[Link] a program to find and display area of a Scalene Triangle, using Heron
Formula.
S = (side 1 + Side 2 + Side 3)/2
Area = square root of (s(s – side 1)*(s-side 2) * (s – side 3))

SOURCE CODE

s1=int(input("Enter first side= "))


s2=int(input("Enter second side= "))
s3=int(input("Enter third side= "))
s=(s1+s2+s3)/2
a=(s*(s-s1)*(s-s2)*(s-s3))**0.5
print("Area of triangle= ",a)

OUTPUT:

PAGE 5
Enter first side= 6
Enter second side= 5
Enter third side= 4
Area of triangle= 9.921567416492215
------------------------------------------------------------------------------------------------------------

[Link] a program to input time in seconds and then convert it into Hours,
Minutes and Seconds.

SOURCE CODE

T=int(input("Enter time in seconds= "))


h=T/3600
m=T/60
print("Time in minutes= ",m)
print("Time in hours= ",h)

OUTPUT:
Enter time in seconds= 3600
Time in minutes= 60.0
Time in hours= 1.0
------------------------------------------------------------------------------------------------------------

[Link] a program to input money in Rupees and then convert it into paise,
dollars and pounds

SOURCE CODE

M=int(input("Enter money in rupees= "))


p=M*100
d=M/80
pound=M/100
print("Money in paise= ",p)
print("Money in dollars= ",d)
print("Money in pounds= ",pound)

OUTPUT:
Enter money in rupees= 8000
Money in paise= 800000
Money in dollars= 100.0
Money in pounds= 80.0
------------------------------------------------------------------------------------------------------------

PAGE 6
[Link] a program to input two integers, interchange their values and then
display new values.

SOURCE CODE

a=int(input("Enter first integer= "))


b=int(input("Enter second integer= "))
print("First number= ",a)
print("Second number= ",b)
a=a+b
b=a-b
a=a-b
print("Now the numbers are interchanged")
print("So,the new numbers are:")
print("First number= ",a)
print("Second number= ",b)

OUTPUT:

Enter first integer= 3


Enter second integer= 5
First number= 3
Second number= 5
Now the numbers are interchanged
So,the new numbers are:
First number= 5
Second number= 3
------------------------------------------------------------------------------------------------------------

[Link] a program to find total marks and percentage of a student by inputting


Roll No, Name, Marks in English, History, Computer Science, Geography and
Hindi. Maximum Marks of each subject are 25.

SOURCE CODE

roll_no = input("Enter Roll No: ")


name = input("Enter Name: ")
eng = float(input("Marks in English: "))
hist = float(input("Marks in History: "))
comp = float(input("Marks in Computer Science: "))
geo = float(input("Marks in Geography: "))
hindi = float(input("Marks in Hindi: "))
total = eng + hist + comp + geo + hindi

PAGE 7
percentage = (total / 125) * 100
print("Roll No:", roll_no)
print("Name:", name)
print("Total Marks =", total)
print("Percentage =", percentage, "%")
if total<125:
print(percentage)
else:
print("Wrong marks inputted")

OUTPUT: Enter Roll No: 11


Enter Name: Atharv Jain
Marks in English: 22
Marks in History: 23
Marks in Computer Science: 21
Marks in Geography: 24
Marks in Hindi: 19
Roll No: 11
Name: Atharv Jain
Total Marks = 109.0
Percentage = 87.2 %
------------------------------------------------------------------------------------------------------------
[Link] a program to calculate V = u + at

SOURCE CODE

u = float(input("Enter initial velocity (u): "))


a = float(input("Enter acceleration (a): "))
t = float(input("Enter time (t): "))
v=u+a*t
print("Final velocity =", v ,"m/s")

OUTPUT:
Enter initial velocity (u): 5
Enter acceleration (a): 4
Enter time (t): 5
Final velocity = 25.0 m/s
------------------------------------------------------------------------------------------------------------
[Link] a program to find energy = m * g * h

SOURCE CODE

m = float(input("Enter mass (m): "))


g = 9.8
h = float(input("Enter height (h): "))

PAGE 8
energy = m * g * h
print("Energy =", energy)
OUTPUT:
Enter mass (m): 10
Enter height (h): 10
Energy = 980.0
------------------------------------------------------------------------------------------------------------
[Link] a program to find distance = u * t + ½ at2

SOURCE CODE

u = float(input("Enter initial velocity (u): "))


t = float(input("Enter time (t): "))
a = float(input("Enter acceleration (a): "))
distance = u * t + 0.5 * a * t**2
print("Distance =", distance)
OUTPUT:
Enter initial velocity (u): 24
Enter time (t): 5
Enter acceleration (a): 2
Distance = 145.0
------------------------------------------------------------------------------------------------------------
[Link] a program to find sped = distance / time
SOURCE CODE

distance = float(input("Enter distance: "))


time = float(input("Enter time: "))
speed = distance / time
print("Speed =", speed)
OUTPUT:
Enter distance: 12
Enter time: 6
Speed = 2.0
------------------------------------------------------------------------------------------------------------
Q20. Write a program to enter the days and then convert it into years, months and
days. For example, if the
number of days is 500 then it is equal to 1 year, 4 months and 15 days.
SOURCE CODE

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


years = days // 365
months = (days % 365) // 30
days_left = (days % 365) % 30
print(years, "Years", months, "Months", days_left, "Days")

PAGE 9
OUTPUT:
Enter number of days: 1097
3 Years 0 Months 2 Days
------------------------------------------------------------------------------------------------------------
[Link] a program to input temperature in Fahrenheit and the convert it to
Celsius. (C = F – 32/1.8)
SOURCE CODE

F = float(input("Enter temperature in Fahrenheit: "))


C = (F - 32) / 1.8
print("Temperature in Celsius =", C)
OUTPUT:
Enter temperature in Fahrenheit: 98.4
Temperature in Celsius = 36.88888888888889
------------------------------------------------------------------------------------------------------------
SELECTION STATEMENTS

[Link] a program to input a number and then find it is Odd or Even.


SOURCE CODE

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


if num % 2 == 0:
print("Even Number")
else:
print("Odd Number")
OUTPUT:
Enter a number: 33
Odd Number
------------------------------------------------------------------------------------------------------------
[Link] a program to input a number and then find it is Positive Number or not a
Positive Number.
SOURCE CODE

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


if num > 0:
print("Positive Number")
else:
print("Not a Positive Number")
OUTPUT:
Enter a number: 21
Positive Number
------------------------------------------------------------------------------------------------------------

PAGE 10
[Link] a program to input a number and then find it is a 2-digit number or not?
SOURCE CODE

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


if 10 <= abs(num) <= 99:
print("It is a 2-digit number")
else:
print("It is NOT a 2-digit number")
OUTPUT:
Enter a number: 324
It is NOT a 2-digit number
------------------------------------------------------------------------------------------------------------
[Link] a program to input a number and then check whether its last (Unit Place)
digit is 9 or not?
SOURCE CODE

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


if abs(num) % 10 == 9:
print("Last digit is 9")
else:
print("Last digit is NOT 9")
OUTPUT:
Enter a number: 39
Last digit is 9
------------------------------------------------------------------------------------------------------------
[Link] a program to input age of a person and then find he can vote or not?
SOURCE CODE
age = int(input("Enter age: "))
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible to vote")
OUTPUT:
Enter age: 19
Eligible to vote
------------------------------------------------------------------------------------------------------------
[Link] a program to input a number and then find that the entered number is
multiple of 7 and 9 or not?
SOURCE CODE

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


if num % 7 == 0 and num % 9 == 0:
print("Multiple of both 7 and 9")
else:
print("Not a multiple of both")

PAGE 11
OUTPUT:
Enter a number: 63
Multiple of both 7 and 9
------------------------------------------------------------------------------------------------------------
[Link] a program to input 2 different integers and then find and display the
bigger number.
SOURCE CODE

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


b = int(input("Enter second number: "))
if a > b:
print("Bigger number:", a)
else:
print("Bigger number:", b)
OUTPUT:
Enter first number: 2
Enter second number: 3
Bigger number: 3
------------------------------------------------------------------------------------------------------------
[Link] a program to input 2 different integers and then find that the first number
is factor of second
number or not?
SOURCE CODE

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


b = int(input("Enter second number: "))
if b % a == 0:
print(a, "is a factor of", b)
else:
print(a, "is NOT a factor of", b)
OUTPUT:
Enter first number: 4
Enter second number: 32
4 is a factor of 32
------------------------------------------------------------------------------------------------------------
[Link] a program to input a number and then check whether its second last
(ten’s place) digit is 8 or not?
SOURCE CODE

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


tens = abs(num) // 10 % 10
if tens == 8:
print("Ten’s place digit is 8")
else:
print("Ten’s place digit is NOT 8")

PAGE 12
OUTPUT:
Enter a number: 289
Ten’s place digit is 8
------------------------------------------------------------------------------------------------------------
[Link] a program to input 3 different Numbers and then find as well as display
the biggest number.
SOURCE CODE

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


b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
print("Biggest number:", max(a, b, c))
OUTPUT:
Enter first number: 365
Enter second number: 3556
Enter third number: 2223
Biggest number: 3556
------------------------------------------------------------------------------------------------------------
Q11. Write a program to find total marks, percentage, result and grade of a student
by inputting Roll No,
Name, Marks in English, History, Computer Science, Geography and Hindi.
Maximum Marks of each
subject are 25. Passing marks are 10 in each subject. Grade is calculated as
Percentage Grade
91 – 100 A
81 – 90 B
71 – 80 C
61 – 70 D
51 – 60 E
<=50 F
SOURCE CODE

roll_no = input("Roll No: ")


name = input("Name: ")
eng = float(input("English: "))
hist = float(input("History: "))
comp = float(input("Computer Science: "))
geo = float(input("Geography: "))
hindi = float(input("Hindi: "))

total = eng + hist + comp + geo + hindi


percentage = (total / 125) * 100
passed = all(m >= 10 for m in [eng, hist, comp, geo, hindi])

if not passed:

PAGE 13
grade = "F"
else:
if 91 <= percentage <= 100:
grade = "A"
elif 81 <= percentage <= 90:
grade = "B"
elif 71 <= percentage <= 80:
grade = "C"
elif 61 <= percentage <= 70:
grade = "D"
elif 51 <= percentage <= 60:
grade = "E"
else:
grade = "F"

print("\nRoll No:", roll_no)


print("Name:", name)
print("Total Marks:", total)
print("Percentage:", percentage)
print("Result:", "PASS" if passed else "FAIL")
print("Grade:", grade)
OUTPUT:
Roll No: 11
Name: Atharv Jain
English: 21
History: 22
Computer Science: 23
Geography: 23
Hindi: 22

Roll No: 11
Name: Atharv Jain
Total Marks: 111.0
Percentage: 88.8
Result: PASS
Grade: B
------------------------------------------------------------------------------------------------------------
[Link] a program to enter Day Number and then display Day Name.
SOURCE CODE

day_no = int(input("Enter day number (1-7): "))


days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",
"Sunday"]
if 1 <= day_no <= 7:
print("Day:", days[day_no - 1])

PAGE 14
else:
print("Invalid day number")
OUTPUT:
Enter day number (1-7): 6
Day: Saturday
------------------------------------------------------------------------------------------------------------
Q13. Write a program to enter Month Number and then display Month Name.
SOURCE CODE

month_no = int(input("Enter month number (1-12): "))


months = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"]
if 1 <= month_no <= 12:
print("Month:", months[month_no - 1])
else:
print("Invalid month number")
OUTPUT:
Enter month number (1-12): 11
Month: November
------------------------------------------------------------------------------------------------------------
[Link] a program to enter a value and then find that the given value is Capital
letter, Small letter, digit or any other value.
SOURCE CODE

ch = input("Enter a character: ")


if [Link]():
print("Capital letter")
elif [Link]():
print("Small letter")
elif [Link]():
print("Digit")
else:
print("Other character")
OUTPUT:
Enter a character: A
Capital letter
------------------------------------------------------------------------------------------------------------
Q15. Write a program to create a menu to do the following operations on a
rectangle –
RECTANGLE OPERATIONS
1. AREA
2. PERIMETER
3. DIAGONAL
4. EXIT
SOURCE CODE

PAGE 15
import math
while True:
print("\nRECTANGLE OPERATIONS")
print("1. AREA")
print("2. PERIMETER")
print("3. DIAGONAL")
print("4. EXIT")
choice = int(input("Enter choice: "))
if choice == 4:
break
length = float(input("Enter length: "))
breadth = float(input("Enter breadth: "))
if choice == 1:
print("Area =", length * breadth)
elif choice == 2:
print("Perimeter =", 2 * (length + breadth))
elif choice == 3:
print("Diagonal =", [Link](length**2 + breadth**2))
else:
print("Invalid choice")
OUTPUT:
RECTANGLE OPERATIONS
1. AREA
2. PERIMETER
3. DIAGONAL
4. EXIT
Enter choice: 3
Enter length: 5
Enter breadth: 2
Diagonal = 5.385164807134504
------------------------------------------------------------------------------------------------------------
[Link] a program to create a menu to do the following operations on a circle –
CIRCLE OPERATIONS
1. AREA
2. CIRCUMFERENCE
3. DIAMETER
4. EXIT
SOURCE CODE

import math
while True:
print("\nCIRCLE OPERATIONS")
print("1. AREA")
print("2. CIRCUMFERENCE")
print("3. DIAMETER")

PAGE 16
print("4. EXIT")
choice = int(input("Enter choice: "))
if choice == 4:
break
r = float(input("Enter radius: "))
if choice == 1:
print("Area =", [Link] * r * r)
elif choice == 2:
print("Circumference =", 2 * [Link] * r)
elif choice == 3:
print("Diameter =", 2 * r)
else:
print("Invalid choice")
OUTPUT:
CIRCLE OPERATIONS
1. AREA
2. CIRCUMFERENCE
3. DIAMETER
4. EXIT
Enter choice: 2
Enter radius: 5
Circumference = 31.41592653589793
------------------------------------------------------------------------------------------------------------
Q17. Write a program to create a menu to do the following operations on a Square –
SQUARE OPERATIONS
1. AREA
2. PERIMETER
3. EXIT
SOURCE CODE

while True:
print("\nSQUARE OPERATIONS")
print("1. AREA")
print("2. PERIMETER")
print("3. EXIT")
choice = int(input("Enter choice: "))
if choice == 3:
break
side = float(input("Enter side: "))
if choice == 1:
print("Area =", side * side)
elif choice == 2:
print("Perimeter =", 4 * side)
else:
print("Invalid choice")

PAGE 17
OUTPUT:
SQUARE OPERATIONS
1. AREA
2. PERIMETER
3. EXIT
Enter choice: 2
Enter side: 3
Perimeter = 12.0
------------------------------------------------------------------------------------------------------------
[Link] a program to create a menu to do the following operations on two
numbers entered by user –
ARITHMETIC OPERATIONS
1. SUM
2. DIFFERENCE
3. PRODUCT
4. QUOTIENT
5. REMAINDER
6. EXIT
SOURCE CODE

while True:
print("\nARITHMETIC OPERATIONS")
print("1. SUM")
print("2. DIFFERENCE")
print("3. PRODUCT")
print("4. QUOTIENT")
print("5. REMAINDER")
print("6. EXIT")
choice = int(input("Enter choice: "))
if choice == 6:
break
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
if choice == 1:
print("Sum =", a + b)
elif choice == 2:
print("Difference =", a - b)
elif choice == 3:
print("Product =", a * b)
elif choice == 4:
print("Quotient =", a / b)
elif choice == 5:
print("Remainder =", a % b)
else:
print("Invalid choice")

PAGE 18
OUTPUT:
ARITHMETIC OPERATIONS
1. SUM
2. DIFFERENCE
3. PRODUCT
4. QUOTIENT
5. REMAINDER
6. EXIT
Enter choice: 3
Enter first number: 1
Enter second number: 2
Product = 2.0
------------------------------------------------------------------------------------------------------------
Q19. Write a program to generate electricity bill by inputting Consumer No, Name,
Address, Meter Type
(Commercial or Domestic) and units consumed. Find and display the bill by the
following criteria –

SOURCE CODE

consumer_no = input("Consumer No: ")


name = input("Name: ")
address = input("Address: ")
meter_type = input("Meter Type (Domestic/Commercial): ").lower()
units = float(input("Units consumed: "))

if meter_type == "domestic":
if units <= 200:
rate = 5
elif units <= 400:
rate = 7.5

PAGE 19
else:
rate = 10
elif meter_type == "commercial":
if units <= 200:
rate = 10
elif units <= 400:
rate = 15
else:
rate = 20
else:
rate = 0

bill = units * rate


print("\nElectricity Bill")
print("Consumer No:", consumer_no)
print("Name:", name)
print("Address:", address)
print("Meter Type:", meter_type)
print("Units Consumed:", units)
print("Rate per Unit:", rate)
print("Total Bill: Rs.", bill)
OUTPUT:
Consumer No: 22
Name: ATHARV JAIN
Address: ABC COLONY
Meter Type (Domestic/Commercial): Domestic
Units consumed: 399

Electricity Bill
Consumer No: 22
Name: ATHARV JAIN
Address: ABC COLONY
Meter Type: domestic
Units Consumed: 399.0
Rate per Unit: 7.5
Total Bill: Rs. 2992.5
------------------------------------------------------------------------------------------------------------
Q20. Write a program to create a menu to do the following operations on a number
entered by user –
MENU
1. FIND ODD OR EVEN
2. FIND IT IS OF 3 DIGITS OR NOT
3. LAST DIGIT IS 7 OR NOT
4. EXIT
SOURCE CODE

PAGE 20
while True:
print("\nMENU")
print("1. FIND ODD OR EVEN")
print("2. FIND IF 3 DIGITS OR NOT")
print("3. LAST DIGIT IS 7 OR NOT")
print("4. EXIT")
choice = int(input("Enter choice: "))
if choice == 4:
break
num = int(input("Enter number: "))
if choice == 1:
print("Even" if num % 2 == 0 else "Odd")
elif choice == 2:
print("3-digit number" if 100 <= abs(num) <= 999 else "Not a 3-digit number")
elif choice == 3:
print("Last digit is 7" if abs(num) % 10 == 7 else "Last digit is NOT 7")
else:
print("Invalid choice")
OUTPUT:
MENU
1. FIND ODD OR EVEN
2. FIND IF 3 DIGITS OR NOT
3. LAST DIGIT IS 7 OR NOT
4. EXIT
Enter choice: 1
Enter number: 33
Odd
------------------------------------------------------------------------------------------------------------
Q21. Find the roots of a quadratic equation (Ax2 + Bx + C = 0).
SOURCE CODE
import math
a = float(input("Enter coefficient a: "))
b = float(input("Enter coefficient b: "))
c = float(input("Enter coefficient c: "))
d = b**2 - 4*a*c # discriminant
if d > 0:
root1 = (-b + [Link](d)) / (2*a)
root2 = (-b - [Link](d)) / (2*a)
print("Roots are real and different:", root1, root2)
elif d == 0:
root = -b / (2*a)
print("Roots are real and equal:", root)
else:
real_part = -b / (2*a)
imag_part = [Link](-d) / (2*a)

PAGE 21
print("Roots are complex:", f"{real_part} ± {imag_part}i")
OUTPUT:
Enter coefficient a: 2
Enter coefficient b: 4
Enter coefficient c: 2
Roots are real and equal: -1.0
------------------------------------------------------------------------------------------------------------
ITERATION-LOOPS

[Link] first 10 natural numbers.


SOURCE CODE
print("First 10 natural numbers")
for i in range(1, 11):
print(i, end=" ")
print("\n")
OUTPUT:
1 2 3 4 5 6 7 8 9 10
------------------------------------------------------------------------------------------------------------
[Link] 100, 95, 90, 85, ………………………10
SOURCE CODE
print("100, 95, 90, ..., 10")
for i in range(100, 9, -5):
print(i, end=" ")
print("\n")

OUTPUT:
100 95 90 85 80 75 70 65 60 55 50 45 40 35 30 25 20 15 10
------------------------------------------------------------------------------------------------------------
[Link] 1,4,9,16, 25, ………………………….400

PAGE 22
SOURCE CODE
for i in range(1, 21):
print(i**2, end=" ")
print("\n")
OUTPUT:
1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361 400
------------------------------------------------------------------------------------------------------------
[Link] 1,8,27,64,125, ………………………….1000
SOURCE CODE
for i in range(1, 11):
print(i**3, end=" ")
print("\n")
OUTPUT:
1 8 27 64 125 216 343 512 729 1000
------------------------------------------------------------------------------------------------------------
[Link] 1,4,7,10,13,16,19……………………N
SOURCE CODE
N = int(input("Enter N: "))
a=1
while a <= N:
print(a, end=" ")
a += 3
print("\n")

OUTPUT:
Enter N: 38
1 4 7 10 13 16 19 22 25 28 31 34 37
------------------------------------------------------------------------------------------------------------
[Link] 1,2,4,7,11,16, ………………. N terms
SOURCE CODE

PAGE 23
N = int(input("Enter N: "))
a=1
diff = 1
for i in range(N):
print(a, end=" ")
diff += 1
a += diff
print("\n")
OUTPUT:
Enter N: 38
1 3 6 10 15 21 28 36 45 55 66 78 91 105 120 136 153 171 190 210 231 253 276 300 325
351 378 406 435 465 496 528 561 595 630 666 703 741
------------------------------------------------------------------------------------------------------------
[Link] 1, -4, 7, -10, ……………………N terms
SOURCE CODE
N = int(input("Enter N: "))
num = 1
sign = 1
for i in range(N):
print(num * sign, end=" ")
num += 3
sign *= -1
print("\n")

OUTPUT:
Enter N: 38
1 -4 7 -10 13 -16 19 -22 25 -28 31 -34 37 -40 43 -46 49 -52 55 -58 61 -64 67 -70 73 -76
79 -82 85 -88 91 -94 97 -100 103 -106 109 -112
------------------------------------------------------------------------------------------------------------
[Link] your name N times.

PAGE 24
SOURCE CODE
name = input("Enter your name: ")
N = int(input("Enter N: "))
for _ in range(N):
print(name)
print("\n")
OUTPUT:
Enter your name: Atharv
Enter N: 5
Atharv
Atharv
Atharv
Atharv
Atharv
------------------------------------------------------------------------------------------------------------
[Link] first N odd Numbers, where N is entered by user.
SOURCE CODE
N = int(input("Enter N: "))
for i in range(1, 2*N, 2):
print(i, end=" ")
print("\n")
OUTPUT: Enter N: 5
13579

Q10. Display first N even Numbers, where N is entered by user


SOURCE CODE
N = int(input("Enter N: "))
for i in range(2, 2*N+1, 2):
print(i, end=" ")

PAGE 25
print("\n")
OUTPUT:
Enter N: 5
2 4 6 8 10
------------------------------------------------------------------------------------------------------------
Q11. Find the sum of following series 5 + 10 + 15 + 20 +……………………………. +
50
SOURCE CODE
total = 0
for i in range(5, 51, 5):
total += i
print("Sum =", total, "\n")
OUTPUT: Sum = 275
------------------------------------------------------------------------------------------------------------
Q12. Find Sum of (1) + (1+2) + (1+2+3) + ………………………N terms.
SOURCE CODE
N = int(input("Enter N: "))
total = 0
for i in range(1, N+1):
total += sum(range(1, i+1))
print("Sum =", total, "\n")
OUTPUT:
Enter N: 5
Sum = 35

Q13. Find sum of x + x2 + x3 + ………………………… + xN


SOURCE CODE
x = int(input("Enter x: "))
N = int(input("Enter N: "))
total = 0

PAGE 26
for i in range(1, N+1):
total += x**i
print("Sum =", total, "\n")
OUTPUT:
Enter x: 3
Enter N: 5
Sum = 363
------------------------------------------------------------------------------------------------------------
[Link] the sum of 1/2 + 3/5 + 5/8 + ... N term
SOURCE CODE
N = int(input("Enter N: "))
num = 1
den = 2
total = 0
for _ in range(N):
total += num/den
num += 2
den += 3
print("Sum =", total, "\n")
OUTPUT:
Enter N: 5
Sum = 3.004220779220779
------------------------------------------------------------------------------------------------------------

Q15. Display the Fibonacci series 0,1,1,2,3,5, 8, 13, ………….. N terms


SOURCE CODE
N = int(input("Enter N: "))
a, b = 0, 1
for _ in range(N):
print(a, end=" ")

PAGE 27
a, b = b, a+b
print("\n")
OUTPUT:
Enter N: 5
01123
------------------------------------------------------------------------------------------------------------
[Link] Age of N employees and then find how many are in the age group of
(a) 26 – 35 (b) 36 – 45 (c) 46 - 55
SOURCE CODE
N = int(input("Enter number of employees: "))
g1 = g2 = g3 = 0
for _ in range(N):
age = int(input("Enter age: "))
if 26 <= age <= 35:
g1 += 1
elif 36 <= age <= 45:
g2 += 1
elif 46 <= age <= 55:
g3 += 1
print("26-35:", g1, "36-45:", g2, "46-55:", g3, "\n")
OUTPUT:
Enter number of employees: 5
Enter age: 36
Enter age: 28
Enter age: 48
Enter age: 49
Enter age: 27
26-35: 2
36-45: 1

PAGE 28
46-55: 2
------------------------------------------------------------------------------------------------------------
[Link] a number and then display its table.
SOURCE CODE
num = int(input("Enter number: "))
for i in range(1, 11):
print(num, "x", i, "=", num*i)
print("\n")
OUTPUT:
Enter 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
------------------------------------------------------------------------------------------------------------
Q18. Input a number and then display first 20 multiples of it.
SOURCE CODE
num = int(input("Enter number: "))
for i in range(1, 21):
print(num*i, end=" ")
print("\n")
OUTPUT:
Enter number: 2
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40

PAGE 29
------------------------------------------------------------------------------------------------------------
Q19. Input a number and then display its factors.
SOURCE CODE
num = int(input("Enter number: "))
for i in range(1, num+1):
if num % i == 0:
print(i, end=" ")
print("\n")
OUTPUT:
Enter number: 4
124
------------------------------------------------------------------------------------------------------------
Q20. Input a number and then find and display its factorial.
SOURCE CODE
num = int(input("Enter number: "))
fact = 1
for i in range(1, num+1):
fact *= i
print("Factorial =", fact, "\n")
OUTPUT:
Enter number: 5
Factorial = 120
------------------------------------------------------------------------------------------------------------

[Link] a number and then find is it a prime number, composite number or not
prime nor composite or not?
SOURCE CODE
num = int(input("Enter number: "))
if num <= 1:

PAGE 30
print("Neither prime nor composite")
else:
for i in range(2, int(num**0.5)+1):
if num % i == 0:
print("Composite")
break
else:
print("Prime")
print("\n")
OUTPUT:
Enter number: 5
Prime
------------------------------------------------------------------------------------------------------------
Q22. Input a number and then find is it a perfect number? [Ex: 6 = 1 +2 +3]
SOURCE CODE
num = int(input("Enter number: "))
sum_div = 0
for i in range(1, num):
if num % i == 0:
sum_div += i
if sum_div == num:
print("Perfect number")
else:
print("Not perfect")
print("\n")
OUTPUT:
Enter number: 6
Perfect number
------------------------------------------------------------------------------------------------------------

PAGE 31
Q23. Input a number and then find sum of its digits. (Ex: Number is 186 then sum
of digits is 15)
SOURCE CODE
num = int(input("Enter number: "))
sum_d = sum(int(d) for d in str(num))
print("Sum of digits =", sum_d, "\n")
OUTPUT:
Enter number: 12
Sum of digits = 3
------------------------------------------------------------------------------------------------------------
[Link] a number and then find product of its digits. (Ex: Number is 286 then
product of digits is 96)
SOURCE CODE
num = int(input("Enter number: "))
prod = 1
for d in str(num):
prod *= int(d)
print("Product of digits =", prod, "\n")
OUTPUT:
Enter number: 123
Product of digits = 6
------------------------------------------------------------------------------------------------------------

Q25. Input a number and then find how many digits are in the given number.
SOURCE CODE
num = int(input("Enter number: "))
print("Number of digits =", len(str(num)), "\n")
OUTPUT:

PAGE 32
Enter number: 123
Number of digits = 3
------------------------------------------------------------------------------------------------------------
Q26. Input a number and then find and display its reverse. (Ex: Number is 7869
then reverse is 9687)
SOURCE CODE
num = int(input("Enter number: "))
print("Reverse =", int(str(num)[::-1]), "\n")
OUTPUT:
Enter number: 123
Reverse = 321
------------------------------------------------------------------------------------------------------------
[Link] a number and then find is it a palindrome number or not?
SOURCE CODE
num = int(input("Enter number: "))
if str(num) == str(num)[::-1]:
print("Palindrome")
else:
print("Not palindrome")
print("\n")
OUTPUT:
Enter number: 5
Palindrome
------------------------------------------------------------------------------------------------------------

Q28. Input a number (3 digits) and then find is it a Armstrong number or not?
(Sum of cube of digits = Number)
SOURCE CODE
num = int(input("Enter 3-digit number: "))
if sum(int(d)**3 for d in str(num)) == num:
print("Armstrong number")

PAGE 33
else:
print("Not Armstrong")
print("\n")
OUTPUT:
Enter 3-digit number: 123
Not Armstrong
------------------------------------------------------------------------------------------------------------
Q29. Find HCF (Highest common factor) of 2 numbers M and N, which are entered
by user.
SOURCE CODE
m = int(input("Enter first number: "))
n = int(input("Enter second number: "))
while n:
m, n = n, m % n
print("HCF =", m, "\n")
OUTPUT:
Enter first number: 12
Enter second number: 6
HCF = 6
------------------------------------------------------------------------------------------------------------
[Link] LCM (Least common multiple) of 2 numbers M and N, which are entered
by user
SOURCE CODE
m = int(input("Enter first number: "))
n = int(input("Enter second number: "))
a, b = m, n
while n:
m, n = n, m % n
hcf = m
lcm = (a*b)//hcf

PAGE 34
print("LCM =", lcm, "\n")
OUTPUT:
Enter first number: 12
Enter second number: 24
LCM = 24
------------------------------------------------------------------------------------------------------------
[Link] a program to display all prime numbers which are of 2 digits.
SOURCE CODE
for num in range(10, 100):
for i in range(2, int(num**0.5)+1):
if num % i == 0:
break
else:
print(num, end=" ")
print("\n")
OUTPUT:
11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
------------------------------------------------------------------------------------------------------------
[Link] a program to find sum of 1! + 3! + 5! + 7! +………………………. + N!
SOURCE CODE
N = int(input("Enter N: "))
def factorial(x):
f=1
for i in range(1, x+1):
f *= i
return f
total = sum(factorial(i) for i in range(1, N+1, 2))
print("Sum =", total, "\n")
OUTPUT:
Enter N: 6

PAGE 35
Sum = 127
------------------------------------------------------------------------------------------------------------
Q33. Find Sum of x – x2/2! + x3/3! – x4/4! + ……………………….. Nth term
SOURCE CODE
x = float(input("Enter value of x: "))
N = int(input("Enter number of terms N: "))

def factorial(num):
fact = 1
for i in range(1, num + 1):
fact *= i
return fact

total = 0
for i in range(1, N + 1):
term = (x ** i) / factorial(i)
if i % 2 == 0:
total -= term
else:
total += term

print(f"Sum of series = {total}")

OUTPUT:
Enter value of x: 5
Enter number of terms N: 10
Sum of series = 0.13596092372133883
------------------------------------------------------------------------------------------------------------
[Link] a program to display first N prime numbers where N is entered by user. [
If N is 7 then it should display as 2 3 5 7 11 13 17]

PAGE 36
SOURCE CODE
N = int(input("Enter N: "))
count = 0
num = 2
while count < N:
for i in range(2, int(num**0.5)+1):
if num % i == 0:
break
else:
print(num, end=" ")
count += 1
num += 1
print("\n")
OUTPUT:
Enter N: 5
2 3 5 7 11
------------------------------------------------------------------------------------------------------------

PAGE 37
SOURCE CODE
N = int(input("Enter N for patterns: "))

Pattern 1
for i in range(1, N+1):
for j in range(i, 0, -1):
print(j, end=" ")
print()

Pattern 2
for i in range(1, N+1):
for j in range(1, i+1):
print(j, end=" ")
print()

PAGE 38
Pattern 3
for i in range(1, N+1):
print(" "*(N-i), end="")
for j in range(1, i+1):
print(j, end="")
print()

Pattern 4
for i in range(1, N+1):
print("* "*(2*i-1))

Pattern 5
for i in range(N, 0, -1):
for j in range(i, 0, -1):
print(j, end=" ")
print()

Pattern 6
for i in range(1, N+1):
for j in range(65, 65+i):
print(chr(j), end=" ")
print()

Pattern 7
num = 1
for i in range(1, N+1):
for j in range(i):
print(num, end=" ")
num += 1

PAGE 39
print()

Pattern 8
chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for i in range(N, 0, -1):
print(chars[i-1:N])
OUTPUT:
Enter N for patterns: 5
1
21
321
4321
54321

1
12
123
1234
12345

1
12
123
1234
12345

PAGE 40
*
***
*****
*******
*********

54321
4321
321
21
1

A
AB
ABC
ABCD
ABCDE

1
23
456
7 8 9 10
11 12 13 14 15

PAGE 41
E
DE
CDE
BCDE
ABCDE
------------------------------------------------------------------------------------------------------------

PAGE 42

You might also like