Answer to Question #314854 in Java | JSP | JSF for Pradesh Patil

Question #314854

4. Operation: Operation

by CodeChum Admin

You have been cordially invited to partake in Operation: Operation. Your mission, should you choose to accept it, is to take the two numbers and the operator given then perform the operation successfully.


Instructions:

  1. Input one number (integer or decimal), an operator (+, -, *, /), and another number (integer or decimal).
  2. Print the result of the operation between the two numbers, up to 2 decimal places.

Instructions

  1. Input one number (integer or decimal), an operator (+, -, *, /), and another number.
  2. Print the result of the operation between the two numbers, up to 2 decimal places.

Input

The first line contains the first number.

The second line contains the operator.

The third line contains the second number.

5
+
0.70

Output

A line containing a decimal/float containing two decimal places.

5.70


"The code should use If-Else-Elseif statement"


1
Expert's answer
2022-03-20T14:41:12-0400
import java.io.IOException;
import java.util.Locale;
import java.util.Scanner;

public class Main {


    public void Calcul(float firstNum, char operat, float secondNum){
        if(operat == '+'){
            System.out.printf("%.2f", firstNum+secondNum);
        } else if(operat == '-'){
            System.out.printf("%.2f", firstNum-secondNum);
        } else if(operat == '*'){
            System.out.printf("%.2f", firstNum*secondNum);
        } else if(operat == '/'){
            System.out.printf("%.2f", firstNum/secondNum);
        }
    }


    public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(System.in);
        in.useLocale(Locale.US);
        Main cal = new Main();

        float firstNum = in.nextFloat();
        char operat = (char) System.in.read();
        float secondNum = in.nextFloat();

        cal.Calcul(firstNum, operat, secondNum);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS