Array in c performs to store related data under a single variable name with an index, also known as a subscript. It is easiest to think that it is a list of ordered grouping for variables of the same type. Actually we can store more than one value in a single variable.
Declaration of Array:
We can declare an array by specifying its type and size or by initializing it or by both. type arrayName [ arraySize ];
int array[5] = {5, 9, 6, 3, 7};
Q:
Put five array elements then print those elements.
#include<stdio.h>
int main()
{
int n[5],i;
for(i=0;i<5;i++)
{
printf(“\nEnter Number”);
scanf(“%d”,&n[i]);
}
for(i=0;i<5;i++)
{
printf("%d “,n[i]);
}
return 0;
}
Output:
Enter Number
5
Enter Number
7
Enter Number
8
Enter Number
4
Enter Number
3
5 7 8 4 3
#include<stdio.h>
int main()
{
int n[ 5 ]; /* n is an array of 10 integers */
int i,ch,f=0;
/* initialize elements of array n to 0 */
for ( i = 0; i < 5; i++ ) {
printf("\nEner Number");
scanf("%d",&n[ i ]); /* set element at location 0 */
}
printf("\nEnter Number to search");
scanf("%d",&ch);
/* output each array element's value */
for (i=0; i<5;i++ ) {
printf("Element[%d] = %d\n", i, n[i] );
if(n[i]==ch){
f=1;
}
}
if(f==1){
printf("\nSearching Number %d found",ch);
}
else
{
printf("\nSearching Number %d Not found",ch);
}
return 0;
}
Output:
0 Comments