C Program to Compare Two Arrays

Write C Program to Compare Two Arrays

// C Program to Compare Two Arrays

#include <stdio.h>

int main()
{
	int A[] = {8, 7, 6, 5, 2, 3, 4, 1};
	int B[] = {8, 7, 6, 5, 2, 3, 4, 1};

	int i, j, flag = 0;

	int l1 = sizeof(A)/sizeof(A[0]);
	int l2 = sizeof(B)/sizeof(B[0]);

    printf("Array A :--> ");
	for(i=0; i<l1; i++)
        printf("%d  ", A[i]);

    printf("\nArray B :--> ");
	for(i=0; i<l2; i++)
        printf("%d  ", B[i]);

	if(l1 != l2)
	{
		flag = 1;
	}
	else
	{
 	for(i = 0;i < l1; i++)
		{
            if(A[i] != B[i])
            {
                flag = 1;
		break;
            }
		}
	}

	if(flag == 0)
	{
		printf("\n\nArrays are equal");
	}
	else
	{
		printf("\n\nArrays are not equal");
	}
	return 0;
}

Output:

Run 1:
Array A :--> 8  7  6  5  2  3  4  1
Array B :--> 1  2  3  4  5  6  7  8

Arrays are not equal

Run 2:
Array A :--> 8  7  6  5  2  3  4  1
Array B :--> 8  7  6  5  2  3  4  1

Arrays are equal

≪ Previous | Next ≫