C Programming – HackerRank Solution | SEARCH A NUMBER IN AN ARRAY |

(HackerRank) Write a Modular C Programming code to solve SEARCH A NUMBER IN AN ARRAY, Develop a modular program in c to search a number in an array of size n

Develop a modular program in c to search an number in an array of size n.

Input Format

Read n – number of elements in array.

Read elements of array.

Read key – a number to search in an array.

Constraints

n should be greater than 0.

Output Format

Display all elements of array.

Display search message.

Sample Input 0

7
56 234 91 63 912 -100 67
63

Sample Output 0

56 234 91 63 912 -100 67 
Search successfull. 63 found in list.

Sample Input 1

0

Sample Output 1

array should be greater than 0.

Sample Input 2

4
-123 348 -33 10
25

Sample Output 2

-123 348 -33 10 
Search unsuccessfull. 25 not found in list.

Refer : C Programming HackerRank all solutions for Loops | Arrays | strings

 

CODE:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

void read_a(int a[10],int n)
{
    int i;
    for(i=0;i<n;i++)
    {
        scanf(" %d ",&a[i]);
    }
}

void search_d(int a[10],int n)
{
    int i,key=0,status=0;
    scanf("%d",&key);
    for(i=0;i<n;i++)
    {
        printf("%d ",a[i]);
    }
    printf("\n");
    for(i=0;i<n;i++)
    {
        if(a[i]==key)
        {
            status=1;
            break;
        }
    }
    if(status==1)
    {
        printf("Search successfull. %d found in list.",key);
    }
    else if(status==0)
    {
        printf("Search unsuccessfull. %d not found in list.",key);
    }
}

int main() {
    int n,a[10];
    scanf("%d",&n);
    if(n<1)
    {
        printf("array should be greater than 0.");
        exit(0);
    }
    read_a(a,n);
    search_d(a,n);
    return 0;
}

OUTPUT

Congratulations, you passed the sample test case.

Click the Submit Code button to run your code against all the test cases.

Input (stdin)

7
56 234 91 63 912 -100 67
63
Your Output (stdout)

56 234 91 63 912 -100 67 
Search successfull. 63 found in list.
Expected Output

56 234 91 63 912 -100 67 
Search successfull. 63 found in list.

 

Please find some more codes of Loops, Condition Statements, 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, MISC, Solved model question papers & Hacker Rank all solutions on the below page:

Top 100+ C Programming codes – KLE Technological University

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

Welcome to FactsPrime

Sorry, We have detected that you have activated Ad-Blocker. Please Consider supporting us by disabling your Ad Blocker, It helps us in maintaining this website. To View the content, Please disable adblocker and refresh the page.

Thank You !!!