w3resource

Java: Check if an array of integers contains three increasing adjacent numbers

Java Basic: Exercise-107 with Solution

Write a Java program to check if an array of integers contains three increasing adjacent numbers.

Pictorial Presentation:

Java Basic Exercises: Check if an array of integers contains three increasing adjacent numbers

Sample Solution:

Java Code:

import java.util.*;

public class Exercise107 {
    public static void main(String[] args) {
        // Initialize an integer array with some values
        int[] array_nums = {11, 12, 13, 14, 45, 20};

        // Print the original array
        System.out.println("Original Array: " + Arrays.toString(array_nums));

        // Initialize a result variable to 1
        int result = 1;

        // Iterate through the array to check for consecutive integers
        for (int i = 0; i <= array_nums.length - 3; i++) {
            // Check if the current number, the next, and the one after that are consecutive
            if (array_nums[i] + 1 == array_nums[i + 1] && array_nums[i + 1] + 1 == array_nums[i + 2]) {
                // If they are consecutive, set the result to 0
                result = 0;
            }
        }

        // Check the result and print either true or false
        if (result == 1) {
            System.out.printf(String.valueOf(false));
        } else {
            System.out.printf(String.valueOf(true));
        }

        // Print a newline character for formatting
        System.out.printf("\n");
    }
}

Sample Output:

Original Array: [11, 12, 13, 14, 45, 20]                               
true

Flowchart:

Flowchart: Java exercises: Check if an array of integers contains three increasing adjacent numbers

Java Code Editor:

Previous: Write a Java program to create a new array that is left shifted from a given array of integers.
Next: Write a Java program to add all the digits of a given positive integer until the result has a single digit.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/java-exercises/basic/java-basic-exercise-107.php