Linear Search.(www.programmingsimplified.com)
#include <stdio.h>
int main()
{
//पहले ज़रूरत वाली हर variable को declare कीजिए
int array[100], search, c, n;
//elements की संख्या डालिए
printf("Enter number of elements in array\n");
scanf("%d", &n);
//संख्या भरिए for loop ➰ जी की सहायता से
printf("Enter %d integer(s)\n", n);
//c variable को array index बनाकर c=0 initialise कीजिए,
//फ़िर,c variable को (n-1) variable तक दौड़ाइए
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
//जो संख्या खोजना है, फ़िर वो लिखिए
printf("Enter a number to search\n");
scanf("%d", &search);
for (c = 0; c < n; c++)
{
//देखिए अगर array[c] और search बराबर हो जाएतो that number is present at that location पे print करवाइए//
if (array[c] == search) /* If required element is found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
//अगर c==n हो जाए तो समझ लें कि search-key,array में है ही नहीं क्योंकि,array (0) से (n-1) तक ही है!
if (c == n)
printf("%d isn't present in the array.\n", search);
return 0;
}
/*Enter number of elements in array
7
Enter 7 integer(s)
3 9 4 9 2 0 6
Enter a number to search
2
2 is present at location 5.
[Process completed - press Enter]*/
Comments
Post a Comment