Monday 14 March 2016

simple addition in java - user defined variable

Java is simple language. Here is the simple coding to adding two numbers that are user defined.

First, is to include the header file.




    |import java.io.*; 
    |class add
    |{
Here the adding is the class name. You can keep your desired classname. This should not be any keywords.
This is the main program.
First is to declare the void main then the declaration part. Then the execution segment.
 

   |public static void main(String args[])throws IOException

   |{
   |DataInputStream a = new DataInputStream(System.in)

   |int a,b,c;

   |System.out.println("Enter the first number:");

   |a=Integer.ParseInt(a.readLine());

   |System.out.println("Enter the second number:");
   |b=Integer.ParseInt(a.readLine());

   |c= a+b;

   |System.out.println("Sum of the given number is "+c);

   | }

   | }
After typing the program in the notepad, save the file using an extension as .java
Open the command prompt. Type the name of the drive where you have saved the file. 
Then now to set the path. 
path = C:\jdk1.3\bin;
Now to compile the java program 
javac filename.java
In our case its, add so 

javac add.java
Any errors will be shown. If not the next line will be displayed. 
Now to run the file then. Type java filename.

java add

Output will be 
C:\Documents and Settings\dcom358>H:
H:\>path=C:\jdk1.3\bin;
H:\>javac add.java
H:\>java add

Enter the first number
25
Enter the Second number
30
Sum of two numbers is 55

No comments:

Post a Comment