Thursday, 16 December 2021

To find the sum of squares of the first 100 natural numbers.

 sum of squares of 5 numbers:

12+22+32+42+52

Example 1: sum of squares of n numbers using for loop

n = int(input("Enter nth number : "))
sum = 0
for s in range(1, n+1):
sum = sum + (s*s)
print("Sum of squares is : ", sum)

Output:

Enter nth number : 3
Sum of squares is : 14

Enter nth number : 10
Sum of squares is : 385

To find the third largest/smallest number in a list.

 

Python program :

#1
num = [2,3,7,4,5,6,10,11,120]

#2
largest_num = num[0]
second_largest_num = num[0]
third_largest_num = num[0]

#3
for i in num :
    #4
    if i > largest_num :
        third_largest_num = second_largest_num
        second_largest_num = largest_num
        largest_num = i
    #5
    elif i > second_largest_num :
        third_largest_num = second_largest_num
        second_largest_num = i
    #6
    elif i > third_largest_num :
        third_largest_num = i

#7
print("Third largest number of the list is {}".format(third_largest_num))

Output :

Third largest number of the list is 10

To find the largest and smallest numbers in a list.

 lst = []

num = int(input('How many numbers: '))for n in range(num):
numbers = int(input('Enter number '))
lst.append(numbers)
print("Maximum element in the list is :", max(lst), "\nMinimum element in the list is :", min(lst))

Output :

Smallest and Largest element in list - programminginpython.com

To calculate tax - GST / Income Tax.

 

Program code to calculate the GST

  • Example 1
Original_price = 100
Net_price = 124.7
GST_amount = Net_price - Original_price
GST_percent = ((GST_amount * 100) / Original_price)
print("GST = ",end='')
print(round(GST_percent),end='')
print("%")

Output:- 

GST = 25%

To calculate EMI for Amount, Period and Interest.

 # Monthly Interest Rate (r) = R/(12*100)


# Varaible name details:
# p = Principal or Loan Amount
# r = Interest Rate Per Month
# n = Number of monthly installments

# Reading inputs from user
p = float(input("Enter principal amount: "))
R = float(input("Enter annual interest rate: "))
n = int(input("Enter number of months: " ))

# Calculating interest rate per month
r = R/(12*100)

# Calculating Equated Monthly Installment (EMI)
emi = p * r * ((1+r)**n)/((1+r)**n - 1)

print("Monthly EMI = ", emi)

Output

Enter principal amount: 1000000
Enter annual interest rate: 7
Enter number of months: 120
Monthly EMI =  11610.847921862374

Sunday, 14 November 2021

To calculate profit-loss for a given Cost and Sell Price.

 # Python Program to Calculate Profit or Loss

 
actual_cost = float(input(" Please Enter the Actual Product Price: "))
sale_amount = float(input(" Please Enter the Sales Amount: "))
 
if(actual_cost > sale_amount):
    amount = actual_cost - sale_amount
    print("Total Loss Amount = {0}".format(amount))
elif(sale_amount > actual_cost):
    amount = sale_amount - actual_cost
    print("Total Profit = {0}".format(amount))
else:
    print("No Profit No Loss!!!")

Python profit or loss output

 Please Enter the Actual Product Price: 1500
 Please Enter the Sales Amount: 1200
Total Loss Amount = 300.0

To calculate Simple and Compound interest.

 p = int(input("Enter principal amount = ")) 

r = int(input("Enter the rate = "))
t = int(input("Enter time  = "))
si = (p * r * t ) / 100
com = p * ( (1 + (r / 100 ))** t ) - p
print("Simple interest = ",si)
print("Compound interest = ",com)