Showing posts with label Fundamental of Java. Add Two Numbers in java. Show all posts
Showing posts with label Fundamental of Java. Add Two Numbers in java. Show all posts

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

Thursday, 17 January 2013

Addition of Two Numbers Java Programming Code

Java program to add two numbers

Simple Program to Add two numbers in Java



//A Simple Program to Add Two Numbers in Java Program.



class AddTwoNumbers        //Class Declaration
{
   public static void main(String args[]) //Main Function
   {
      int x, y, z;      // Variable Declaration
      x = 10;           //Assigning 10 to the variable x.
      y = 20;           //Assigning 20 to the variable y.
      z = x + y;        //Expression to Add two variables x, y and save the result to z
      System.out.println("Sum of x and y = "+z); //This line output the value of z on the Screen
   }
}


Output:

Sum of x and y =30