﻿ C - Common element(s) of two sequences

# C Exercises: Common element(s) of two sequences

## C Basic-II: Exercise-4 with Solution

Write a C program that accepts two sequences ((a1, a2, .. an), (b1, b2, .. bn)) of integers as input. Find all integers that appear in both sequences, one by one, in ascending order.

Constraints:

• 1≤N1≤100, N1 -> number of integers in first sequence.
• 1≤N2≤100, N2 -> number of integers in second sequence.
• 1 ≤ ai≤ 100 ( 1 ≤ i ≤ N1 )
• 1 ≤ bj≤ 100 ( 1 ≤ j ≤ N2 )

Sample Date:

( 1 2 3 1 2 3 4) -> 1 2 3
( 1 2 3 1 2 3) -> 1 2 3
(1 2 3 4 5 6) ->

C Code:

``````#include <stdio.h>
int N1,N2,n[100];
int main()
{
int i,x;
printf("Number of integers want to input in the first sequence: ");
scanf("%d",&N1);
printf("Input the numbers:\n");

for(i=0;i<N1;i++)
{
scanf("%d",&x);
n[x] = 1;
}
printf("Number of integers want to input in the second sequence: ");
scanf("%d",&N2);
printf("\nInput the numbers:\n");
for(i=0;i<N2;i++)
{
scanf("%d",&x);
n[x] |= 2;
}
printf("\nCommon elements of the said sequences:\n");
for(i=0;i<=100;i++)
if(n[i]==3)
printf("%d\n",i);
return(0);
}
``````

Sample Output:

```Number of integers want to input in the first sequence: 3
Input the numbers:
1 2 3
Number of integers want to input in the second sequence: 3

Input the numbers:
1 2 3

Common elements of the said sequences:
1
2
3
100
```

Flowchart:

C Programming Code Editor:

Previous C Programming Exercise: Second largest among three integers.
Next C Programming Exercise: Sum values before, after the maximum value in a sequence.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿

```MY_TYPE a = { .flag = true, .value = 123, .stuff = 0.456 };