Question:

SRM Students decides to create a software to extend our help to Petty shops and Shops. In this regard the "STUDENT" team has selected a few students to complete the task. The task was monitored by a group of experts and the software was tested by a expert team from corporate.

The task is as follows when there are two items and if the shop keeper says 1 then it needs to add the two items. If the shop keeper yells 2 then the two items should be subtracted. And when the shop keeper tells 3 then the product of the items needs to be outputted. When shop keeper tells as 4 then the items should fight with one another.

Refer sample input and output:

Input should be between 1 to 4

Only Integer numbers as input.

If input is less than or greater than 1 to 4 print "Invalid Input".

TEST CASE 1 

INPUT
1
35 36
OUTPUT
71

TEST CASE 2 

INPUT
2
35 36
OUTPUT
-1

Solution :

#include <iostream>
using namespace std;
int main() {
 int option,num1,num2;
 cin>>option;
  if(option<5 && option>0){
    cin>>num1>>num2;
    if(option==1)
      cout<<num1+num2;
    else if(option==2)
      cout<<num1-num2;
    else if(option==3)
      cout<<num1*num2;
    else
      cout<<num1/num2;
  }
  else{
   cout<<"Invalid Input";
  }
 return 0;
}

Problem Description

You need to write a program that performs different arithmetic operations based on the user's input. The program should:

  1. Read an integer option (1, 2, 3, or 4) from the user.
  2. Depending on the option, perform the following operations:
    • 1: Add two numbers.
    • 2: Subtract the second number from the first.
    • 3: Multiply two numbers.
    • 4: Divide the first number by the second (integer division).
  3. If the option is not between 1 and 4, print "Invalid Input".

Solution Explanation

Here's how the solution works:

  1. Input Reading:

    • Read the option integer.
    • If option is between 1 and 4 (inclusive), read the two integer numbers num1 and num2.
  2. Perform Operation:

    • Based on the value of option, perform the corresponding arithmetic operation.
    • For option 1: Output the sum of num1 and num2.
    • For option 2: Output the result of num1 - num2.
    • For option 3: Output the product of num1 and num2.
    • For option 4: Output the result of integer division of num1 by num2. Note: Ensure num2 is not zero to avoid division by zero errors.
  3. Invalid Input Handling:

    • If option is outside the range 1 to 4, output "Invalid Input".

Explanation of the Code

  1. Reading Input:

    • cin >> option; reads the operation code.
    • cin >> num1 >> num2; reads the two integers for the operation.
  2. Conditional Checks:

    • if (option >= 1 && option <= 4) checks if the option is valid.
    • Based on the option, the corresponding arithmetic operation is performed.
  3. Handling Division by Zero:

    • When option is 4 (division), a check if (num2 != 0) is added to ensure that the division is valid.
  4. Invalid Input Handling:

    • If option is outside the range 1 to 4, "Invalid Input" is printed.