Java Program to Add two Numbers

In this Java program, you'll learn how to read two integer numbers from the console using a Scanner. After addition, the final sum is displayed on the screen.

Java Program to Add Two Numbers

package com.javaguides.java.tutorial;

import java.util.Scanner;

/**
 * Java Program to Add two Numbers
 * 
 * @author https://www.sourcecodeexamples.net/
 *
 */
public class JavaProgramAddTwoNumber {
    public static void main(String[] args) {

        int num1, num2, sum;
        try (Scanner sc = new Scanner(System.in)) {
            System.out.println("Enter First Number: ");
            num1 = sc.nextInt();

            System.out.println("Enter Second Number: ");
            num2 = sc.nextInt();
            sum = num1 + num2;
            System.out.println("Sum of these numbers: " + sum);
        }
    }
}
Output:
Enter First Number: 
10
Enter Second Number: 
20
Sum of these numbers: 30

Related Java Programs

Comments