0% found this document useful (0 votes)
21 views5 pages

EXPT6

Uploaded by

jbadgujar730
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)
21 views5 pages

EXPT6

Uploaded by

jbadgujar730
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/ 5

AIM:-To simulate simple calculator that performs basic tasks such as

addition,subtraction, multiplication and division with special operations


like computing x and x!.

THEORY:-

ADDITION:- Performing addition in Python is straightforward. You can use


the + operator to add numbers together.

SUBTRACTION:- Performing subtraction in Python is also straightforward,


using the - operator.

DIVISION:- Performing division in Python is easy, using the / operator for


floating-point division or the // operator for integer (floor) division.

MULTIPLICATION:- Performing multiplication in Python is simple using the *


operator.

ALGORITHM:-

 Start

 Display the menu of operations:

 Addition (+)

 Subtraction (-)

 Multiplication (*)

 Division (/)

 Exponentiation (x^y)

 Factorial (x!)

 Prompt the user to choose an operation (1-6).

 If operation is 1 (Addition):

 Input: Read num1 and num2.

 Calculate: result = num1 + num2.

 Output: Display the result.

 If operation is 2 (Subtraction):

 Input: Read num1 and num2.

 Calculate: result = num1 - num2.

 Output: Display the result.

 If operation is 3 (Multiplication):
 Input: Read num1 and num2.

 Calculate: result = num1 * num2.

 Output: Display the result.

 If operation is 4 (Division):

 Input: Read num1 and num2.

 If num2 is 0:

o Output: Display an error message ("Cannot divide by zero").

 Else:

o Calculate: result = num1 / num2.

o Output: Display the result.

 If operation is 5 (Exponentiation):

 Input: Read num1 and num2.

 Calculate: result = num1 ** num2.

 Output: Display the result.

 If operation is 6 (Factorial):

 Input: Read num (ensure it is a non-negative integer).

 If num < 0:

o Output: Display an error message ("Factorial not defined for


negative numbers").

 Else:

o Calculate: result = factorial(num).

o Output: Display the result.

 If invalid operation:

 Output: Display an error message ("Invalid operation selected").

 End

FLOWCHART:-
CODE:-

import math

def calculator():

print("Welcome to the Simple Calculator!")

print("Choose an operation:")

print("1. Addition (+)")

print("2. Subtraction (-)")

print("3. Multiplication (*)")

print("4. Division (/)")

print("5. Exponentiation (x^y)")

print("6. Factorial (x!)")

operation = input("Enter the number of the operation you want to


perform (1-6): ")
if operation in ['1', '2', '3', '4', '5']:

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

if operation in ['2', '4', '5']:

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

if operation == '1':

result = num1 + num2

print(f"The result of {num1} + {num2} is: {result}")

elif operation == '2':

result = num1 - num2

print(f"The result of {num1} - {num2} is: {result}")

elif operation == '3':

result = num1 * num2

print(f"The result of {num1} * {num2} is: {result}")

elif operation == '4':

if num2 != 0:

result = num1 / num2

print(f"The result of {num1} / {num2} is: {result}")

else:

print("Error: Cannot divide by zero.")

elif operation == '5':

result = num1 ** num2

print(f"The result of {num1} ^ {num2} is: {result}")

elif operation == '6':

num = int(input("Enter a non-negative integer for factorial: "))

if num < 0:

print("Error: Factorial is not defined for negative numbers.")

else:

result = math.factorial(num)

print(f"The factorial of {num} is: {result}")


else:

print("Invalid operation selected.")

# Run the calculator

calculator()

OUTPUT:-

Welcome to the Simple Calculator!

Choose an operation:

1. Addition (+)

2. Subtraction (-)

3. Multiplication (*)

4. Division (/)

5. Exponentiation (x^y)

6. Factorial (x!)

Enter the number of the operation you want to perform (1-6): 1

Enter the first number: 10

Enter the second number: 5

The result of 10.0 + 5.0 is: 15.0

You might also like