11 Python Programs with Code and Output
1. Area of Circle and Triangle
Code:
import math
r=5
circle_area = [Link] * r * r
base = 10
height = 6
triangle_area = 0.5 * base * height
print(circle_area)
print(triangle_area)
Output:
78.53981633974483
30.0
2. Swap Two Variables
Code:
a=5
b = 10
a, b = b, a
print(a)
print(b)
Output:
10
5
3. Generate Random Number
Code:
import random
print([Link](1, 100))
Output:
57 (sample output)
4. Kilometers to Miles
Code:
km = 10
miles = km * 0.621371
print(miles)
Output:
6.21371
5. Maximum of Two Numbers
Code:
a = 15
b=9
if a > b:
print(a)
else:
print(b)
Output:
15
6. Even or Odd
Code:
n=7
if n % 2 == 0:
print("Even")
else:
print("Odd")
Output:
Odd
7. Positive, Negative or Zero
Code:
n = -3
if n > 0:
print("Positive")
elif n < 0:
print("Negative")
else:
print("Zero")
Output:
Negative
8. Number of Desks
Code:
a = 20
b = 21
c = 22
desks = (a + 1)//2 + (b + 1)//2 + (c + 1)//2
print(desks)
Output:
32
9. Hour Hand Angle
Code:
H=3
M = 30
S=0
angle = H*30 + M*0.5 + S*(0.5/60)
print(angle)
Output:
105.0
10. Fourth Vertex of Rectangle
Code:
x = [1, 1, 7]
y = [4, 6, 4]
for i in x:
if [Link](i) == 1:
x4 = i
for j in y:
if [Link](j) == 1:
y4 = j
print(x4, y4)
Output:
76
11. Missing Card Number
Code:
N=5
cards = [1, 2, 4, 5]
total = N * (N + 1) // 2
missing = total - sum(cards)
print(missing)
Output:
3