0% found this document useful (0 votes)
37 views

Calculator Cod

The document provides two examples of code for building a simple calculator program in Python. The first uses if-else statements to perform calculations based on the operator entered by the user. The second uses functions defined for each operation type instead of if-else statements. Both examples take input from the user, perform the calculation, and output the result. The examples are meant as a starting point and do not cover all cases or error handling for a full-featured calculator program.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Calculator Cod

The document provides two examples of code for building a simple calculator program in Python. The first uses if-else statements to perform calculations based on the operator entered by the user. The second uses functions defined for each operation type instead of if-else statements. Both examples take input from the user, perform the calculation, and output the result. The examples are meant as a starting point and do not cover all cases or error handling for a full-featured calculator program.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Certainly!

Here are two examples of code for making a simple calculator in Python:

Example 1: Simple Calculator using if-else statements

```

# This is a simple calculator program using if-else statements in Python

# Take input from the user

num1 = float(input("Enter the first number: "))

operator = input("Enter the operator (+, -, *, /): ")

num2 = float(input("Enter the second number: "))

# Perform calculation based on operator

if operator == '+':

result = num1 + num2

elif operator == '-':

result = num1 - num2

elif operator == '*':

result = num1 * num2

elif operator == '/':

result = num1 / num2

else:

print("Invalid operator. Please try again.")

exit()

# Display the result

print("Result: " + str(result))

```
Example 2: Simple Calculator using functions

```

# This is a simple calculator program using functions in Python

# Function to add two numbers

def add(x, y):

return x + y

# Function to subtract two numbers

def subtract(x, y):

return x - y

# Function to multiply two numbers

def multiply(x, y):

return x * y

# Function to divide two numbers

def divide(x, y):

return x / y

# Take input from the user

num1 = float(input("Enter the first number: "))

operator = input("Enter the operator (+, -, *, /): ")

num2 = float(input("Enter the second number: "))

# Perform calculation based on operator

if operator == '+':

result = add(num1, num2)


elif operator == '-':

result = subtract(num1, num2)

elif operator == '*':

result = multiply(num1, num2)

elif operator == '/':

result = divide(num1, num2)

else:

print("Invalid operator. Please try again.")

exit()

# Display the result

print("Result: " + str(result))

```

Note that these examples are just simple calculators and do not cover all possible cases or error
handling. They should be used as a starting point for building a more comprehensive calculator program.

You might also like