Write a c program to search an element in an array using
recursion



Write a c program to search an element in an array using recursion ..

Answer / lel

#include<stdio.h>
#include<conio.h>
void search(int a[], int k, int n, int j);
int main()
{
int a[12],i,k,j=0,n;
printf("Enter the no. of ele : \n");
scanf("%d",&n);
printf("Enter the ele\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the ele you want to search : \n");
scanf("%d",&k);
search(a,k,n,j);
getch();
return 0;
}
void search(int a[], int k, int n, int j)
{
if(j<n)
{
if(k==a[j])
{
printf("The ele is present. It is %d at a[%d]",a[j],j);
}
else
{
search(a,k,n,j+1);
}
}
else
printf("The ele you are searching for is not present !!");
}

Is This Answer Correct ?    7 Yes 2 No

Post New Answer

More C Code Interview Questions

What is wrong with the following code? int *foo() { int *s = malloc(sizeof(int)100); assert(s != NULL); return s; }

1 Answers  


#include"math.h" void main() { printf("Hi everybody"); } if <stdio.h> will be included then this program will must compile, but as we know that when we include a header file in "" then any system defined function find its defination from all the directrives. So is this code of segment will compile? If no then why?

2 Answers  


int i,j; for(i=0;i<=10;i++) { j+=5; assert(i<5); }

3 Answers   Cisco, HCL,


main() { int i=_l_abc(10); printf("%d\n",--i); } int _l_abc(int i) { return(i++); }

2 Answers  


main() { int i=5; printf("%d%d%d%d%d%d",i++,i--,++i,--i,i); }

7 Answers  






main(){ unsigned int i; for(i=1;i>-2;i--) printf("c aptitude"); }

2 Answers  


#include<stdio.h> main() { struct xx { int x; struct yy { char s; struct xx *p; }; struct yy *q; }; }

2 Answers  


posted by surbhi just now main() { float a = 5.375; char *p; int i; p=(char*)&a; for(i=0;i<=3;i++) printf("%02x",(unsigned char) p[i]); } how is the output of this program is :: 0000ac40 please let me know y this output has come

2 Answers   GATE,


main() { int i=300; char *ptr = &i; *++ptr=2; printf("%d",i); }

4 Answers   CSC,


Can you send Code for Run Length Encoding Of BMP Image in C Language in linux(i.e Compression and Decompression) ?

0 Answers   Honeywell,


How to read a directory in a C program?

4 Answers  


how can u draw a rectangle in C

53 Answers   Accenture, CO, Codeblocks, Cognizant, HCL, Oracle, Punjab National Bank, SAP Labs, TCS, University, Wipro,


Categories