Statement of C Program: This Program accepts n Integers and stores them in an Array called num. It also prints them:
#include<stdio.h>
#include<conio.h>
void main()
{
int num[20];
int n , i ;
clrscr();
printf(" Enter the size of an Array\n");
scanf("%d" , &n);
for(i = 0 ; i<n ; i++)
{
scanf("%d" , num[i]);
}
printf(" Array:\n ");
for(i = 0 ; i<n ; i++)
{
printf(" num[%d] = %d\n" , i , num[i]);
}
} /* End of main() */
Output:
Enter the size of an Array
5
Enter the elements
10
20
30
40
50
Array:
num[0] = 10
num[1] = 20
num[2] = 30
num[3] = 40
num[4] = 50
Explanation of the Program:
- Array declaration:
int num[20]; |
- int specifies the type of the variable.
- num specifies the name of variable.
- [20] , The number 20 tells how many elements of the type int will be in our Array. This number is often called 'dimension' of the Array.
- The bracket [ ] tells the compiler that we are dealing with Array.
- num[15] is not the 15th element of the Array but the 16th element.
- The number with in the square brackets also specifies the element's position in the Array.
- Entering Data into an Array:
for(i = 0 ; i<n ; i++)
{
scanf("%d" , num[i]);
}
- Reading Data from an Array:
for(i = 0 ; i<n ; i++)
{
printf(" num[%d] = %d\n" , i , num[i]);
}
That's All
0 Response to "WAP of C Language that accepts the size of Array and Stores the Element in num Array"
Post a Comment