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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import java.util.Scanner; class AdditiveInverse { public static void main(String arg[]) { int n,c; Scanner sc=new Scanner(System.in); System.out.println("Enter number of elements "); n=sc.nextInt(); int a[]=new int[n]; int b[]=new int[n]; System.out.println("Enter " +n+" elements"); for(int i=0;i<n;i++) { a[i]=sc.nextInt(); } AI(a,b); System.out.print("AdditiveInverseOf("); print(a); System.out.print(")------->("); print(b); System.out.println(")"); } static void AI(int a[],int b[]) { for(int i=0;i<a.length;i++) { b[i]=a[i]*-1; } } static void print(int a[]) { for(int i=0;i<a.length;i++) { System.out.print(a[i]+" "); } } } |
Output – 1:
- Solution :
- The main method begins by declaring two int variables n and c.
- 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.
- 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.
- 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).
- 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.
- Two arrays namely a and b are initialized having size n as defined by the user. Array a will be used to store the input numbers and array b will store their additive inverses.
1 2 3 4 |
System.out.println("Enter " +n+" elements"); for(int i=0;i<n;i++) { a[i]=sc.nextInt(); } |
- The user is asked to enter n number of elements and as the loop runs, each input int value is stored in position i of that iteration in the array a.
1 2 3 4 5 |
static void AI(int a[],int b[]) { for(int i=0;i<a.length;i++) { b[i]=a[i]*-1; } } |
- The method AI is called to calculate the additive inverse and arrays a and b 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.
- As the loop in this method iterates, each element in array a 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 2 3 4 5 |
static void print(int a[]) { for(int i=0;i<a.length;i++) { System.out.print(a[i]+" "); } } |
- 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 b is passed in the other time.
- This method iterates over all the elements of each array and prints them out to the console.
1 2 3 4 5 6 7 8 9 |
Enter number of elements 5 Enter 5 elements 2 -4 -5 -1 -45 AdditiveInverseOf(2 -4 -5 -1 -45 )------->(-2 4 5 1 45 ) |
- In this case, array a 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:
1 2 3 4 5 |
Enter number of elements 4 Enter 4 elements 5 -7 8 3 AdditiveInverseOf(5 -7 8 3 )------->(-5 7 -8 -3 ) |
- In this case, array a 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.