Searching using pointers in C


Levels of difficulty: / perform operation:

C program to search a given key number within n given numbers using pointers. If the key number exists print found otherwise print not found.

Program

  
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
void main()
{
	int n,*p,i,num,flag=0;
	clrscr();
	printf("\nHOW MANY NUMBER: ");
	scanf("%d",&n);
	p=(int *) malloc(n*2);
	if(p==NULL)
	{
		printf("\nMEMORY ALLOCATION UNSUCCESSFUL");
		exit();
	}
	for(i=0;i<n;i++)
	{
		printf("\nENTER NUMBER %d: ",i+1);
		scanf("%d",p+i);
	}
	printf("\nENTER A NUMBER TO SEARCH: ");
	scanf("%d",&num);
	for(i=0;i<n;i++)
	{
		if(num==*(p+i))
			flag=1;
	}
	if(flag==1)
		printf("\nTHE NUMBER %d IS FOUND",num);
	else
		printf("\nTHE NUMBER %d DOES NOT EXIST",num);
	getch();
}


Output