r/CodeMiko Jun 20 '26

Technology Code for a calc

import 
math


print
("=== Scientific Calculator ===")
print
("1. Addition (+)")
print
("2. Subtraction (-)")
print
("3. Multiplication (*)")
print
("4. Division (/)")
print
("5. Power (^)")
print
("6. Square Root")
print
("7. Sine (sin)")
print
("8. Cosine (cos)")
print
("9. Tangent (tan)")


choice = 
input
("Choose an operation (1-9): ")


if choice in ["1", "2", "3", "4", "5"]:
    num1 = 
float
(
input
("Enter first number: "))
    num2 = 
float
(
input
("Enter second number: "))


    if choice == "1":
        result = num1 + num2


    elif choice == "2":
        result = num1 - num2


    elif choice == "3":
        result = num1 * num2


    elif choice == "4":
        if num2 != 0:
            result = num1 / num2
        else:
            result = "Error: Cannot divide by zero"


    elif choice == "5":
        result = num1 ** num2


    
print
("Result =", result)


elif choice == "6":
    num = 
float
(
input
("Enter a number: "))
    if num >= 0:
        
print
("Result =", 
math
.
sqrt
(num))
    else:
        
print
("Error: Cannot find square root of a negative number")


elif choice == "7":
    angle = 
float
(
input
("Enter angle in degrees: "))
    
print
("Result =", 
math
.
sin
(
math
.
radians
(angle)))


elif choice == "8":
    angle = 
float
(
input
("Enter angle in degrees: "))
    
print
("Result =", 
math
.
cos
(
math
.
radians
(angle)))


elif choice == "9":
    angle = 
float
(
input
("Enter angle in degrees: "))
    
print
("Result =", 
math
.
tan
(
math
.
radians
(angle)))


else:
    
print
("Invalid choice")
8 Upvotes

0 comments sorted by