Simple java program to add two numbers

java program to add two numbers is a very simple program.

It is required by learners How can we add two numbers.

We will start with simple addition and try to cover additional things here.

Flowchart To Add Two Numbers
Flowchart To Add Two Numbers

Simple java program to add two numbers

Steps to add two numbers

  1. Store two numbers in two variables
  2. Add both variables and store in another variable
  3. Print the result
 
public class Addition {

    public static void main(String[] args) {
        int a = 5;
        int b = 6;
        int sum = a + b;
        System.out.println("Addition is " + sum);
    }
}
 
Addition is 11

Here we take a=5 and a=6 two variables

added a and b and stored in variable sum

printed the sum in consol.

Fig: Addition of two numbers in Notepad++
Fig: Compilation and run of Addition program

Addition of two numbers in java

Above we add two integer numbers.

Here lets take two decimal numbers in java we represent decimal number in float and double

Addition of two decimal numbers in java

 
public class Addition {

    public static void main(String[] args) {
        double a = 5.6;
        double b = 8.9;
        double sum = a + b;
        System.out.println("Addition is " + sum);
    }
}
 
Addition is 14.5

The process is same only we changed data types.

Addition of two numbers in java

Here we are considering two numbers one is integer and another is double.

 
public class Addition {

    public static void main(String[] args) {
        int a = 6;
        double b = 15.9;
        double sum = a + b;
        System.out.println("Addition is " + sum);
    }
}
 
Addition is 21.9

Here a is integer data type and b is double data type.

For addition a is automatically promoted to double data type by the compiler the addition is performed.

Whenever two or more variable of different data types take part in any operation then a lower data type is always converted to higher data type.

This automatic conversion is done by compiler and the process in known as widening.

Addition of short numbers in java

 
public class Addition {

    public static void main(String[] args) {
        short a = 12;
        short b = 15;
        int sum = a + b;
        System.out.println("Addition is " + sum);
    }
}
 
Addition is 27

Here variable a and b both are short type.

While adding two short numbers it is converted to integer and the result is stored in integer data type.

Both byte and short data types are converted to an integer.

Sum of two numbers in java using method

 

public class Addition {

    public static void main(String[] args) {
        System.out.println("Addition of two numbers");
        int sum1 = Addition.sum(5, 7);
        System.out.println("Sum is " + sum1);
        Addition a = new Addition();
        a.add1();
        int sum2 = a.add2();
        System.out.println("Addition is " + sum2);
        a.add3(5.7, 6.7);
        double sum3 = a.add4(34.2, 44.2);
        System.out.println("Addition is " + sum3);
    }

    public static int sum(int a, int b) {
        return a + b;
    }

    public void add1() {
        int a = 5;
        int b = 7;
        int sum = a + b;
        System.out.println("Sum =" + sum);
    }

    public int add2() {
        int a = 5;
        int b = 7;
        int sum = a + b;
        return sum;
    }

    public void add3(double num1, double num2) {
        double sum = num1 + num2;
        System.out.println("Sum =" + sum);
    }

    public double add4(double num1, double num2) {
        double sum = num1 + num2;
        return sum;
    }

}

 
Addition of two numbers
Sum is 12
Sum =12
Addition is 12
Sum =12.4
Addition is 78.4

In the above program sum() is a static method so we called it with Class name.

add1(), add2(), add3() and add4() are non static so we called by object of class.

Methods are with and without argument and with and without return type.

Write the definition of a method add, which receives two integer parameters and returns their sum.

 
public class Addition {

    public static void main(String[] args) {
        System.out.println("Addition of two numbers");
        Addition a = new Addition();
        int sum1 = a.add(12, 16);
        System.out.println("Sum is " + sum1);
    }

    public int add(int a, int b) {
        return a + b;
    }

}

 
Addition of two numbers
Sum is 28

Adding Two Numbers using Integer class sum()

 
public class Addition {

    public static void main(String[] args) {
        System.out.println("Addition of two numbers");
        int a = 10;
        int b = 20;
        int sum = Integer.sum(a, b);
        System.out.println("Sum = " + sum);
    }

}

 
Addition of two numbers
Sum = 30

Java program to add two numbers From User Input

1 add two numbers in java using Scanner class input

 
import java.util.Scanner;

public class Addition {

    public static void main(String[] args) {
        System.out.println("Addition of two numbers");
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter First Number");
        int a = scanner.nextInt();
        System.out.println("Enter Second Number");
        int b = scanner.nextInt();
        int sum = a+b;
        System.out.println("Sum = " + sum);
    }

}


 
Addition of two numbers
Enter First Number
23
Enter Second Number
34
Sum = 57

2 add two numbers in java using BufferedReader Class input

 

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Addition {

    public static void main(String[] args) {
        System.out.println("Addition of two numbers");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int a = 0, b = 0;
        try {
            System.out.println("Enter first number");
            a = Integer.parseInt(br.readLine());
            System.out.println("Enter first number");
            b = Integer.parseInt(br.readLine());
        } catch (Exception e) {
            System.out.println("Exception " + e);
        }
        int sum = a + b;
        System.out.println("Sum = " + sum);
    }

}

 
Addition of two numbers
Enter first number
23
Enter first number
12
Sum = 35

3. add two numbers in java using Console class input

 

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Addition {

    public static void main(String[] args) {
        System.out.println("Addition of two numbers");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int a = 0, b = 0;
        try {
            System.out.println("Enter first number");
            a = Integer.parseInt(br.readLine());
            System.out.println("Enter first number");
            b = Integer.parseInt(br.readLine());
        } catch (Exception e) {
            System.out.println("Exception " + e);
        }
        int sum = a + b;
        System.out.println("Sum = " + sum);
    }

}

 
Addition of two numbers
Enter first number
23
Enter first number
23
Sum = 46

write a program to add two numbers using class and constructor

 
public class Addition {

    Addition() {

    }

    Addition(int a, int b) {
        int sum = a + b;
        System.out.println("sum is " + sum);
    }

    public static void main(String[] args) {
        System.out.println("Addition of two numbers");
        new Addition(4, 5);
    }

}

 
Addition of two numbers
sum is 9

Addition if Numbers class in java

The abstract class Number is the superclass of platform classes representing numeric values that are convertible to the primitive types byte, double, float, int, long, and short.

java.lang.Number

To convert Number class to int long float double the methods intValue(), longValue(), floatValue() and doubleValue() are used respectively.

 

public class Addition {

    public static void main(String[] args) {
        Number a = 55l;
        Number b = 64l;
        Number sum = add(a, b);
        System.out.println("Sum =" + sum);
    }

    public static Number add(Number a, Number b) {
        if (a instanceof Double || b instanceof Double) {
            return a.doubleValue() + b.doubleValue();
        } else if (a instanceof Float || b instanceof Float) {
            return a.floatValue() + b.floatValue();
        } else if (a instanceof Long || b instanceof Long) {
            return a.longValue() + b.longValue();
        } else {
            return a.intValue() + b.intValue();
        }
    }
}


 
Sum =119

How to add Two BigInteger in Java

To add two BigInteger numbers we use add() method.

 
import java.math.BigInteger;

public class Addition {

    public static void main(String[] args) {
        BigInteger b1 = new BigInteger("55");
        BigInteger b2 = new BigInteger("33");
        b1.add(b2);
        System.out.println("Sum ="+b1);
    }

}
 
Sum =55