Basic Programs Notes
1. Write a Python program that prompts the user to enter the principal amount,
the annual interest rate, and the number of years to compute the compound
interest.
Use the formula:
Total Amount
A = P(1+r/100)**t
Note: Assume that the interest is compounded once per year.
Test case 1
Sample Input:
1000.0
5.0
2
Sample Output:
1102.50
p = float(input())
r = float(input())
t = int(input())
rate = (r/100)
CI = p*(1+rate)**t
print("%0.2f"%CI)
2. Python program to print Largest Among three numbers
a,b,c = map(int,input().split())
if (a>b) and (b>c):
print(a,"is Greatest")
elif (b > c):
print(b,"is Greatest")
Basic Programs Notes 1
else:
print(c,"is Greatest")
3. Program to check whether given number is prime or not
Brute Force Approach
n = int(input())
if n <= 1:
print("Not Prime")
else:
cnt = 0
for i in range(1,n+1):
if n%i == 0:
cnt += 1
if cnt == 2:
print("Prime Number")
else:
print("Not a Prime Number")
# TC: O(n)
Optimised Approach
n = int(input())
if n <= 1:
print("Not Prime")
else:
is_prime = True
for i in range(2,int(n**0.5)+1):
if n%i == 0:
is_prime = False
break
if is_prime:
print("Prime Number")
Basic Programs Notes 2
else:
print("Not a Prime Number")
#TC: O(sqrt(n))
4. Program to print Factorial of a number
n = int(input())
fact = 1
for i in range(1,n+1):
fact = fact*i
print(fact)
#TC: O(n)
5. Program to print fibonaccci of a number
n = int(input())
a,b = 0,1
print(a,b,end=" ")
for i in range(2,n):
c = a+b
a=b
b=c
print(c,end=" ")
#TC: O(n)
6. Progam to print Armstrong number or not
Basic Programs Notes 3
n = int(input())
temp = n
res = 0
cnt = len(str(n))
while n != 0:
rem = n%10
res += (rem**cnt)
n = n//10
if res == temp:
print("Armstrong Number")
else:
print("Not an Armstrong Number")
#TC: O(n*logn)
7. Program to print GCD of a number
Brute Force Approach
a,b = map(int,input().split())
gcd = 1
for i in range(1,min(a,b)+1):
if (a%i == 0) and (b%i == 0):
gcd = i
print(gcd)
#TC: O(n)
Optimised Approach by using Subtraction Method
a,b = map(int,input().split())
while a != b:
if a> b:
a = a-b
Basic Programs Notes 4
else:
b = b-a
print(a)
#TC: O(max(a,b))
Eucledian Approach (Efficient)
a,b = map(int,input().split())
while b != 0:
a,b = b,a%b
print(a)
#TC: O(logn)
8. Program to print LCM of a number
Brute Force
a,b = map(int,input().split())
large = max(a,b)
while True:
if large%a == 0 and large%b == 0:
lcm = large
break
large += 1
print(lcm)
#TC: O(a*b)
Optimised Approach by using GCD Eucledian Method
Basic Programs Notes 5
def gcd(a,b):
while b!= 0:
a,b = b,a%b
return a
a,b = map(int,input().split())
lcm = a*b//gcd(a,b)
print(lcm)
#TC: O(logn)
9. Program to print sum of digit of a given number
n = int(input())
tot = 0
while n != 0:
rem = n%10
tot += rem
n = n//10
print(tot)
#TC: O(logn)
10. Program to print Reverse of a number
Brute Force
n = int(input())#123
st = str(n) #"123"
st = st[::-1]
print(int(st))
#TC:O(n), SC: O(n)
Optimised
Basic Programs Notes 6
n = int(input())
rev = 0
while n != 0:
rem = n%10
rev = (rev*10)+rem
n = n//10
print(rev)
#TC:O(logn), SC:O(1)
11. program to compute power of a number
Brute force
x,n = map(int,input().split())
res = 1
for i in range(1,n+1):
res = res*x
print(res)
#TC: O(n)
#Power of a Number (Exponential by Squaring)
x,n = map(int,input().split())
res = 1
while n > 0:
if n%2 == 1:
res = res*x
x = x*x
n = n//2
print(res)
#TC:O(logn)
Basic Programs Notes 7
12. Program to read Elements from an array
Reading elements in single line
L = list(map(int,input().split()))
print(L)
print(*L)
Reading elements with append operation
L = list(map(int,input().split()))
print(L)
print(*L)
13. Program to Print Sum of elements in an array
without inbuilt methods
n = int(input("n = "))
L = [] #[]
tot = 0
for i in range(n): #1 < 5
el =int(input())
[Link](el)
for i in range(n):
tot += L[i]
print(tot)
With inbuilt method
n = int(input("n = "))
L = [] #[]
for i in range(n): #1 < 5
el =int(input())
Basic Programs Notes 8
[Link](el)
print(sum(L))
14. Program to print Max Element of an array
Without inbuilt method
L = list(map(int,input().split()))
large = L[0]
for i in range(1,len(L)):
if L[i] > large:
large = L[i]
print(large)
With inbuilt method
L = list(map(int,input().split()))
print(max(L))
Basic Programs Notes 9