0% found this document useful (0 votes)
16 views

Python Programs

The document contains 20 Python programs that demonstrate basic programming concepts like input/output, conditional statements, loops, functions etc. The programs include calculating sum, average, interest, checking even/odd, greatest of 3 numbers, printing patterns, tables, natural numbers etc. Each program contains the code, input/output examples.

Uploaded by

Sarthak Bishnoi
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)
16 views

Python Programs

The document contains 20 Python programs that demonstrate basic programming concepts like input/output, conditional statements, loops, functions etc. The programs include calculating sum, average, interest, checking even/odd, greatest of 3 numbers, printing patterns, tables, natural numbers etc. Each program contains the code, input/output examples.

Uploaded by

Sarthak Bishnoi
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
You are on page 1/ 9

Python Programs

1. Write a program to find the sum of two numbers.


n1 = int(input("Enter the first number"))
n2 = int(input("Enter the second number"))
sum = n1+n2
print("Sum is ",sum)

Output:
Enter the first number 5
Enter the second number 10
Sum is 15

2. Write a program to find the average of three numbers


n1 = int(input("Enter the first number"))
n2 = int(input("Enter the second number"))
n3 = int(input ("Enter the third number"))
avg = (n1+n2+n3)/3
print("Average is",avg)

Output:
Enter the first number 5
Enter the second number 10
Enter the third number 15
Average is 10

3. Write a program to calculate simple interest.


p = int(input("Enter Principle : "))
r = int(input("Enter Rate : "))
t = int(input ("Enter Time : "))
si = (p*r*t)/100
print("Simple Interest is ",si)

Output:
Enter Principle : 5000
Enter Rate : 10
Enter Time : 5
Simple Interest is 2500
4. Write a program to find if a given number is odd or even.
n = int(input(“Enter any number : ”))
if n%2==0:
print(n, “ is Even”)
else:
print(n, “ is Odd”)

Output:
Enter any number : 5
5 is Odd

5. Write a program to calculate Area of Circle.


r = float(input("Enter radius of circle"))
area = 3.14 * r * r
print("Area of Circle is", area)

Output:
Enter radius of circle 5
Area of Circle is 78.5

6. Write a program to convert Celsius to Fahrenheit.


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

Output:
Enter temperature in Celsius 50
Temperature in Fahrenheit is 122

7. Write a program to calculate the perimeter of a rectangle.


l = int(input("Enter Length of Rectangle: "))
b = int(input("Enter Breadth of Rectangle: "))
p = 2*(l+b)
print("Perimeter = ", p)

Output:
Enter Length of Rectangle: 10
Enter Breadth of Rectangle: 5
Perimeter = 30
8. Write a program to find the greatest of three numbers
n1 = int(input("Enter First Number: "))
n2 = int(input("Enter Second Number: "))
n3 = int(input("Enter Third Number: "))
if n1>n2 and n1>n3:
print(" Largest =", n1)
if n2>n1 and n2>n3:
print(" Largest =", n2)
if n3>n1 and n3>n2:
print(" Largest =", n3)

Output:
Enter First Number: 10
Enter Second Number: 15
Enter Third Number: 20
Largest = 20

9. Write a program to check the validity of age for Voting.


x = int(input ("Enter your age: "))
if x>=18:
print("You are eligible for voting")
else :
print("You are not eligible for voting")

Output:
Enter your age: 18
You are eligible for voting

10. Write a program to check whether the entered number is positive, negative
or zero.
x = int(input ("Enter the number"))
if x>0:
print("x is positive ")
elif x<0:
print("x is negative ")
elif x==0:
print("x is zero")
else:
print("Error")
Output:
Enter the number 0
x is zero

11. Write a program to print 0 to 5 digits in words.


x = int(input(“Enter numbers between 0 to 5: ”))
if x==0:
print(“Zero”)
elif x==1:
print(“One”)
elif x==2:
print(“Two”)
elif x==3:
print(“Three”)
elif x==4:
print(“Four”)
elif x==5:
print(“Five”)
else:
print(“Invalid Choice”)

Output:
Enter numbers between 0 to 5: 0
Zero

12. Write a program to print the first 10 natural numbers.


i=1
print(“The First 10 natural Numbers:”)
while i<=10:
print(i)
i = i+1

Output:
The First 10 natural Numbers:
1
2
3
4
5
6
7
8
9
10

13. Write a program to print a table of any number.


x = int(input(“Enter any number: ”))
i=1
while i<=10:
print(i*x)
i = i+1

Output:
Enter any number: 10
10
20
30
40
50
60
70
80
90
100

14. Write a program to print a table of 5.


i=1
while i<=10:
print(i*5)
i = i+1

Output:
5
10
15
20
25
30
35
40
45
50

15. Write a program to print nos from 10 to 1.


i = 10
print(“Descending numbers from 10 to 1:”)
while i>=1:
print(i)
i = i-1

Output:
Descending numbers from 10 to 1:
10
9
8
7
6
5
4
3
2
1

16. Write a program to print even numbers from 1 to 20.


i=1
print(“Even numbers between 1 to 20 are ”)
while i<=20:
if i%2==0:
print(i)
i = i+1
else:
i = i+1

Output:
Even numbers between 1 to 20 are
2
4
6
8
10
12
14
16
18
20

17. Write a program to print square numbers.


i=1
print(“The First 10 Natural Numbers Square are :”)
while i<=10:
print(i*i)
i = i+1

Output:
The First 10 Natural Numbers Square are :
4
9
16
25
36
49
64
81
100

18. Write a program to print odd numbers from 1 to 20.


i=1
print(“Odd numbers between 1 to 20 are ”)
while i<=20:
if i%2==0:
i = i+1
else:
print(i)
i = i+1

Output:
Odd numbers between 1 to 20 are
1
3
5
7
9
11
13
15
17
19

19. Write a program to design a calculator.


print(“***** Calculator *****”)
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
ch = int(input("Enter Your Choice (1-4): "))
num1 = float(input("Enter First Numbers: "))
num2 = float(input("Enter Second Numbers: "))
if ch==1:
result = num1 + num2
print("Result = ", result)
elif ch==2:
result = num1 - num2
print("Result = ", result)
elif ch==3:
result = num1 * num2
print("Result = ", result)
elif ch==4:
result = num1 / num2
print("Result = ", result)
else:
print("Invalid Input!..Try Again!")
print("------------------------")

Output:
***** Calculator *****
1. Addition
2. Subtraction
3. Multiplication
4. Division
Enter Your Choice (1-4): 1
Enter First Numbers: 15
Enter Second Numbers: 15
Result = 30
------------------------

20. Write a program to print fruit names as per entered letter.


print(“***** Fruit Name *****”)
print("A")
print("B")
print("C")
print("D")
ch = input("Enter Your Choice (A-D): ")
if ch==”A” or ch==”a”:
print("Apple")
elif ch==”B” or ch==”b”:
print("Banana")
elif ch==”C” or ch==”c”:
print("Cherry")
elif ch==”D” or ch==”d”:
print("Dragon Fruit")
else:
print("Invalid Input!..Try Again!")
print("------------------------")

Output:
***** Fruit Name *****
A
B
C
D
Enter Your Choice (A-D): D
Dragon Fruit
------------------------

You might also like