Program to Check Array is Perfect or Not

Description

An array is called perfect if reverse of array matches with original array.
For checking perfect, reverse the array and compare with original array, if
it matches then it is perfect else not perfect.

C/C++

/* C program to Check Array is Perfect or Not */
//Save it as PerfectArray.c

#include<stdio.h>
int main(){

    int i, n;

    printf("Enter the size of array : ");
    scanf("%d",&n);

    int arr[n];
    int arrRev[n];

    printf("Enter the array element : ");
    for(i=0;i<n;i++) {
        scanf("%d",&arr[i]);
    }

    printf("Entered elements are : ");
    for(i=0;i<n;i++) {
        printf("%d ",arr[i]);
    }

    int count=0;
    for(i=n-1;i>=0;i--) {
        arrRev[count++] = arr[i];
    }

    printf("\nThe reverse of array is : ");
    for(i=0;i<count;i++) {
        printf("%d ",arrRev[i]);
    }

    int flag = 1;
    for(i=0;i<n;i++) {
        if(arr[i] != arrRev[i]) {
            flag = 0;
            break;
        }
    }

    if(flag == 1) {
        printf("\nThe array is PERFECT");
    }else {
        printf("\nThe array is NOT PERFECT");
    }

    return 0;
}
Input:
Enter the size of array : 
5
Enter the array element : 
4
6
1
9
7

Output:
Entered elements are : 
4 6 1 9 7 
The reverse of array is : 
7 9 1 6 4 
The array is NOT PERFECT

Java

/* Java program to Check Array is Perfect or Not */
//Save it as PerfectArray.java

import java.io.*;
import java.util.Scanner;

public class PerfectArray {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        
        System.out.println("Enter the size of array : ");
        int n = scanner.nextInt();
        
        int arr[] = new int[n];
        int arrRev[] = new int[n];
        
        System.out.println("Enter the array element : ");
        for(int i=0;i<n;i++) {
            arr[i] = scanner.nextInt();
        }
        
        System.out.println("Entered elements are : ");
        for(int i=0;i<n;i++) {
            System.out.print(arr[i]+" ");
        }
        
                //Reverse the array
        int count=0;
        for(int i=n-1;i>=0;i--) {
            arrRev[count++] = arr[i];
        }
        
        System.out.println("\nThe reverse of array is : ");
        for(int i=0;i<arrRev.length;i++) {
            System.out.print(arrRev[i]+" ");
        }
        
                //Checking reverse of array is matches with original array
        int flag = 1;
        for(int i=0;i<n;i++) {
            if(arr[i] != arrRev[i]) {
                flag = 0;
                break;
            }
        }
        
        if(flag == 1) {
            System.out.println("\nThe array is PERFECT");
        }else {
            System.out.println("\nThe array is NOT PERFECT");
        }
    }
}
Input:
Enter the size of array : 
5
Enter the array element : 
1
2
3
2
1

Output:
Entered elements are : 
1 2 3 2 1 
The reverse of array is : 
1 2 3 2 1 
The array is PERFECT

Related Programs

1) Program to Check Whether a Number is Prime or Not
2) Program to Check Whether a Character is a Vowel or Not
3) Program to Check Whether a Number is Even or Odd
4) Program To Check Armstrong Number
5) Program to Check Leap Year
6) Program to find all prime numbers in given range
7) Program to Find ASCII value of a character
8) Program to find largest and smallest digit in a Number
9) Program to find HCF using Recursion
10) Program to find factors of a Number
Share Me

Leave a Reply