Saturday, 19 January 2013

JAVA Program to Add Two Numbers

ADD TWO NUMBERS USING JAVA

A program to accept two numbers from keyboard and calculate the sum.


//A small java program to add two numbers

import java.util.Scanner;

class AddTwoNumbers
{
   public static void main(String args[])
   {
      int a, b, c;
      System.out.println("Enter two integers to calculate the sum ");
      //A simple text scanner which can parse primitive types and strings using regular                                      expressions.
      Scanner in = new Scanner(System.in);
      //Capturing the scanned token as Int and storing it to the variables a and b.
      a = in.nextInt();
      b = in.nextInt();
      c = a + b;
      System.out.println("Sum of entered integers = "+c);
   }
}

Output:

Enter two integers to calculate the sum
10
15
Sum of entered integers = 25

No comments :

Post a Comment