1.
Arithmetic Operations
Code:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Addition =", a + b)
print("Subtraction =", a - b)
print("Multiplication =", a * b)
print("Division =", a / b)
print("Floor Division =", a // b)
print("Modulus =", a % b)
print("Exponent =", a ** b)
Sample Input / Output:
Input:
10
3
Output:
Addition = 13
Subtraction = 7
Multiplication = 30
Division = 3.3333333333333335
Floor Division = 3
Modulus = 1
Exponent = 1000
2. Simple Interest
Code:
p = float(input("Enter principal: "))
t = float(input("Enter time: "))
r = float(input("Enter rate: "))
si = (p * t * r) / 100
print("Simple Interest =", si)
Sample Input / Output:
Input:
1000
2
5
Output:
Simple Interest = 100.0
3. Distance Between Two Points
Code:
import math
x1 = float(input("Enter x1: "))
y1 = float(input("Enter y1: "))
x2 = float(input("Enter x2: "))
y2 = float(input("Enter y2: "))
d = [Link]((x2-x1)**2 + (y2-y1)**2)
print("Distance =", d)
Sample Input / Output:
Input:
1 2
4 6
Output:
Distance = 5.0
4. Swapping Two Numbers
Code:
a = int(input("Enter a: "))
b = int(input("Enter b: "))
a, b = b, a
print("After swapping:", a, b)
Sample Input / Output:
Input:
5
10
Output:
After swapping: 10 5
5. Area of Square and Rectangle
Code:
s = int(input("Enter side of square: "))
l = int(input("Enter length of rectangle: "))
b = int(input("Enter breadth of rectangle: "))
print("Area of square =", s*s)
print("Area of rectangle =", l*b)
Sample Input / Output:
Input:
4
5
6
Output:
Area of square = 16
Area of rectangle = 30
6. Positive Number
Code:
n = int(input("Enter number: "))
if n > 0:
print(n)
Sample Input / Output:
Input:
8
Output:
8
7. Even or Odd
Code:
n = int(input("Enter number: "))
if n % 2 == 0:
print("Even")
else:
print("Odd")
Sample Input / Output:
Input:
7
Output:
Odd
8. Voting Eligibility
Code:
age = int(input("Enter age: "))
if age >= 18:
print("Eligible for voting")
else:
print("Not eligible for voting")
Sample Input / Output:
Input:
20
Output:
Eligible for voting
9. Largest of Two Numbers
Code:
a = int(input("Enter a: "))
b = int(input("Enter b: "))
if a > b:
print("Largest =", a)
else:
print("Largest =", b)
Sample Input / Output:
Input:
8
5
Output:
Largest = 8
10. Leap Year
Code:
y = int(input("Enter year: "))
if (y % 4 == 0 and y % 100 != 0) or y % 400 == 0:
print("Leap Year")
else:
print("Not a Leap Year")
Sample Input / Output:
Input:
2024
Output:
Leap Year
11. Relationship Between Two Integers
Code:
a = int(input("Enter a: "))
b = int(input("Enter b: "))
if a > b:
print("a is greater than b")
elif a < b:
print("a is less than b")
else:
print("a is equal to b")
Sample Input / Output:
Input:
5
5
Output:
a is equal to b
12. Grade Based on Marks
Code:
m = int(input("Enter marks: "))
if m >= 90:
print("Grade A")
elif m >= 80:
print("Grade B")
elif m >= 70:
print("Grade C")
elif m >= 60:
print("Grade D")
elif m >= 40:
print("Grade E")
else:
print("Grade F")
Sample Input / Output:
Input:
85
Output:
Grade B
13. Parking Charges
Code:
v = input("Enter vehicle type (b/c/t): ").lower()
h = int(input("Enter hours: "))
if v == 'b':
print("Charge =", h*20)
elif v == 'c':
print("Charge =", h*15)
elif v == 't':
print("Charge =", h*10)
else:
print("Invalid vehicle type")
Sample Input / Output:
Input:
c
3
Output:
Charge = 45
14. Sum of Digits
Code:
n = int(input("Enter number: "))
s = 0
while n > 0:
s += n % 10
n //= 10
print("Sum of digits =", s)
Sample Input / Output:
Input:
123
Output:
Sum of digits = 6
15. Palindrome Number
Code:
n = int(input("Enter number: "))
temp = n
rev = 0
while n > 0:
rev = rev*10 + n%10
n //= 10
if temp == rev:
print("Palindrome")
else:
print("Not Palindrome")
Sample Input / Output:
Input:
1331
Output:
Palindrome
16. Fibonacci Series
Code:
n = int(input("Enter number of terms: "))
a, b = 0, 1
for i in range(n):
print(a, end=" ")
a, b = b, a+b
Sample Input / Output:
Input:
6
Output:
0 1 1 2 3 5
17. Prime Number
Code:
n = int(input("Enter number: "))
if n <= 1:
print("Not Prime")
else:
for i in range(2, n):
if n % i == 0:
print("Not Prime")
break
else:
print("Prime")
Sample Input / Output:
Input:
7
Output:
Prime
18. First and Last Digit
Code:
n = int(input("Enter number: "))
last = n % 10
while n >= 10:
n //= 10
print("First digit =", n)
print("Last digit =", last)
Sample Input / Output:
Input:
4567
Output:
First digit = 4
Last digit = 7
19. Print Name N Times
Code:
name = input("Enter name: ")
n = int(input("Enter times: "))
for i in range(n):
print(name)
Sample Input / Output:
Input:
VVIT
3
Output:
VVIT
VVIT
VVIT
20. Triangle Pattern
Code:
text = input("Enter text: "))
for i in range(1, len(text)+1):
print(text[:i])
Sample Input / Output:
Input:
VVIT
Output:
V
VV
VVI
VVIT