Menu
Coddy logo textTech

Recap - If Else

Part of the Fundamentals section of Coddy's C++ journey — lesson 31 of 74.

challenge icon

Challenge

Beginner

You are given a code which gets as input two numbers n1 and n2  of type double and a single char string op.

Note: we will learn in next lessons how to get input from the user, currently just don't touch the three first lines.

 

The possible values for op are '+', '-', '/' and '*'

Your task is to set the variable result of type double based on the conditions:

  • if op is '+', set result with n1 + n2.
  • if op is '-', set result with n1 - n2.
  • if op is '/', set result with n1 / n2.
  • if op is '*', set result with n1 * n2.

Try it yourself

#include <iostream>

int main() {
    double n1, n2;
    char op;
    std::cin >> n1 >> n2 >> op;
    
    // Write your code below
    double result = 0;
    
    
    
    std::cout << result << std::endl;
    return 0;
}

All lessons in Fundamentals