Aim :- Write a Java Program to Make a Calculator.
Java Program :
import java.util.*;
class calcu
{
int add(int i,int j)
{
return (i+j);
}
int sub(int i,int j)
{
return (i-j);
}
int mul(int i,int j)
{
return (i*j);
}
int div(int i,int j)
{
return (i/j);
}
}
class calculate
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println();
System.out.print("Enter 1st Number : ");
int i=sc.nextInt();
System.out.print("Enter 2nd Number : ");
int j=sc.nextInt();
System.out.println("1).Addition\t\t2).Subtraction\n3).
Multiplication\t4).Division");
System.out.print("Enter Choice : ");
int ch=sc.nextInt();
float result=0;
calcu c=new calcu();
switch(ch)
{
case 1:result=c.add(i,j);
break;
case 2:result=c.sub(i,j);
break;
case 3:result=c.mul(i,j);
break;
case 4:result=c.div(i,j);
break;
}
System.out.print("\nAnswer is : "+result);
}
}
Output :
0 Comments