Python Programs Based on Mathematics


Pythonpedia
Python Programs Based on Mathematics

1.Program to find the area of triangle?
Program:-
Pythonpedia
Code:-

import math

a=int(input("Enter first side: "))

b=int(input("Enter second side: "))

c=int(input("Enter third side: "))

s=(a+b+c)/2

area=math.sqrt(s*(s-a)*(s-b)*(s-c))

print("Area of the triangle is: ",round(area,2))

Output:-
Pythonpedia
Enter first side: 9

Enter second side: 7

Enter third side: 5

Area of the triangle is:  17.41

2.Program to convert height in centimeter to feet and inches?

Program:-
Pythonpedia
Code:-

cm=int(input("Enter the height in centimeters:"))

inches=0.394*cm

feet=0.0328*cm

print("The length in inches",round(inches,2))

print("The length in feet",round(feet,2))

Output:-
Pythonpedia
Enter the height in centimeters:165

The length in inches 65.01

The length in feet 5.41

3.Program to find LCM of two numbers entered?

Program:-
Pythonpedia
Code:-

a=int(input("Enter the first number:"))

b=int(input("Enter the second number:"))

if(a>b):

    min1=a

else:

    min1=b

while(1):

    if(min1%a==0 and min1%b==0):

        print("LCM is:",min1)

        break

    min1=min1+1

Output:-
Pythonpedia
Enter the first number:8

Enter the second number:9

LCM is: 72

4.Program to find the sum of first n Natural numbers?

Program:-
Pythonpedia
Code:-

n=int(input("Enter a number: "))

sum1 = 0

while(n > 0):

    sum1=sum1+n

    n=n-1

print("The sum of first n natural numbers is",sum1)

Output:-
Pythonpedia
Enter a number: 9

The sum of first n natural numbers is 45

5.Program to calculate the simple interest?

Program:-
Pythonpedia
Code:-

principle=float(input("Enter the principle amount:"))

time=int(input("Enter the time(years):"))

rate=float(input("Enter the rate:"))

simple_interest=(principle*time*rate)/100

print("The simple interest is:",simple_interest)

Output:-
Pythonpedia
Enter the principle amount:2000

Enter the time(years):10

Enter the rate:60

The simple interest is: 12000.0

6. Program to Print the Pascal’s triangle for n number of rows given by the user?

Program:-
Pythonpedia
Code:-

n=int(input("Enter number of rows: "))

a=[]

for i in range(n):

    a.append([])

    a[i].append(1)

    for j in range(1,i):

        a[i].append(a[i-1][j-1]+a[i-1][j])

    if(n!=0):

        a[i].append(1)

for i in range(n):

    print("   "*(n-i),end=" ",sep=" ")

    for j in range(0,i+1):

        print('{0:6}'.format(a[i][j]),end=" ",sep=" ")

    print()

Output:-
Pythonpedia
Enter number of rows: 6

                        1

                     1      1

                  1      2      1

               1      3      3      1

            1      4      6      4      1

         1      5     10     10      5      1

7. Program to Compute the Value of Euler’s Number e. Use the Formula: e = 1 + 1/1! + 1/2! + …… 1/n!

Program:-
Pythonpedia
Code:-

import math

n=int(input("Enter the number of terms: "))

sum1=1

for i in range(1,n+1):

    sum1=sum1+(1/math.factorial(i))

print("The sum of series is",round(sum1,2))

Output:-
Pythonpedia
Enter the number of terms: 6

The sum of series is 2.72

-Thank you

Comments

Post a Comment

Popular posts from this blog

Cyber Security