In this tutorial you will learn about the C Program to Find Common Elements in Two Array and its application with practical example.
C Program to Find Common Elements in Two Array
In this tutorial, we will learn to create a C program that will Find Common Elements in Two Arrays using C programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- For loop in C Programming.
Find Common Elements in Two Array:-
As we all know array is a collection of similar data type elements. In an array, only one variable is declared which can store multiple values. First will take the number of elements of an array from the user. Then will take the elements from the user for the array. And at last, we will Find Common Elements in Two Array C Programming Language.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 |
1. Declaring the variables for the program. 2. Taking the array size from the user. 3. Taking the elements of the array. 4. Sorting the common elements of the array. 5. Printing the results. 6. End program. |
Program:-
Find Common Elements in Two Array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
#include<stdio.h> #define max 100 //function to print the common elements from two array int ifexists(int z[], int u, int v) { int i; if (u==0) return 0; for (i=0; i<=u;i++) if (z[i]==v) return (1); return (0); } void main() { //declaring the variables int p[max], q[max], r[max]; int m,n; int i,j,k; k=0; //taking input size of array 1 printf("Enter the length of the first array:"); scanf("%d",&m); //taking elements of array 1 printf("Enter %d elements of the first array\n",m); for(i=0;i<m;i++ ) scanf("%d",&p[i]); //taking input size of array 2 printf("\nEnter the length of the second array:"); scanf("%d",&n); //taking elements of array 2 printf("Enter %d elements of the second array\n",n); for(i=0;i<n;i++ ) scanf("%d",&q[i]); k=0; //Cecking the common elements of two array //sending it to a userdefined function to print common elements for (i=0;i<m;i++) { for (j=0;j<n;j++) { if (p[i]==q[j]) { if(!ifexists(r,k,p[i])) { r[k]=p[i]; k++; } } } } if(k>0) { printf("\nThe common elements in the two arrays are:\n"); for(i = 0;i<k;i++) printf("%d\n",r[i]); } else printf("There are no common elements in the two arrays\n"); } |
Output:-
In the above program, we have first initialized the required variable
- p[max] = it will hold the elements in an array.
- q[max] = it will hold the elements in an array.
- r[max] = it will hold the elements in an array.
- n = it will hold the number of elements in an array.
- m = it will hold the number of elements in an array.
- i = it will hold the integer value to control the array.
- j = it will hold the integer value to control the array.
- k = it will hold the integer value to control the array.
Taking input from the user in an array number of elements in the array.
Checking the array,
Printing the results.