A program for Searching for a number in an array and display their position

 Searching for a number and display their position



        In this example, we will  understand to Searching for a number in an array and display their position and know how to write, compile and debug it in C language and also learn how to implement it in c.

        
In order to do this you will need
  • Basic knowledge of c programming language.
  • As well as know how to operate codeblocks.   

            An array is a fixed-size sequential collection of  elements of same data types that share a common  name.It is simply a group of data types. An array is a  derived data type.An array is used to represent a list of numbers or  a list of names.


So let's begin with our program

1.Open a codeblocks software.

2.Open new empty file.

3.Copy and paste the code from below. 






Input code-

 //program for Searching for a number in an array and display their position  
   
 #include <stdio.h>  
 int main()  
 {  
  int array[100],search,i,n;  
   
  printf("Enter number of integers in array\n");  
  scanf("%d",&n);  
   
  printf("Enter %d numbers\n",n);  
   
  for (i=0;i<n;i++)  
   scanf("%d", &array[i]);  
   
  printf("Enter a number to search:");  
  scanf("%d",&search);  
   
  for (i=0;i<n;i++)  
  {  
   if (array[i] == search)  
   {  
    printf("%d is present at location %d\n", search, i+1);  
    break;  
   }  
  }  
  if (i==n)  
   printf("%d isn't present in the array\n", search);  
   
  return 0;  
 }  
   



Now simply build the code. Look for any error. Now run the code.

For output enter 5 then enter any five number you want and press enter key then enter the number you want to find and then press enter key.





Click the following button to download a program to find Sum of Digits of a Number using functions.






Thats it.Thank you for scrolling.






Post a Comment

If you have any doubt or suggestions.please let me know.

Previous Post Next Post

Ads by google

Ads by google