Addition of two numbers in Java


 

Sum of two numbers

In this program we use two variables which receive value from keyboard and add these values and sum of these numbers are store in third variable.

Example

import java.util.Scanner;

class Addition

{

           public static void main(String[] args)

           {

    int a, b, c=0;

           Scanner s=new Scanner(System.in);

           System.out.println("Enter Any Two Numbers :");

           a=s.nextInt();

           b=s.nextInt();

           c=a+b;

           System.out.println("Sum: "+c);

           }

}

Output

Enter any two numbers :

20

30

Sum: 50

Syntax to compile and run java program

Syntax

for compile -> c:/javac Addition.java

for run -> c:/java Addition

Explnation of Code

·         Scanner s=new Scanner(System.in): are used for receive input from keyboard.

·         nextInt(): method are used for get integer type value from keyboard.

·         System.out.println("....."): are used for display message on screen or console.

Post a Comment

0 Comments