Create a Calculator using Python

 Calculator using Python - 2022


This Python program is a versatile calculator that offers a variety of mathematical and geometric operations. It provides the user with a menu of options to choose from, each corresponding to a specific operation. 

1. Addition: For this option, the program allows the user to input multiple numbers, and it calculates and displays their sum.

2. Subtraction: Here, the user inputs two numbers, and the program computes and shows the result of the subtraction.

3. Multiplication: The user can input multiple numbers for multiplication, and the program calculates and displays their product.

4. Division: This option enables the user to input two numbers for division, and the program computes and displays the quotient.

5. X to the power Y: The user is prompted to input two numbers, X and Y. The program then calculates and displays the result of X raised to the power of Y.

6. Solve a quadratic equation: The program assists the user in solving a quadratic equation by asking for the coefficients of the equation. It then computes and displays the roots, considering various cases, including fractional roots.

7. Calculate AREA and PERIMETER: This menu item provides a range of geometric shapes for which the user can compute both the area and perimeter. Shapes include square, rectangle, circle, triangle, and parallelogram. The program requests relevant input parameters, performs the calculations, and presents the results.

8. Calculate VOLUME: Similar to the geometric operations, this option offers volume calculations for shapes like sphere, cube, cuboid, cylinder, cone, and prism. The user provides necessary measurements, and the program calculates and displays the volume.

9. Factors of any number: The program helps the user find the factors of a given number. It generates a list of factors for the input number.

10. Generate Table of any given number: The user can choose a number for which they want to generate a multiplication table. The program prints the multiplication table for the chosen number.

11. Find Quotient and Remainder: For this option, the user inputs two numbers, and the program calculates and displays both the quotient and remainder.

The program is quite comprehensive, covering a wide range of mathematical and geometric operations. It's designed to be a helpful tool for mathematical calculations and serves as a learning opportunity for those looking to explore Python programming, especially in the context of mathematical applications. Users can input their values for the various operations, making it versatile and user-friendly.
Code:
import math
from fractions import Fraction

#Menu shown to the user
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
print("5. X to the power Y")
print("6. Solve a quadratic equation")
print("7. Calculate AREA and PERIMETER")
print("8. Calculate VOLUME")
print("9. Factors of any number")
print("10. Generate Table of any given number")
print("11. Find Quotient and Remainder")
print("_"*60)
opr = int(input("Enter the number of operation : ")) #Input from user


#Addition 
if opr == 1:
    nu = int(input("How many numbers you want to add :")) 
    sum = 0
    for i in range(nu):
        num1=int(input("Enter a number: "))
        sum+=num1

    print(sum)



#Subtraction
elif opr == 2:
    num1 = int(input("Enter a number : "))
    num2 = int(input("Enter another number : "))
    sub = num1 - num2
    print(sub)


#Multiplication
elif opr == 3:
    n = int(input("How many numbers you want to multiply : "))
    prod = 1
    for i in range(n):
        num1=int(input("Enter a number : "))
        prod*=num1

    print(prod)

    
#Division
elif opr == 4:
    num1 = int(input("Enter a number : "))
    num2 = int(input("Enter a number : "))
    divi = num1/num2
    print(divi)

    
#X to the power Y
elif opr == 5:
    X = int(input("Type X: "))
    Y = int(input("Type Y: "))
    srt = 1
    for i in range(Y):
        srt*=X

    print(srt)

    
#Roots of quadratic equation
elif opr == 6:
    a = int(input("Enter Coefficient of x square: "))
    b = int(input("Enter Coefficent of x: "))
    c = int(input("Enter the number: "))

    bsqr = b*b
    ssd = 4*a*c
    D = bsqr - ssd
    a2 = 2*a
    dsqrt = math.sqrt(D)
    rt1n = -b + dsqrt
    rt2n = -b - dsqrt   
    root1 = rt1n/a2
    root2 = rt2n/a2

    n = root1.is_integer()
    n2 = root2.is_integer()
    if n == True and n2 == True:
       print("The roots of the equation are ",root1,root2)

    elif n == True and n2 == False:
      s = Fraction(root2)
      print("The roots of the equation are ",root1,s)

    elif n == False and n2 == True:
      t = Fraction(root1)
      print("The roots of the equation are ",t,root2)

    elif n == False and n2 == False:
      y = Fraction(root1)
      j = Fraction(root2)
      print("The roots of the equation are ",y,j)

      
#AREA and PERIMETER
elif opr == 7:
    print("1. Square")
    print("2. Rectangle")
    print("3. Circle")
    print("4. Triangle")
    print("5. Paralellogram")
    shp = int(input("Enter number of shape: "))

    if shp == 1:
        sd = int(input("Enter side of square: "))
        peri = 4*sd
        area = sd*sd
        print("Area:",area,"Perimeter:",peri)

    elif shp == 2:
        l = int(input("Enter length: "))
        b = int(input("Enter breadth: "))
        area = l*b
        lb = l + b
        peri = 2*lb
        print("Area",area,"Perimeter",peri)

    elif shp == 3:
        r = int(input("Enter Radius: "))
        pi = 3.14
        circum = 2*pi*r
        area = pi*r*r
        print("Area:",area,"Circumference:",circum)

    elif shp == 4:
        s1 = int(input("Enter side: "))
        s2 = int(input("Enter another side: "))
        h = int(input("Enter height: "))
        b = int(input("Enter base: "))
        peri = s1+s2+b
        area = 0.5*b*h
        print("Area:",area,"Perimeter:",peri)

    elif shp == 5:
        s1 = int(input("Enter side: "))
        h = int(input("Enter height: "))
        b = int(input("Enter base: "))
        area = b*h
        s1b = s1 + b
        peri = 2*s1b
        print("Area",area,"Perimeter:",peri)

        
#VOLUME of shapes
elif opr == 8:
    print("1. Sphere")
    print("2. Cube")
    print("3. Cuboid")
    print("4. Cylinder")
    print("5. Cone")
    print("6. Prism")
    sh = int(input("Enter number of shape: "))
    pi = 3.14

    if sh == 1:
        r = int(input("Enter radius: "))
        V = 1.33*pi*r*r*r
        print("Volume:",V)

    elif sh == 2:
        side = int(input("Enter side: "))
        V = side*side*side
        print("Volume",V)

    elif sh == 3:
        l = int(input("Enter Length: "))
        b = int(input("Enter Breadth: "))
        h = int(input("Enter Height: "))
        V = l*b*h
        print("Volume:",V)

    elif sh == 4:
        r = int(input("Enter Radius: "))
        h = int(input("Enter Height: "))
        V = pi*r*r*h
        print("Volume",V)

    elif sh == 5:
        r = int(input("Enter Radius: "))
        h = int(input("Enter Height: "))
        V = pi*r*r*h
        V1 = V/3
        print("Volume: ",V1)

    elif sh == 6:
        l = int(input("Enter Length: "))
        b = int(input("Enter Breadth: "))
        h = int(input("Enter Height: "))
        V = l*b*h
        V1 = V/3
        print("Volume: ",V1)

        
#Factors of any given number
elif opr == 9:
    num1 = int(input("Enter a number: "))
    list1 = []
    divisor = 0
    if num1 == 0:
        print("Not defined")

    else:
         for i in range(1,100000) :
                divisor+=1
                if num1%divisor == 0 :
                 list1.append(divisor)
         print(list1)

     
#Table Generator
elif opr == 10:
    def myfunction():
     num = int(input("Enter a number: ")) #table generator
     a = 0
     for i in range(1,11):
        a+=1
        print(num,'x',a,'=',num*a)

    myfunction()

    
#Quotient and Remainder
elif opr == 11:
    num1 = int(input("Enter  a number: "))
    num2 = int(input("Enter another number: "))
    quo = num1//num2
    rem = num1%num2
    print("Quotient: ",quo,"Remainder: ",rem)

Post a Comment

Previous Post Next Post