Free CoreJava Programs Source codes | Examples | Video Tutorials Download

Tuesday, January 20, 2009

Add Two Numbers Program

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
* Add two numbers (input by user) and show the result
*/
public class AddTwoNumbers
{
/**
* add two numbers and display the result
* if error occured will pass to the caller
* @throws Exception
*/
public static void addNums() throws Exception
{
System.out.println("Enter First No : ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
int no1=Integer.parseInt(str);

System.out.println("Enter Second No : ");
str=new BufferedReader(new InputStreamReader(System.in)).readLine();
int no2=Integer.parseInt(str);

System.out.println("Sum : " + no1 + " + " + no2 + " = " + (no1+no2));
}

/**
* execution begin from here
* @param arg
*/
public static void main(String[] arg)
{
try
{
addNums();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

No comments:

Post a Comment

Followers