Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Tuesday, August 30, 2022

Input and Display the elements of Array in C programming

 Input and Display the elements of Array in C programming


By directly defining the array size.

#include<stdio.h>

#include<conio.h>

main()

{

int arr[5],i;

printf("Enter the value for array:\n");

for(i=0;i<5;i++)

{

  scanf("%d",&arr[i]);

}

printf("\n");

printf("The array elements are :\n");

 for(i=0;i<5;i++)

 {

        printf("%d\n",arr[i]);

 }

  getch();

}

The output of the program:

 Note: In above program array size 5 is direrectly define by the user and five elements which are input by user stored in that elements.


By using constant value for array

#include<stdio.h>

#include<conio.h>

int main()

{

int i,num[5]= {3,56,76,5,90};

printf("List of array\n");

for(i=0;i<5;++i)

{

         printf("%d\t",num[i]);

}

getch();

}

The output of the program:

 Note: In above program size of the array is 5 already defined and elements are also fixed for that elements.


By inputting the size of the array from the user

#include <stdio.h>

#include <conio.h>

main()

{

int n, i;

printf("Enter the size of the arrays:\n");

scanf("%d", &n);

int arr[n];

printf("Enter %d the elements of the array:\n",n);

for (i=0; i<n; i++)

{

    scanf("%d", &arr[i]);

}

 printf("The array elements are:\n");

{

         for(i=0;i<n;i++)

         printf("%d\n",arr[i]);

}

getch();

}

The output of the program:


Note: In above program the size of the array is input by the user so on the basis of that array size can be declared and elements are stored on that.


No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages