[Go to site: main page, start]

0% found this document useful (0 votes)
6 views2 pages

Python Math Functions and Calculations

The document contains a series of Python code snippets for various mathematical calculations and checks. These include calculating the area and perimeter of a rectangle, hypotenuse and area of a right triangle, area of a circle, temperature conversion, finding the highest common factor (HCF), checking for prime numbers, reversing digits, summing and multiplying digits, determining leap years, and summing the first n natural numbers. Each snippet prompts the user for input and prints the result.

Uploaded by

itsmehacker8053
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Python Math Functions and Calculations

The document contains a series of Python code snippets for various mathematical calculations and checks. These include calculating the area and perimeter of a rectangle, hypotenuse and area of a right triangle, area of a circle, temperature conversion, finding the highest common factor (HCF), checking for prime numbers, reversing digits, summing and multiplying digits, determining leap years, and summing the first n natural numbers. Each snippet prompts the user for input and prints the result.

Uploaded by

itsmehacker8053
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

Area and perimeter of rectangle


l = float(input("Enter length: "))
b = float(input("Enter breadth: "))
print("Area =", l*b)
print("Perimeter =", 2*(l+b))

2. Hypotenuse & area of right triangle


import math
base = float(input("Base: "))
height = float(input("Height: "))
hyp = [Link](base**2 + height**2)
area = 0.5 * base * height
print("Hypotenuse =", hyp)
print("Area =", area)

3. Area of circle
import math
r = float(input("Radius: "))
print("Area =", [Link] * r * r)

4. Celsius to Fahrenheit
c = float(input("Celsius: "))
f = (c * 9/5) + 32
print("Fahrenheit =", f)

5. HCF of two numbers


a = int(input("a: "))
b = int(input("b: "))
while b:
a, b = b, a % b
print("HCF =", a)

6. Check prime
n = int(input("Enter number: "))
flag = True
if n < 2:
flag = False
else:
for i in range(2, int(n**0.5)+1):
if n % i == 0:
flag = False
break
print("Prime" if flag else "Not prime")

7. Reverse digits
num = int(input("Enter number: "))
rev = 0
while num > 0:
d = num % 10
rev = rev*10 + d
num //= 10
print("Reversed =", rev)

8. Sum of digits
num = int(input("Enter number: "))
s = 0
while num > 0:
s += num % 10
num //= 10
print("Sum of digits =", s)

9. Product of digits
num = int(input("Enter number: "))
p = 1
while num > 0:
p *= (num % 10)
num //= 10
print("Product of digits =", p)

10. Leap year


year = int(input("Enter year: "))
if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
print("Leap year")
else:
print("Not leap year")

12. Sum of first n natural numbers


n = int(input("n: "))
print("Sum =", n*(n+1)//2)

You might also like