Simple Programs

Q1 Python Program to find the area of triangle

a = float(input(‘Enter first side: ‘)) b = float(input(‘Enter second side: ‘)) c = float(input(‘Enter third side: ‘)) s = (a + b + c) / 2                                                # calculate the semi-perimeter area = (s*(s-a)*(s-b)*(s-c)) ** 0.5)                           #calculate the area
print(‘The area of the triangle is “,area)

Q2. Python Program to calculate the square root

num = float(input(‘Enter a number: ‘))

num_sqrt = num ** 0.5
print(‘The square root of “,num,”is”,num_sqrt))

Q3 Python program to swap two variables

x = input(‘Enter value of x: ‘) y = input(‘Enter value of y: ‘) x,y=y,x print(‘The value of x after swapping’,x) print(‘The value of y after swapping’,y)

4. Program to calculate x to the power y

x=int(input(“enter first no “))
y=int(input(“enter second no “))
res=x**y
print((“The result of base %d and power %d is %d”) %(x,y,res))

5. Program to get selling price and GST rate and then print Invoice along with both GST and SGST.

Item=input(“Enter Item Name:”) SP=float(input(“enter selling price of Item”)) gstrate=float(input(“Enter GST rate”)) cgst=SP*((gstrate/2)/100) sgst=cgst amount=SP+cgst+sgst print(“\t Invoice”) print(“Item:”, item) print(“Price :”,SP) print(“CGST (@”,(gstrate/2), “%): “,cgst) print(“SGST (@”,(gstrate/2), “%): “,sgst) print(“Amount payable :”,amount)

6.  Program to find the Sum of series 2+ (2+4) + (2+4+6)… Source Code:

a=int(input(“Enter the no of terms”)) x=2 y=[] cnt=0 sum=0 for i in range(0,a): y.append(x) x=x+2 cnt=cnt+1 for i in range(0,cnt): y.append(“%d”%x) x=x+2 sum=sum+int(y[i]) print((“The sum of series is : %d”)%(sum))

7. Code to print Formatted Values

a=12
b=123
c=”Sita”
d=”RadhaKrishnan”
e=45.567
f=1.6
print(“%5d %15s %7.5f”%(a,c,e))
print(“%5d %15s %7.5f”%(a,d,f))

OUTPUT :

12 Sita 45.56700
12 RadhaKrishnan 1.60000

Advertisement