Addition Program in Java

Addition Program in Java | An Addition program in Java is the basic program, which is used to introduce to beginners. The Addition program in Java simply adds two numbers and displays them to the output screen.

Addition of Two Numbers in Java without using Scanner

It is a simple Java addition program that adds two numbers and displays the results. The values are hardcoded by the programmer.

public class Addition {
   public static void main(String[] args) {

      // declare variables
      int num1, num2, sum;

      // take two numbers
      num1 = 10;
      num2 = 20;

      // calculate sum value
      sum = num1 + num2;

      // display the sum value
      System.out.println("Sum = " + sum);
   }
}

Output:-

Sum = 30

In this program, we take three variables of the int data type. The variables’ names are num1, num2, and sum. After declaring the variables, num1 and num2 variables are initialized with 10 and 20 respectively. The variables num1 and num2 hold the value of the input. After initializing the variables, the sum is calculated as num1 + num2. Finally, the sum value is displayed using the println() method of the System class.

Simple Java Program to Add Two Numbers

In the previous program, the values of numbers are hardcoded. But we can also pass these values dynamically to the program. There are various ways to get input from the user in Java. In the below program we use the Scanner class to take input from the user.

To get input from the user first we need to import the Scanner class as below:-
import java.util.Scanner;

After import, we can create an object of Scanner class which will be used to get input from the user as:- Scanner scan = new Scanner(System.in);

Here, Scanner is a class, the scan is a user-defined identifier and new is a keyword used to create an object. For each type of data type separate method is given.

Data typeMethod
bytenextByte()
shortnextShort()
intnextInt()
longnextLong()
floatnextFloat()
doublenextDouble()

Simple Java Program to Add Two Numbers

import java.util.Scanner;

public class Addition {
   public static void main(String[] args) {

      // create Scanner class object
      // to take the input
      Scanner scan = new Scanner(System.in);

      // declare variables
      int number1, number2, sum;

      // take input for first number
      System.out.print("Enter first number: ");
      number1 = scan.nextInt();

      // take input for second number
      System.out.print("Enter second number: ");
      number2 = scan.nextInt();

      // calculate the sum
      sum = number1 + number2;

      // display the result
      System.out.println("Sum = " + sum);
   }
}

Output:-

Enter first number: 15
Enter second number: 25
Sum = 40

Enter first number: -50
Enter second number: 75
Sum = 25

In the above program, we take two integer data types as input. We can also take double data type as input, for this purpose simply use the nextDouble() method. The above program can be written in different ways. The below program takes floating-point numbers and displays the sum to the output screen.

import java.util.Scanner;

public class Addition {
   public static void main(String[] args) {

      // create Scanner class object
      // to take input values
      Scanner scan = new Scanner(System.in);

      // declare variables
      double number1, number2, sum;

      // take input values
      System.out.print("Enter two floating-point numbers: ");
      number1 = scan.nextDouble();
      number2 = scan.nextDouble();

      // display result
      System.out.println("Sum = " + (number1 + number2));
   }
}

Output:-

Enter two floating-point numbers: 12.5 17.5
Sum = 30.0

Enter two floating-point numbers: 25.2 8.1
Sum = 33.3

Addition of Two Numbers in Java Using Method

Using the method we can also calculate the sum of two numbers. Methods are similar to functions in C/C++. it takes parameters and returns a value to the caller method. The below program uses a method add() to calculate the sum of two numbers. The add() method takes two double data type value, calculate the sum value and return it to the caller method.

import java.util.Scanner;

public class Addition {
   public static void main(String[] args) {

      // create Scanner class object
      // to take input values
      Scanner scan = new Scanner(System.in);

      // declare variables
      double num1, num2, sum;

      // take input values
      System.out.print("Enter two floating-point numbers: ");
      num1 = scan.nextDouble();
      num2 = scan.nextDouble();

      // method call
      sum = add(num1, num2);

      // display result
      System.out.println("Sum = " + sum);
   }

   // method to add two numbers
   private static double add(double n1, double n2) {
      // calculate sum and return it
      return n1 + n2;
   }
}

Sum of Two Numbers Using Command Line Arguments In Java

The command line is used to pass the arguments at the run time. Similar to other methods main() is also a method with a specialty that every Java program execution starts with the main method. The main() method takes the String array value as input and returns void i.e. nothing.

The passed parameters are in String so, first, we need to convert it into actual value and then calculate the sum value.

public class Addition {

   public static void main(String[] args) {

      // declare variables
      int num1, num2, sum;

      // convert to int value
      num1 = Integer.parseInt(args[0]);
      num2 = Integer.parseInt(args[1]);

      // calculate sum
      sum = num1 + num2;

      // display result
      System.out.println("Sum = " + sum);
   }

}

While executing this program, pass the values.

> javac Addition.java
> java Addition 10 20

30

If you don’t pass exactly two arguments then it will throw java.lang.ArrayIndexOutOfBoundsException.

> java Addition 10

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1
at Addition.main(Addition.java:10)

We have passed only one value, but our program calculates the sum of two numbers. Since the second number is not given so it throws an exception.

Similarly, if the passed value is not a number then JVM can’t convert to a number and throws java.lang.NumberFormatException

> java Addition a b

Exception in thread "main" java.lang.NumberFormatException: For input string: "a"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)

Since a and b are not a number that’s why we got an Exception.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.