BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to Add two Numbers

Last Updated: February 20, 2026 by Chaitanya Singh | Filed Under: Java Examples

In this tutorial, you will learn how to write a Java program to add two numbers. This is one of the basic program which helps to understand the basic concepts of java programming. We will do it in three different ways:

  • In the first program, we have the two numbers we just write the logic to add the given values.
  • In the second program, user is asked to enter the two numbers and the program calculates the sum of the input values.
  • In the third program, we will calculate the sum of two non-integer numbers.

Example 1: Sum of two numbers

This is a simple example, where two integer numbers are given. We have stored the given numbers in two int variables num1 and num2. The calculated sum of these values would be stored in third int variable sum.

public class JavaExample {
  public static void main(String[] args) {
    // two integer variables with values
    // and a variable "sum" to store the result
    int num1 = 5, num2 = 15,sum;

    //calculating the sum of num1 and num2 and
    //storing the result in the variable sum
    sum = num1+num2;

    //printing the result
    System.out.println("Sum of "+num1+" and "+num2+" is: "+sum);
  }
}

Output:

Sum of 5 and 15 is: 20

Example 2: Sum of two numbers using Scanner

The Scanner class provides the methods that allows us to read the user input. The values entered by user is read using Scanner class and stored in two variables num1 and num2. The program then calculates the sum of input numbers and displays it.

import java.util.Scanner;
public class AddTwoNumbers2 {

    public static void main(String[] args) {
        
        int num1, num2, sum;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter First Number: ");
        num1 = sc.nextInt();
        
        System.out.println("Enter Second Number: ");
        num2 = sc.nextInt();
        
        sc.close();
        sum = num1 + num2;
        System.out.println("Sum of these numbers: "+sum);
    }
}

In this program, the statement Scanner sc = new Scanner(System.in); creates an instance of Scanner class. This instance calls nextInt() method to read the number entered by user. The read values are stored in variables num1 and num2. Once the values are stored in variables. The addition is performed on these variables and result is displayed.
Output:

Enter First Number: 
121
Enter Second Number: 
19
Sum of these numbers: 140

Example 3: Program to add two non-integer numbers

In previous examples, we have added integer numbers. In this example we are going to add float numbers such as 10.5, 16.667 etc.

Note: Variables data types is double unlike previous examples where data type was int.

import java.util.Scanner;
public class JavaExample {

  public static void main(String[] args) {

    double num1, num2, sum;
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter First Number: ");
    num1 = sc.nextDouble();

    System.out.print("Enter Second Number: ");
    num2 = sc.nextDouble();

    sc.close();
    sum = num1 + num2;
    System.out.println("Sum of "+num1+" and "+num2+" is: "+sum);
  }
}

Output:

Java Program to Add two Numbers

Practice the same program in C language here.

Related Java Examples:

  • Java Program to print patterns
  • Java Program to add two complex numbers
  • Java Program to add two matrices
  • Java Program to add two binary numbers
❮ Java TutorialJava Programs ❯

Top Related Articles:

  1. Java Program to Add two Binary Numbers
  2. Java Program to Calculate average using Array
  3. Java Program to find largest of three numbers using Ternary Operator
  4. Java Program to Find Factorial using For and While loop
  5. Java Program to find the Smallest of three numbers using Ternary Operator

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2026 BeginnersBook . Privacy Policy . Sitemap