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.


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

and save this file as 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
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