A Simple Java Program - Addition - JAVA

Program:
import java.io.*;
class add
{
public static void main(String args[]) throws IOException
{
    int num1,num2,sum;
    try
    {
        System.out.println("Enter two numbers:");
        /*DataInputStream x=new DataInputStream(System.in);
        num1=Integer.parseInt(x.readLine());
        num2=Integer.parseInt(x.readLine());
        */
        InputStreamReader Isr=new InputStreamReader(System.in);
        BufferedReader br=new BufferedReader(Isr);
        String s=br.readLine();
        num1=Integer.parseInt(s);
        num2=Integer.parseInt(br.readLine());
        sum=num1+num2;
        System.out.println("Sum="+sum);
    }catch(Exception e)
    {}
}
}
Output:
nn@linuxmint ~ $ javac add.java
nn@linuxmint ~ $ java add
Enter two numbers:
5
10
Sum=15
nn@linuxmint ~ $

1 comment:

Related Posts Plugin for WordPress, Blogger...