,

How to take 1-D array elements as input?

  • How to take array as input and print its value?
  • Generally, the number of element that number of time you have write scanf() and take input as an single number and store it at respected index.
  • Lets see an example regarding to whatever you read at above.

    Q:declare int array of size 5 and take 5 element from the user and print them sequentialy.

    Solution:

    #include<stdio.h>
    
    void main()
    {
        int a[5];
        printf("\nEnter the first element:");   //taking input for first element and storing at index 0
        scanf("%d",&a[0]);
         printf("\nEnter the first element:");
        scanf("%d",&a[1]);                       //taking input for second element and storing at index 1
         printf("\nEnter the first element:");
        scanf("%d",&a[2]);                       //taking input for third element and storing at index 2
         printf("\nEnter the first element:");
        scanf("%d",&a[3]);                       //taking input for fourth element and storing at index 3
         printf("\nEnter the first element:");
        scanf("%d",&a[4]);                       //taking input for fifth element and storing at index 4
    
          //printing the array elements sequentially
        printf("\nFirst element is %d",a[0]);
         printf("\nSecond element is %d",a[1]);
          printf("\nThird element is %d",a[2]);
           printf("\nFourth element is %d",a[3]);
            printf("\nFifth element is %d",a[4]);
    }
    

    Output:
    Enter the first element:4
    Enter the second element:5
    Enter the third element:6
    Enter the fourth element:8
    Enter the fifth element:9
    First element is 4
    Second element is 5
    Third element is 6
    Fourth element is 8
    Fifth element is 9
  • Taking input of an array element like above we did is not an good approach of taking input,because repeatedly we were writing the almost same piece of code.
  • This problem can be solve with taking input by applying looping functions like while,do-while,for loops.
  • Lets do the same above problem ,but this time by better approach.

    Solution:

    #include<stdio.h>
    
    void main()
    {
        int a[5];
       int i;
       for(i=0;i<5;i++)
       {   printf("enter the element of %d index",i);
           scanf("%d",&a[i]);//storing the value at ith index at every iteration 
       }
       for(i=0;i<4;i++)
       {
           printf("\nthe element at %d index is %d",i,a[i]);
       }     //printing the value for ith index at  every iteration
    }
    

    Output:
    enter the element of 0 index:4
    Enter element of 1 inidex:5
    enter the element of 2 index:6
    enter the element of 3 index:8
    enter the element of 4 index:9
    the element at 0 index is 4
    the element at 1 index is 5
    the element at 2 index is 6
    the element at 3 index is 8
    the element at 4 index is 9




Further Concepts:

Practice Programs

  • Programs regarding Arrays
  • Programs Regarding 2d arrays
  • Programs regarding matrix
  • And lot more


Further Topics:


People Also Searched: