Write and Execute Java Addition Program

By | August 4, 2019

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.

"<yoastmark

Click on File menu and select New project. A new dialog box will open.

"<yoastmark

Select ‘java’ and click on Java Application. Click on next button.

Writing and executing Java Addition Program

Writing and executing Java Addition Program

Type the name of your Java project in the Project name text box.

Java Addition Program

Java Addition Program

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.

Write and run Java add program

Write and run Java add program

Type the source code to add two numbers in Java.

The source code of Java Addition Program

The source code of Java Addition Program

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.

Execute Java add two nums program

Execute Java add two nums program

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

Leave a Reply

Your email address will not be published. Required fields are marked *