# Java Programming | Addition of numbers

Introduction

This tutorial is for addition of numbers in java. The program is given below that explains it. The program is extendable. Go enjoy the program. Lets begin……

Program for addition of two numbers in java.

``````
//import Scanner as we require it.
import java.util.Scanner;

// the name of our class its public
//void main
public static void main (String[] args)
{
//declare int
int x,y,z;
//print message
System.out.println("Enter Two numbers:");
//Take input
Scanner input = new Scanner(System.in);
//calculate
x = input.nextInt();
y = input.nextInt();
z = x + y;
//print the sum
System.out.println("Sum = "+z);
}
}
``````

Output

Enter Two numbers:
12
54
Sum = 66

Program for addition of three numbers in java

//import Scanner as we require it.
import java.util.Scanner;

// the name of our class its public
//void main
public static void main (String[] args)
{
//declare int
int w,x,y,z;
//print message
System.out.println(“Enter Three numbers:”);
//Take input
Scanner input = new Scanner(System.in);
//calculate
w = input.nextInt();
x = input.nextInt();
y = input.nextInt();
z = w + x + y;
//print the sum
System.out.println(“Sum = “+z);
}
}

Output

Enter Three numbers:
1
2
3
Sum = 6

How it works

1. The program prints the message to enter numbers.
2. The user enters numbers.
3. Sum is calculated.
4. The sum is printed.

Extending it

The program can be extended by adding more numbers and calculating their sum.

Explanation.

1. Import the Scanner.
2. Declare the class as public
3. Add the void main function
4. Add system.out.println() function with the message to enter numbers.
5. Declare input as Scanner.
6. Take the numbers and save it in variables.
7. Calculate Sum and save it in other variable.
8. Add system.out.println() function to print the sum.

At the end

You learnt creating the Java program for addition of two numbers. So now enjoy the program.

Please comment on the post and share it.
And like it if you liked.

Sharing options: