Searching for a Number in an Array
In this program, we demonstrate how to search for a specific number within an array of integers. The user is prompted to enter the number of elements in the array and then input each element. After populating the array, the user provides a number to search for. The program iterates through the array to determine if the number exists and, if so, reports its position. This basic search algorithm helps in understanding array traversal and condition checking in C programming.
Code Breakdown
Algorithm
- Start
- Initialize Array and Variables: Create an array aof size 10, and variablesi,n,x, andfound.
- Input the Number of Elements: Prompt the user to enter the limit n(the number of elements in the array).
- Input Array Elements: Use a loop to input nelements into the arraya.
- Input the Number to Search: Prompt the user to enter the number xto search for in the array.
- Search for the Number: Initialize foundto 0. Iterate through the array:- Compare each element with x.
- If an element equals x, setfoundto 1 and break the loop.
 
- Compare each element with 
- Output the Result:
- If foundis 1, print the position ofxin the array.
- If foundis 0, print thatxis not found in the array.
 
- If 
- End
Code Explanation
Example Flowchart
                                   Start
                                     |
                                     V
                    +-----------------------------------+
                    |  Input the number of elements (n) |
                    +-----------------------------------+
                                     |
                                     V
                    +-----------------------------------+
                    | Input n elements into array (a[]) |
                    +-----------------------------------+
                                     |
                                     V
                    +----------------------------------+
                    |  Input the number to search (x)  |
                    +----------------------------------+
                                     |
                                     V
                    +----------------------------------+
                    |      Initialize found = 0        |
                    +----------------------------------+
                                      |
                                      V
                    +----------------------------------+
                    |       For i = 0 to n-1           |
                    |     (Iterate through array)      |
                    +----------------------------------+
                         |                          |
                         |                          |
                         V                          V
           +-------------------------+    +-------------------------+
           |    Is a[i] == x?        |    |    Is a[i] != x?        |
           +-------------------------+    +-------------------------+
                          |                          |
                          |                          |
                          V                          V
           +-------------------------+    +-------------------------+
           |     Set found = 1       |    |     Continue loop       |
           +-------------------------+    +-------------------------+
                          |                          |
                          V                          | 
           +-------------------------+               V
           |      Break loop         |              End
           +-------------------------+               
                          |                          
                          V                          
           +-------------------------+               
           |     Check found value   |               
           +-------------------------+               
                          |                          
            +-------------+-------------------+           
            |                                 |           
            V                                 V           
+-------------------------+       +-------------------------+
|   If found = 1          |       |      If found = 0       |
|   Print "x is found     |       |     Print "x is not     |
|   at location i+1"      |       |   found in the array"   |
+-------------------------+       +-------------------------+
            |                                  |
            V                                  V
           End                                End