Thursday, 23 June 2011

Java Program to take two values from user and display sum of these values

Open notepad or any text editor and type this code.


import java.io.*;

class userio
   {
     public static void main(String args[]) throws IOException
      {
        BufferedReader ob=new BufferedReader(new InputStreamReader(System.in));
        String a,b;
        System.out.print("Enter 1st no: ");
        a=ob.readLine();
        System.out.print("Enter 2nd no: ");
        b=ob.readLine();
        int c=Integer.parseInt(a)+Integer.parseInt(b);
        System.out.println("The Sum is: "+c);
       }
    }    


and save this file as userio.java in the bin directory (c:\j2sdk1.4.2\bin).

Now open command prompt and go to your Java directory.
Now compile your  program by typing  javac userio.java
Now the program will be compiled.
Now Run your program by typing  java userio

The output of the above program will be like this.

No comments:

Post a Comment