Wednesday, August 19, 2009

Addition of two numbers Java Program

Hello all of you. Welcome back to Java Code Online. Today I will be giving you a very simple and basic program of Java, that is the addition of two numbers. The code makes use of Scanner for getting the input from the console.

The code starts by asking the user to enter two numbers to be added. The user enters the first number, press enter, and then enters the second number and then press enter again. After this the code computes the addition of these two numbers.

The Java Code is provided below:-

//////////////////////////////////////

package developer;

import java.util.Scanner;

public class AddTwoNumber {

/**
* @param args
*/
public static void main(String[] args) {
System.out.println("Enter the first and second numbers");
//Get the first number
Scanner scanOne = new Scanner(System.in);
//Get the second number
Scanner scanTwo = new Scanner(System.in);

if(scanOne.hasNextInt() && scanTwo.hasNextInt())
{
System.out.print("The sum of the two numbers entered is: ");
System.out.println(scanOne.nextInt()+scanTwo.nextInt());
}

}

}

//////////////////////////////////////////

I hope that this basic code was beneficial for the beginners to Java Programming. For more information and programs on Java, keep buzzing Java Code Online.

Related Programs
Hello World Program in Java
Length of a String Java Program
Palindrome Program in Java

1 comment:

  1. Thansk for the Java code. I am a fresher in java, and wish to pursue my carrer in it. I found your blog to be really good for begineeres. Thanks a lot.

    ReplyDelete

Note: Only a member of this blog may post a comment.