C Programming – HackerRank Solution | READ ARRAY DISPLAY ARRAY |

(HackerRank) Write a Modular C Programming code to solve READ ARRAY DISPLAY ARRAY, Develop a modular C program to read an array and display an array of size N.

Develop a modular C program to read an array and display an array of size N.

Input Format

Read N – a number for array elements.

Read each array element and store it in array.

Constraints

N should be greater than 0.

Output Format

Display all array elements.

Sample Input 0

5
10 20 30 40 50

Sample Output 0

10 20 30 40 50 

Sample Input 1

0

Sample Output 1

Elements should be more than 0.

Sample Input 2

-5

Sample Output 2

Elements should be more than 0.

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[20],int n)
{
    int i;
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
}
void display_a(int a[20],int n)
{
    int i;
    for(i=0;i<n;i++)
    {
        printf("%d ",a[i]);
    }
}
int main() {

    int n,a[20];
    scanf("%d",&n);
    if(n<1)
    {
        printf("Elements should be more than 0.");
        exit(0);
    }
    read_a(a,n);
    display_a(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)

5
10 20 30 40 50
Your Output (stdout)

10 20 30 40 50 
Expected Output

10 20 30 40 50

 

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