1.
Addition of Two Numbers
a=int(input('Enter first number: '))
b=int(input('Enter second number: '))
print('Sum =',a+b)
Input/Output Examples:
Input: 5 7
Output: Sum = 12
Input: -3 9
Output: Sum = 6
2. Average of Three Numbers
a=int(input('Enter first: '))
b=int(input('Enter second: '))
c=int(input('Enter third: '))
print('Average =',(a+b+c)/3)
Input/Output Examples:
Input: 3 6 9
Output: Average = 6.0
3. Largest of Two Numbers
a=int(input('Enter first: '))
b=int(input('Enter second: '))
if a>b:
print('Largest:',a)
else:
print('Largest:',b)
Input/Output Examples:
Input: 4 9
Output: Largest: 9
4. Swap Two Numbers
a=int(input('Enter a: '))
b=int(input('Enter b: '))
a,b=b,a
print('After swap:',a,b)
Input/Output Examples:
Input: 5 8
Output: After swap: 8 5
5. Check Even or Odd
n=int(input('Enter number: '))
if n%2==0:
print('Even')
else:
print('Odd')
Input/Output Examples:
Input: 7
Output: Odd
Input: 12
Output: Even
6. Factorial using Loop
n=int(input('Enter n: '))
f=1
for i in range(1,n+1):
f*=i
print('Factorial=',f)
Input/Output Examples:
Input: 5
Output: Factorial=120
7. Fibonacci Series
n=int(input('Enter terms: '))
a,b=0,1
for i in range(n):
print(a,end=' ')
a,b=b,a+b
Input/Output Examples:
Input: 6
Output: 0 1 1 2 3 5
8. Prime Number Check
n=int(input('Enter number: '))
flag=1
for i in range(2,n):
if n%i==0:
flag=0
break
if flag and n>1:
print('Prime')
else:
print('Not Prime')
Input/Output Examples:
Input: 7
Output: Prime
Input: 12
Output: Not Prime
9. Palindrome Number
n=int(input('Enter number: '))
t=n
rev=0
while t>0:
rev=rev*10+t%10
t//=10
if rev==n:
print('Palindrome')
else:
print('Not Palindrome')
Input/Output Examples:
Input: 121
Output: Palindrome
Input: 123
Output: Not Palindrome
10. Armstrong Number
n=int(input('Enter number: '))
t=n
s=0
while t>0:
d=t%10
s+=d**3
t//=10
if s==n:
print('Armstrong')
else:
print('Not Armstrong')
Input/Output Examples:
Input: 153
Output: Armstrong
11. Sum of Digits
n=int(input('Enter number: '))
s=0
while n>0:
s+=n%10
n//=10
print('Sum of digits=',s)
Input/Output Examples:
Input: 1234
Output: 10
12. Reverse a Number
n=int(input('Enter number: '))
rev=0
while n>0:
rev=rev*10+n%10
n//=10
print('Reversed=',rev)
Input/Output Examples:
Input: 987
Output: 789
13. Multiplication Table
n=int(input('Enter number: '))
for i in range(1,11):
print(n,'x',i,'=',n*i)
Input/Output Examples:
Input: 3
Output: 3x1=3 ... 3x10=30
14. Square Pattern
n=int(input('Enter size: '))
for i in range(n):
print('* '*n)
Input/Output Examples:
Input: 4
Output:
* * * *
* * * *
* * * *
* * * *
15. Right Triangle Pattern
n=int(input('Enter size: '))
for i in range(1,n+1):
print('* '*i)
Input/Output Examples:
Input: 5
Output:
*
* *
* * *
* * * *
* * * * *
16. Inverted Triangle
n=int(input('Enter size: '))
for i in range(n,0,-1):
print('* '*i)
Input/Output Examples:
Input: 4
Output:
* * * *
* * *
* *
*
17. Pyramid Pattern
n=int(input('Enter size: '))
for i in range(1,n+1):
print(' '*(n-i)+'* ' * i)
Input/Output Examples:
Input: 4
Output:
*
* *
* * *
* * * *
18. Number Triangle
n=int(input('Enter size: '))
for i in range(1,n+1):
for j in range(1,i+1):
print(j,end=' ')
print()
Input/Output Examples:
Input: 5
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
19. Floyd's Triangle
n=int(input('Enter rows: '))
num=1
for i in range(1,n+1):
for j in range(i):
print(num,end=' ')
num+=1
print()
Input/Output Examples:
Input: 5
Output:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
20. Hollow Square
n=int(input('Enter size: '))
for i in range(n):
for j in range(n):
if i==0 or i==n-1 or j==0 or j==n-1:
print('*',end=' ')
else:
print(' ',end=' ')
print()
Input/Output Examples:
Input: 4
Output:
* * * *
* *
* *
* * * *
21. Diamond Pattern
n=int(input('Enter size: '))
for i in range(1,n+1):
print(' '*(n-i)+'* ' * i)
for i in range(n-1,0,-1):
print(' '*(n-i)+'* ' * i)
Input/Output Examples:
Input: 3
Output:
*
* *
* * *
* *
*
22. Sum of N Natural Numbers
n=int(input('Enter n: '))
s=0
for i in range(1,n+1):
s+=i
print('Sum=',s)
Input/Output Examples:
Input: 5
Output: Sum=15
23. Check Leap Year
y=int(input('Enter year: '))
if (y%4==0 and y%100!=0) or y%400==0:
print('Leap Year')
else:
print('Not Leap Year')
Input/Output Examples:
Input: 2024
Output: Leap Year
Input: 2023
Output: Not Leap Year
24. GCD of Two Numbers
a=int(input('Enter a: '))
b=int(input('Enter b: '))
while b:
a,b=b,a%b
print('GCD=',a)
Input/Output Examples:
Input: 12 18
Output: GCD=6
25. LCM of Two Numbers
a=int(input('Enter a: '))
b=int(input('Enter b: '))
import math
print('LCM=',abs(a*b)//math.gcd(a,b))
Input/Output Examples:
Input: 4 6
Output: LCM=12
26. Count Digits
n=int(input('Enter number: '))
c=0
while n>0:
c+=1
n//=10
print('Digits=',c)
Input/Output Examples:
Input: 12345
Output: Digits=5
27. Power Calculation
a=int(input('Enter base: '))
b=int(input('Enter exponent: '))
print('Result=',a**b)
Input/Output Examples:
Input: 2 5
Output: 32
28. Sum of Even Numbers up to N
n=int(input('Enter n: '))
s=0
for i in range(2,n+1,2):
s+=i
print('Sum of evens=',s)
Input/Output Examples:
Input: 10
Output: 30
29. Factor Check
n=int(input('Enter number: '))
print('Factors:')
for i in range(1,n+1):
if n%i==0:
print(i,end=' ')
Input/Output Examples:
Input: 12
Output: 1 2 3 4 6 12
30. Star Hourglass Pattern
n=int(input('Enter size: '))
for i in range(n,0,-1):
print(' '*(n-i)+'* '*i)
for i in range(2,n+1):
print(' '*(n-i)+'* '*i)
Input/Output Examples:
Input: 4
Output:
* * * *
* * *
* *
*
* *
* * *
* * * *