Computer Science, asked by Rißhab, 1 year ago

write a java program to input two numbets and one arithmetic operator and calculate the result according to operator

Answers

Answered by saransh1775270p9fpvv
0
here is your answer, hope it helps you , please mark as brainliest
Attachments:

saransh1775270p9fpvv: thanks for marking brainliest
Answered by QGP
6
import java.util.Scanner;  //Importing Scanner for input
public class Brainly        //Creating a Class
{
    public static void main(String[] args)      //Creating main() function
    {
        Scanner sc = new Scanner(System.in);        //Declaring Scanner Object
        
        System.out.print("Enter first number: ");       //Taking Input of first number
        double a = sc.nextDouble();
        
        System.out.print("Enter second number: ");      //Taking Input of second number
        double b = sc.nextDouble();
        
        System.out.print("Enter arithmetic operator: ");    //Taking Input of arithmetic operator
        char ch = sc.next().charAt(0);      //sc.next() gives a String. sc.next().charAt(0) gives the first character of that string.
        
        System.out.println();
        
        switch(ch)
        {
            case '+':       //Addition Case
                System.out.println(a+" "+ch+" "+b+" = "+(a+b));
                break;
                    
            case '-':       //Subtraction Case
                System.out.println(a+" "+ch+" "+b+" = "+(a-b));
                break;
                
            case '*':       //Multiplication Case
                System.out.println(a+" "+ch+" "+b+" = "+(a*b));
                break;
                
            case '/':       //Division Case
                System.out.println(a+" "+ch+" "+b+" = "+(a/b));
                break;
                
            default:
                System.out.println("Invalid Arithmetic Operator");
        }
    }
}


Similar questions