Java: Test if 10 appears as either the first or last element of an array of integers
10 as First or Last in Array
Write a Java program to test if 10 appears as the first or last element of an array of integers. The array length must be broader than or equal to 2.
Sample Solution:
Java Code:
import java.lang.*;
public class Exercise74 {
public static void main(String[] args) {
// Define an integer array
int[] num_array = {10, -20, 0, 30, 40, 60, 10};
// Check if the first or last element of the array is equal to 10
boolean result = (num_array[0] == 10 || num_array[num_array.length - 1] == 10);
// Print the result
System.out.println(result);
}
}
Sample Output:
true
Pictorial Presentation:
Flowchart:

For more Practice: Solve these Related Problems:
- Modify the program to check for 20 instead of 10.
- Write a program to check if an array starts or ends with an even number.
- Modify the program to work with arrays of different lengths.
- Write a program to check if the first and last elements are the same.
Java Code Editor:
Previous: Write a Java program to create a new string taking first and last characters from two given strings.
Next: Write a Java program to test if the first and the last element of an array of integers are same.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics