Creating basic calculator using java is the best way to know some basic arithmetic operations like addition, subtraction, multiplication, division etc.
Let’s write the code
Now let’s see output
Let’s write the code
class Calc{
int c;
void add(int a,int b)
{ c=a+b;
System.out.println("Add = "+c);
}
void sub(int a,int b)
{ c=a-b;
System.out.println("Sub = "+c);
}
void div(int a,int b)
{ c=a/b;
System.out.println("Div = "+c);
}
void mul(int a,int b)
{ c=a*b;
System.out.println("Mul = "+c);
}
}
class Cal{
public static void main(String args[])
{
int a=10;
int b=2;
Calc ob=new Calc();
ob.add(a,b);
ob.sub(a,b);
ob.div(a,b);
ob.mul(a,b);
}
}
In above program we are we use two classes. In main class we take two integer “a” and “b” then complete addition, subtraction, division, multiplication operation on it.int c;
void add(int a,int b)
{ c=a+b;
System.out.println("Add = "+c);
}
void sub(int a,int b)
{ c=a-b;
System.out.println("Sub = "+c);
}
void div(int a,int b)
{ c=a/b;
System.out.println("Div = "+c);
}
void mul(int a,int b)
{ c=a*b;
System.out.println("Mul = "+c);
}
}
class Cal{
public static void main(String args[])
{
int a=10;
int b=2;
Calc ob=new Calc();
ob.add(a,b);
ob.sub(a,b);
ob.div(a,b);
ob.mul(a,b);
}
}
Now let’s see output
Add = 12
Sub = 8
Div = 5
Mul = 20
Sub = 8
Div = 5
Mul = 20

0 comments:
Post a Comment