How to calculate with Java

How to calculate with Java - Basic calculation and swapping with java programming language. Today we will see the code of addition, substruction, multiple, division and swapping in Java. At first write a file and save it in jdk directory in bin folder now open it with notepad editor and paste the bellow code and save it.
Related: First Java Program | Install Java | Keyboard input in Java

How to calculate with Java


import java.io.*;
class fourth
{
public static void main(String args[])throws IOException
{
Integer r;
Float n,add,sub,mul,div,empty;
String s1;
String s2;

InputStreamReader in=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(in);
System.out.print("Enter first value");
s1=br.readLine();

System.out.print("Push second value");
s2=br.readLine();

r=Integer.valueOf(s1);
n=Float.valueOf(s2);
add=r+n;
sub=r-n;
mul=r*n;
div=r/n;

System.out.println("add of two variable"+add);
System.out.println("sub of two variable"+sub);
System.out.println("mul of two variable"+mul);
System.out.println("divi of two variable"+div);

empty=add;
add=mul;
mul=empty;

empty=sub;
sub=div;
div=empty;
System.out.println("after changing/Swapping");
System.out.println("add of two Variable"+add);
System.out.println("sub of two variable"+sub);
System.out.println("mult of two variable"+mul);
System.out.println("divi of two variable"+div);

}
}

Here in the above code when you run it it will show like this screenshot:-

How to calculate with Java

When you run with cmd then this will ask you to enter first value and then second value and finally it will print all the result after the calculation. How to calculate with Java new codes coming soon.

0 Comments