Day 43: All Factors of a Number of 100DaysCodingChallenge
Hello all,
#day43
Day43
Today's my task was to Write a Program to Make a Simple Calculator to Add, Subtract, Multiply or Divide Using Switch case
The program should takes an arithmetic operator (+, -,*, /) in string and two operands from a user and performs the operation on those two operands depending upon the operator entered in string by the user.
I have started the 100 days of coding challenge to improve my programming skills..
Code :
// Function to perform arithmetic operations
function calculate(operator, operand1, operand2) {
  let result;
  
  switch (operator) {
    case 'add':
      result = operand1 + operand2;
      break;
    case 'subtract':
      result = operand1 - operand2;
      break;
    case 'multiply':
      result = operand1 * operand2;
      break;
    case 'divide':
      result = operand1 / operand2;
      break;
    default:
      console.log('Invalid operator!');
      return;
  }
  
  console.log(`Result: ${operand1} ${operator} ${operand2} = ${result}`);
}
// Example usage
const operand1 = parseFloat(prompt('Enter the first operand:'));
const operand2 = parseFloat(prompt('Enter the second operand:'));
const operator = prompt('Enter an arithmetic operator (add, subtract, multiply, divide):');
calculate(operator, operand1, operand2);
Output :
Happy Coding:)
Thank you...

 
 
 
Comment please if you like
ReplyDelete