Add two numbers in Java

In this article we learn how to add two numbers in java without using eclipse.

Step 1: Lets open a notepad and write the following code.

//Code

class demo  {

        public static void main(String arg[])       {

int a = 10, b = 20, c = 0;

c = a + b;

System.out.println(“sum of two no is :” + c);

}

}

Step 2: Name it as “ab.java” and save the file on any location. I saved at “C:\app“.

Step 3: Open command prompt (Press window + R and write cmd and hit OK).

1

Step 4: Go to “C:\app” by using command propmpt

2

 

Step 5: Now write the following code for checking my java file is compiling or not.

javac ab.java

3

 

My java program compiled successfully.

Step 6: Write the following code in command prompt. Press enter and see the output.

java demo

// demo is a class name which is written in my ab.java file.

Output

4

Happy Coding.

Tags: , ,