Finding difference in java example:

Java program to find the difference:
Takes array of integer inputs and finding the difference

package in.javadomain;

public class FindDifference {
	public static void main(String[] args) {
		// inputs here
		int[] i = { 1,11,21,31 };
		int temp = i[1] - i[0];
		int k = 1;
		int lastValue = i[i.length - 1];
		boolean logicCheck = false;
		boolean wrongLogic = true;
		for (int j = 0; j < i.length; j++) {
			if (k < i.length) {
				if (i[j] == lastValue) {
					if (wrongLogic) {
						logicCheck = true;
					} else {
						logicCheck = false;
					}
				} else {
					if ((i[j + k] - i[j]) == temp) {
						logicCheck = true;
					} else {
						logicCheck = false;
						wrongLogic = false;
					}

				}

			}

		}
		if (logicCheck) {
			System.out.println("Difference is :::" + temp);
		} else {
			System.out.println("Not able to find, sorry!!");
		}
	}
}

Output:

Difference is :::10

Recommended Java Books:

372 total views, 2 views today

Leave a Reply

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