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:
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:
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