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

Computer Science Practical Python Programs

The document contains a practical file with 15 Python programs, each demonstrating a specific functionality such as adding numbers, finding squares, and checking for leap years. Each program includes the aim, code, sample input, and output. The programs cover fundamental programming concepts and operations in Python.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Computer Science Practical Python Programs

The document contains a practical file with 15 Python programs, each demonstrating a specific functionality such as adding numbers, finding squares, and checking for leap years. Each program includes the aim, code, sample input, and output. The programs cover fundamental programming concepts and operations in Python.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Computer Science Practical File

15 Python Programs with Input and Output

1. Program to Add Two Numbers


Aim: To write a program to add two numbers.

Code:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
sum = a + b
print("Sum =", sum)

Input:
Enter first number: 5
Enter second number: 3

Output:
Sum = 8

2. Program to Find Square of a Number


Aim: To find the square of a number.

Code:
num = int(input("Enter a number: "))
square = num * num
print("Square =", square)

Input:
Enter a number: 4

Output:
Square = 16

3. Program to Find Area of Rectangle


Aim: To calculate area of rectangle.

Code:
length = int(input("Enter length: "))
breadth = int(input("Enter breadth: "))
area = length * breadth
print("Area =", area)

Input:
Enter length: 10
Enter breadth: 5

Output:
Area = 50

4. Program to Check Even or Odd


Aim: To check whether a number is even or odd.

Code:
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even Number")
else:
print("Odd Number")

Input:
Enter a number: 7

Output:
Odd Number

5. Program to Find Largest of Two Numbers


Aim: To find largest of two numbers.

Code:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
if a > b:
print("Largest =", a)
else:
print("Largest =", b)

Input:
Enter first number: 8
Enter second number: 5

Output:
Largest = 8

6. Program to Swap Two Numbers


Aim: To swap two numbers.

Code:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
temp = a
a = b
b = temp
print("a =", a)
print("b =", b)

Input:
Enter first number: 4
Enter second number: 9

Output:
a = 9
b = 4

7. Program to Find Simple Interest


Aim: To calculate simple interest.

Code:
p = int(input("Enter principal: "))
r = int(input("Enter rate: "))
t = int(input("Enter time: "))
si = (p * r * t) / 100
print("Simple Interest =", si)

Input:
Enter principal: 1000
Enter rate: 5
Enter time: 2

Output:
Simple Interest = 100

8. Program to Find Factorial


Aim: To find factorial of a number.

Code:
num = int(input("Enter a number: "))
fact = 1
for i in range(1, num + 1):
fact = fact * i
print("Factorial =", fact)

Input:
Enter a number: 5

Output:
Factorial = 120

9. Program to Print Table


Aim: To print multiplication table.

Code:
num = int(input("Enter a number: "))
for i in range(1, 11):
print(num, "x", i, "=", num*i)

Input:
Enter a number: 3

Output:
3 x 1 = 3
...
3 x 10 = 30

10. Program to Check Leap Year


Aim: To check leap year.

Code:
year = int(input("Enter year: "))
if year % 4 == 0:
print("Leap Year")
else:
print("Not a Leap Year")

Input:
Enter year: 2024

Output:
Leap Year

11. Program to Reverse a Number


Aim: To reverse a number.
Code:
num = int(input("Enter a number: "))
rev = 0
while num > 0:
digit = num % 10
rev = rev * 10 + digit
num = num // 10
print("Reverse =", rev)

Input:
Enter a number: 123

Output:
Reverse = 321

12. Program to Count Digits


Aim: To count digits in a number.

Code:
num = int(input("Enter a number: "))
count = 0
while num > 0:
num = num // 10
count += 1
print("Total digits =", count)

Input:
Enter a number: 5678

Output:
Total digits = 4

13. Program to Find Average of Three Numbers


Aim: To find average of three numbers.

Code:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
avg = (a + b + c) / 3
print("Average =", avg)

Input:
Enter first number: 10
Enter second number: 20
Enter third number: 30

Output:
Average = 20

14. Program to Convert Celsius to Fahrenheit


Aim: Convert Celsius to Fahrenheit.

Code:
c = float(input("Enter temperature in Celsius: "))
f = (c * 9/5) + 32
print("Fahrenheit =", f)

Input:
Enter temperature in Celsius: 25

Output:
Fahrenheit = 77
15. Program to Check Positive, Negative or Zero
Aim: Check whether number is positive, negative or zero.

Code:
num = int(input("Enter a number: "))
if num > 0:
print("Positive Number")
elif num < 0:
print("Negative Number")
else:
print("Zero")

Input:
Enter a number: -4

Output:
Negative Number

You might also like