Computer Science, asked by Anonymous, 1 year ago

hi,
WRITE A JAVA PROGRAM WITH THE FUNCTION THAT TAKES TWO INTEGERS AND AN ARITHMETIC OPERATOR AND RETURNS THE CORRESPONDING RESULT.


# CHAPTER- METHODS/FUNCTIONS IN JAVA.

Answers

Answered by siddhartharao77
7
       int a,b;
        char c;        
 Scanner d =  new Scanner(System.in);   
             System.out.println("Enter Two Integers");   
     a = d.nextInt(); 
      b = d.nextInt();    
    System.out.println("Enter operator");     
   c = scanner.next().charAt(0);        
switch (c)
{     
  case '+':     
       System.out.println("%d + %d = %d\n", a, b, a + b);   
        break;     
   case '-':     
       System.out.println("%d - %d = %d\n", a, b, a - b);   
         break;     
   case '*':         
   System.out.println("%d * %d = %d\n", a, b, a * b);       
    break;     
   case '/':         
  System.out.println("%d / %d = %d\n", a, b, a / b);   
         break;       
       case '%' : 
           System.out.println("%d / %d = %d\n", a, b, a % b);   
         break;   
       default:   
        System.out.println("Go to hell");   
    }

Hope this helps!
Answered by Anonymous
8
// comment
class Operator
{
public void calculate (int a, int b, char op)                here a and b for integer 
int result;                                                                   values and op for taking 
{                                                                                arithmetic operator   
if(op=='+')                                                                  from the user
result = a+b;
else if (op=='-')
result = a-b;
else if(op=='*')
result = a*b;
else if(op=='/')
result = a/b;
else
System.out.println("invalid operator");
}
System.out.println("the answer is : " + result);
}
}

Anonymous: Thanks for marking me as brainliest
Anonymous: welcm ☺
Similar questions