Java – Array Programs

Java – Check the first element Even or not

class Code
{
      public static void main(String[] args)
      {
                  int[] arr = {4, 2, 8, 9, 1, 6, 7, 5};
                  int first = arr[0];
                  if(first%2==0)
                              System.out.println(“First element is Even”);
                  else
                              System.out.println(“First element is not Even”);
      }
}

Java – Check the sum of first and last elements is equals to 10 or not

class Code
{
      public static void main(String[] args)
      {
                  int[] arr = {4, 2, 8, 9, 1, 6, 7, 5};
                  int first = arr[0];
                  int last = arr[arr.length-1];
                  if(first+last==10)
                              System.out.println(“Equals”);
                  else
                              System.out.println(“Not Equals”);
      }
}

Java – Program to check first and last elements of Array are same or not

class Code
{
      public static void main(String[] args)
      {
                  int[] arr = {4, 2, 8, 9, 1, 6, 7, 5};
                  if(arr[0]==arr[arr.length-1])
                              System.out.println(“Same”);
                  else
                              System.out.println(“Not Same”);
      }
}

Java – Program to Check the length of array is even or not

class Code
{
      public static void main(String[] args)
      {
                  int[] arr = {4, 2, 8, 9, 1, 6, 7, 5};
                  if(arr.length%2==0)
                              System.out.println(“Even length array”);
                  else
                              System.out.println(“Not an Even length array”);
      }
}

Java – Check the last element of array is even or not

class Code
{
      public static void main(String[] args)
      {
                  int[] arr = {4, 2, 8, 9, 1, 6, 7, 5};
                  int n = arr.length;
                  if(arr[n-1]%2==0)
                              System.out.println(“Last element of Array is Even”);
                  else
                              System.out.println(“Last element of array is not even”);
      }
}

Java – Program to Check the length of array is prime or not

  • Prime Number : A number which is having 2 factors
  • First we need to find the length of array into variable n
  • Count the factors of n value
  • If the count is 2, then it is prime number
class Code
{
      public static void main(String[] args)
      {
                  int[] arr = {4, 2, 8, 9, 1, 6, 7};
                  int n = arr.length;
                  int count=0;
                  for (int i=1 ; i<=n ; i++)
                  {
                              if(n%i==0)
                                          count++;
                  }
                  if(count==2)
                              System.out.println(“Array length is prime”);
                  else
                              System.out.println(“Array length is not prime”);
      }
}

Java – Check the first element of array is perfect number or not

  • Perfect Number : Sum of factors except itself is equals to Perfect number
  • Example : n=6 -> factors = 1, 2, 3, 6 -> 1+2+3 = 6
class Code
{
      public static void main(String[] args) {
                  int[] arr = {6, 2, 8, 9, 1, 6, 7, 5};
                  int n = arr[0];
                  int sum=0;
                  for (int i=1 ; i<n ; i++){
                              if(n%i==0)
                                          sum = sum+i;
                  }
                  if(sum==n)
                              System.out.println(“First element is Perfect Number”);
                  else
                              System.out.println(“First element is Not perfect Number”);
      }
}

Java – Print the factorial for first element in array

class Code
{
      public static void main(String[] args)
      {
                  int[] arr = {6, 2, 8, 9, 1, 6, 7, 5};
                  int n = arr[0];
                  int fact=1;
                  for (int i=1 ; i<=n ; i++)
                  {
                              fact = fact*i;
                  }
                  System.out.println(“Factorial value is : ” + fact);
      }
}

Java – Display the multiplication table of last element in the array

class Code
{
      public static void main(String[] args)
      {
                  int[] arr = {6, 2, 8, 9, 1, 6, 7, 5};
                  int n = arr[arr.length-1];
                  for (int i=1 ; i<=10 ; i++)
                  {
                              System.out.println(n + ” x ” + i + ” = ” + (n*i));
                  }
      }
}
Scroll to Top