Latest :

Java Program to Find Additive Inverse | Java Programs

Finding Additive Inverse through a Java Program – This specific article expresses the code for finding the additive inverse Java Program of a number.

  • The problem here is to find the additive inverse of a user-defined number. The additive inverse of a number a is the number that, when added to a, yields 0 (example: Additive Inverse of 2 is -2 because 2 + (-2) = 0).
  • The constraint here is taking in only integers to find the additive inverse. The code here does not support decimal input.
  • The input here is several integers input by the user.
  • The output is the additive inverse of each of the integer inputs by the user.

Output – 1:

  • Solution :
  1. The main method begins by declaring two int variables and c. 
  2. A new Scanner class object is initialized and a reference variable sc is set to represent the object. This class is used to take user input in Java.
  3. Scanner class is part of the java.util package and hence the import statement, in the beginning, is stated to import the functionality of the specified class.
  4. The next line prints a statement to the console instructing the user to enter the number of elements (i.e. all the numbers whose additive inverse is to be calculated).
  5. The .nextInt() method is called by the Scanner object sc. This method reads an int value input by a user and stores it to the int variable n.
  6. Two arrays namely a and b are initialized having size as defined by the user. Array will be used to store the input numbers and array will store their additive inverses.



  1. The user is asked to enter number of elements and as the loop runs, each input int value is stored in position of that iteration in the array a.



  1. The method AI is called to calculate the additive inverse and arrays a and are passed in as arguments. This method is a static method which means it does not belong to any object of the class AdditiveInverse but belongs to the class itself.
  2. As the loop in this method iterates, each element in array is multiplied by -1 to determine its additive inverse and is stored in the corresponding position in array b (example: 2*-1 = -2 and -2 is the additive inverse of 2). Array b now holds all the additive inverses of the input integers.



  1. Within the following print statements in the main method, the print method is called twice. The given array “a” containing input integers is passed in as the argument once and array is passed in the other time.
  2. This method iterates over all the elements of each array and prints them out to the console.



  • In this case, array holds the elements 2,-4,-5,-1,-45. The additive inverse for each is obtained by multiplying each number by -1.
  • The output of -2,4,5,1,45 is the correct additive inverse for each integer.


Output – 2:


  • In this case, array holds the elements 5,-7,8,3. The additive inverse for each is obtained by multiplying each number by -1.
  • The output of -5,7,-8,-3 is the correct additive inverse for each integer.
  • Thus, the additive inverse java program concludes.

Check Also

Rhombus Star Pattern Program In Java – Patterns

Java program to print Rhombus star pattern program. We have written the below print/draw Rhombus ...