# This function will add the two numbers given to it
def add(a, b):
return a + b
# This function will subtract the two numbers given to it
def subtract(a, b):
return a – b
# This function will multiply the two numbers given to it
def multiply(a, b):
return a * b
# This function divides two numbers
def divide(a, b):
return a / b
# Showing the options to the user
print(“Welcome to a calculator simulation using Python.”)
print(“We have the following options in this program: “)
print(“1.Add \n2.Subtract \n3.Multiply \n4.Divide”)
while True:
# We will now take the input from the user
choice = input(“Please enter the number in accordance with your choice: “)
# We will check if the choice given by the user is a valid option
if choice in (‘1’, ‘2’, ‘3’, ‘4’):
num1 = float(input(“Enter first number: “))
num2 = float(input(“Enter second number: “))
# Based on the choice of the user, we will call the corresponding function
if choice == ‘1’:
print(num1, “+”, num2, “=”, add(num1, num2))
elif choice == ‘2’:
print(num1, “-“, num2, “=”, subtract(num1, num2))
elif choice == ‘3’:
print(num1, “*”, num2, “=”, multiply(num1, num2))
elif choice == ‘4’:
print(num1, “/”, num2, “=”, divide(num1, num2))
# We will now see if the user wants to do more numerical calculations using this program
more_calculations = input(“Let’s do next calculation? (yes/no): “)
if more_calculations == “no” or more_calculations == “No” or more_calculations == “NO”:
break
# This is the scenario where the user inputs a wrong choice
else:
print(“Invalid Input. Please try again!”)
function add(a,b)
{
return ((+a)+(+b));
}
// This function will subtract the two numbers given to it
function subtract(a,b)
{
return ((+a)-(+b));
}
// This function will multiply the two numbers given to it
function multiply(a,b)
{
return ((+a)*(+b));
}
// This function divides two numbers
function divide(a,b)
{
return ((+a)/(+b));
}
// Showing the options to the user
window.alert(“Welcome to a calculator simulation using Javascript”);
window.alert(“We have the following options in this program:”);
window.alert(“1.Add \n 2.Subtract \n 3.Multiply \n 4.Divide”);
var isquit = true;
while(isquit)
{
// We will now take the input from the user
var choice = window.prompt(“Please enter the number in accordance with your choice:”);
// We will check if the choice given by the user is a valid option
// Based on the choice of the user, we will call the corresponding function
if (choice == 1)
{
var num1= window.prompt(“Enter first number:”);
var num2= window.prompt(“Enter second number:”);
window.alert(num1 + “+” + num2 + “=” + add(num1,num2));
// We will now see if the user wants to do more numerical calculations using this program
var morecalculations = window.prompt(“Let’s do next calculation? (yes/no)”);
if (morecalculations == “no” || morecalculations == “No” || morecalculations == “nO” || morecalculations == “NO”)
{
isquit= false;
}
}
else if (choice == 2)
{
var num1= window.prompt(“Enter first number:”);
var num2= window.prompt(“Enter second number:”);
window.alert(num1 + “-” + num2 + “=” + subtract(num1,num2));
var morecalculations = window.prompt(“Let’s do next calculation? (yes/no)”);
if (morecalculations == “no” || morecalculations == “No” || morecalculations == “nO” || morecalculations == “NO”)
{
isquit= false;
}
}
else if (choice == 3)
{
var num1= window.prompt(“Enter first number:”);
var num2= window.prompt(“Enter second number:”);
window.alert(num1 + “*” + num2 + “=” + multiply(num1,num2));
var morecalculations = window.prompt(“Let’s do next calculation? (yes/no)”);
if (morecalculations == “no” || morecalculations == “No” || morecalculations == “nO” || morecalculations == “NO”)
{
isquit= false;
}
}
else if (choice == 4)
{
var num1= window.prompt(“Enter first number:”);
var num2= window.prompt(“Enter second number:”);
window.alert(num1 + “/” + num2 + “=” + divide(num1,num2));
var morecalculations = window.prompt(“Let’s do next calculation? (yes/no)”);
if (morecalculations == “no” || morecalculations == “No” || morecalculations == “nO” || morecalculations == “NO”)
{
isquit= false;
}
}
else
{
// This is the scenario where the user inputs a wrong choice
window.alert(“Invalid Input. Please try again!”);
}
}