Find a number from given array

Write a program to find a number from given array and if it is not present then add it at the end of the array position.


Source Code:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,a[20],t,n,c=0;
clrscr();
printf("Enter n : ");
scanf("%d",&n);
printf("Enter array elements : \n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("The array elements are \n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
printf("Enter the element you wanna search : ");
scanf("%d",&t);
for(i=0;i<n;i++)
{	
if(a[i]==t)
{
c++;
}
}	
if(c==0)
{
a[n]=t;
printf("Element is not present in array \n");
printf("New array\n");
for(i=0;i<=n;i++)
{
printf("%d\n",a[i]);
}	
}
else
{
printf("%d is present in array %d times",t,c);
}
getch();
}

Output:

Enter no: 4
Enter array elements: 
1
5
3
7
The array elements are
1
5
3
7
Enter the element you want search: 10
Element is not present in array
New array
1
5
3
7
10

Leave a Reply

Your email address will not be published. Required fields are marked *