Introduction
In this post, I am going to write a c program to find duplicate numbers from the 3 by 3 matrix. I have easily written this program.
Also Read: Switch Case in C Program to Calculate Area of Circle and Triangle
The logic of this program is so simple. Here, I am converting this multi-dimensional array into a single-dimensional array. Then it has become very easy to find out duplicate numbers.
Before going further, let us see the expected output first.
Expected Output

As you can see in the above figure, we are reading the elements for the 3 by 3 matrix. After that, we are displaying the same matrix. After displaying the matrix, we will display the duplicate numbers.
Also Read: C Program to Print Multiples of 5 using do-while loop
Now see the actual c program.
C Program to Find Duplicate Numbers From the 3 by 3 Matrix
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[3][3],i,j;
int sa[10],k=0;
printf("Enter elements for 3 * 3 matrix\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
sa[k]=a[i][j];
k++;
}
}
printf("Matrix Elements are\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("Duplicate Array Elements are\n");
for(int i=0;i<9; i++)
{
for(int j=i+1;j<9;j++)
{
if(sa[i] == sa[j])
{
printf("%d ", sa[j]);
}
}
}
return 0;
}
Explanation
Now, I am going to explain this program.
Variable Declaration
In this program, I have declared five variables of type int. Out of which, one is a two-dimensional array, one single dimensional array, two-loop variables, and one counter variable.
Input Part
Here, we are asking the user to enter elements for the 3 * 3 matrix. We are storing these elements in the array a[][].
At the same time, I am also storing these array elements from a two-dimensional array to a one-dimensional array.
Processing Part and Output
Using two loops, we are finding the duplicate numbers in the given array. The first element of the array is compared with all the elements until we get our duplicate number. If we get the duplicate number, we will print that number and then move to the next duplicate number.
I hope, this program will help you. Thanks for reading.
Important C Programs
- Program in C to Find Longest Line in a File
- Palindrome in C using Pointers
- Insert and Delete element in Array in C using switch case
- C Program to Add Alternate Elements of a 2D Array
- Arrays in C for Complete Beginners
- C Program to Find Area of a Circle using Preprocessor
- Program in C to Remove White Spaces and Comments from a File
- C Program to Print Numbers Except Multiples of n
- Reverse a Number using getchar and putchar function in c
- The while loop in C Programming