Finding difference in java example:
Java program to find the difference:
Takes array of integer inputs and finding the difference
[java gutter=”false”]
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!!");
}
}
}
[/java]
Output:
[plain gutter=”false”]
Difference is :::10
[/plain]
Recommended Java Books: