Practice Program
Solution:
void main()
{
int i,element,a[10],n;
printf("\nEnter the number of element: ");
scanf("%d",&n);
//taking array element as input from user.
for(i=0;i<i++)
{
printf("\nEnter the element: ");
scanf("%d",&a[i]);
}
//printing array elements
printf("\nArray Elements are: ");
for(i=0;i<i++)
{
printf("%d\t",a[i]);
}
//taking number which we have to find
printf("\nEnter the number you want to find: ");
scanf("%d",&element);
//finding element.
for(i=0;i<i++)
{
if(element == a[i])
printf("\nElement is found at %d Position",i+1);
}
}
void main() { int i,element,a[10],n; printf("\nEnter the number of element: "); scanf("%d",&n); //taking array element as input from user. for(i=0;i<i++) { printf("\nEnter the element: "); scanf("%d",&a[i]); } //printing array elements printf("\nArray Elements are: "); for(i=0;i<i++) { printf("%d\t",a[i]); } //taking number which we have to find printf("\nEnter the number you want to find: "); scanf("%d",&element);//finding element. for(i=0;i<i++) { if(element == a[i]) printf("\nElement is found at %d Position",i+1); } }
Output1
Enter the number of elements: 5
Enter the Element: 1
Enter the Element: 2
Enter the Element: 3
Enter the Element: 4
Enter the Element: 5
Array Elements
5 4 3 2 1
Enter the number you want to find: 4
Element is found at 2 Position
Enter the Element: 1
Enter the Element: 2
Enter the Element: 3
Enter the Element: 4
Enter the Element: 5
Array Elements
5 4 3 2 1
Enter the number you want to find: 4
Element is found at 2 Position
Output2
Enter the number of elements: 10
Enter the Element: 1
Enter the Element: 2
Enter the Element: 3
Enter the Element: 4
Enter the Element: 5
Enter the Element: 6
Enter the Element: 7
Enter the Element: 8
Enter the Element: 9
Enter the Element: 10
Array Element are :
10 9 8 7 6 5 4 3 2 1
Enter the number you want to find: 5
Element is found at 6 Position
Enter the Element: 1
Enter the Element: 2
Enter the Element: 3
Enter the Element: 4
Enter the Element: 5
Enter the Element: 6
Enter the Element: 7
Enter the Element: 8
Enter the Element: 9
Enter the Element: 10
Array Element are :
10 9 8 7 6 5 4 3 2 1
Enter the number you want to find: 5
Element is found at 6 Position
Output3
Enter the number of elements: 15
Enter the Element:1
Enter the Element:2
Enter the Element: 3
Enter the Element: 4
Enter the Element:5
Enter the Element: 6
Enter the Element:7
Enter the Element: 8
Enter the Element: 9
Enter the Element:10
Enter the Element: 11
Enter the Element: 12
Enter the Element: 13
Enter the Element: 14
Enter the Element: 15
Array Element are :
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
Enter the number you want to find: 10
Element is found at 6 Position
Enter the Element:1
Enter the Element:2
Enter the Element: 3
Enter the Element: 4
Enter the Element:5
Enter the Element: 6
Enter the Element:7
Enter the Element: 8
Enter the Element: 9
Enter the Element:10
Enter the Element: 11
Enter the Element: 12
Enter the Element: 13
Enter the Element: 14
Enter the Element: 15
Array Element are :
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
Enter the number you want to find: 10
Element is found at 6 Position
defining a function 'find_element' which will return position of the element.
int find_element(int a[],int n,int element)
{
int i;
for(i=0;i<i++)
{
if(element == a[i])
return i;
}
}
int find_element(int a[],int n,int element) { int i; for(i=0;i<i++) { if(element == a[i]) return i; } }
Using above define function 'find_element()':
void main()
{
int i,res,a[10] = {10,12,13,14,15,16,17,18};
res = find_element(a,8,14);
printf("Element is found at %d Position",res+1);
}
void main() { int i,res,a[10] = {10,12,13,14,15,16,17,18}; res = find_element(a,8,14); printf("Element is found at %d Position",res+1); }
Output1
Element is found at 4 Position
Using above define function 'find_element()':
void main()
{
int i,res,a[10] = {21,22,23,24,25,26,27,28};
res = find_element(a,8,25);
printf("Element is found at %d Position",res+1);
}
void main() { int i,res,a[10] = {21,22,23,24,25,26,27,28}; res = find_element(a,8,25); printf("Element is found at %d Position",res+1); }
Output2
Element is found at 5 Position
Using above define function 'find_element()':
void main()
{
int i,res,a[10] = {31,32,33,34,35,36,37,38};
res = find_element(a,8,36);
printf("Element is found at %d Position",res+1);
}
void main() { int i,res,a[10] = {31,32,33,34,35,36,37,38}; res = find_element(a,8,36); printf("Element is found at %d Position",res+1); }
Output3
Element is found at 6 Position
0 Comments
Post a Comment