Git Product home page Git Product logo

calc's Introduction

Calculator Program

Importing Modules

import math

Defining Functions

def add(x, y): return x + y

def sub(x, y): return x - y

def mul(x, y): return x * y

def div(x, y): return x / y

def sqrt(x): return math.sqrt(x)

def power(x, y): return math.pow(x, y)

Main Program

print("Select Operation.") print("1. Addition") print("2. Subtraction") print("3. Multiplication") print("4. Division") print("5. Square Root") print("6. Power") print("E. Exit") print("R. Restart")

while True: choice = input("Enter Choice (1/2/3/4/5/6/E/R): ")

if choice in ('1', '2', '3', '4', '5', '6'):
    num1 = float(input("Enter First Number: "))
    num2 = float(input("Enter Second Number: "))

    if choice == '1':
        print(num1, "+", num2, "=", add(num1, num2))

    elif choice == '2':
        print(num1, "-", num2, "=", sub(num1, num2))

    elif choice == '3':
        print(num1, "*", num2, "=", mul(num1, num2))

    elif choice == '4':
        print(num1, "/", num2, "=", div(num1, num2))

    elif choice == '5':
        print("Square Root of", num1, "=", sqrt(num1))
        print("Square Root of", num2, "=", sqrt(num2))

    elif choice == '6':
        print(num1, "to the power of", num2, "=", power(num1, num2))

elif choice.upper() == 'E':
    confirm = input("Are you sure you want to exit? (Y/N): ")
    if confirm.upper() == 'Y':
        print("Exiting...")
        break
    elif confirm.upper() == 'N':
        print("Select Operation.")
        print("1. Addition")
        print("2. Subtraction")
        print("3. Multiplication")
        print("4. Division")
        print("5. Square Root")
        print("6. Power")
        print("E. Exit")
        print("R. Restart")
    else:
        print("Invalid Input")

elif choice.upper() == 'R':
    print("Restarting...")
    print("Select Operation.")
    print("1. Addition")
    print("2. Subtraction")
    print("3. Multiplication")
    print("4. Division")
    print("5. Square Root")
    print("6. Power")
    print("E. Exit")
    print("R. Restart")

else:
    print("Invalid Input")        

calc's People

Contributors

giantbi avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.