Task: Write and Execute Java Addition Program. This Java Programming Tutorial will provide and explain the source code of a basic addition program in Java. The program will input two numbers from the user at run time. The Java program will calculate the sum of these two numbbers and will display the answer of addition.
Note: You must know How to get input from user in a Java Console Application
You may also like to read How to Write and Execute First Java Program in NetBeans
The Complete Procedure to Write, Compile and Execute Java Addition Program
Start NetBeans IDE.
Click on File menu and select New project. A new dialog box will open.
Select ‘java’ and click on Java Application. Click on next button.
Type the name of your Java project in the Project name text box.
Clcik on Finish button.
You will get a code editor window with a default and basic Java program template. Edit the default template as shown in the image below.
Type the source code to add two numbers in Java.
Click on Run menu and select Run Project to execute your Java Addition project. The program will be compiled first. The Java compiler ‘javac’ will convert the Java source code ‘Addition.java’ in to ‘Addition.class’ file. This class file is called the Java Byte Code. This Java byte code is iterpreted by Java Virtual Machine – JVM will interpret and execute this byte code.
The source code of the Basic Add Two Number Program in Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
/* * Write a Java program to input and add * two numbers. */ package addition; /** * * @author www.EasyCodeBook.com */ import java.util.Scanner; public class Addition { public static void main(String[] args) { int num1, num2, sum; Scanner input = new Scanner(System.in); System.out.print("Enter first number="); num1 = input.nextInt(); System.out.print("Enter second number="); num2 = input.nextInt(); sum = num1 + num2; System.out.println("Result of Addition="+sum); } } |
Outout
Enter first number=100
Enter second number=200
Result of Addition=300