filmov
tv
Input data into an Integer Array and print all values from that array using C Programming Language

Показать описание
Input data into the array in C
If the array size is four, we iterate from index=0 to index=3 in the array. We have written a printf statement(to display the output) in the For loop. So the message Enter number will print four times, and then we have written scanf statement(to input the values). This statement will ask the user to input the value. And now, that value entered by the user will be stored in an array.
Reading out data from an Array in C
Suppose we want to display the output then again, we have to use the For loop and write a printf statement in it.
Syntax:
for(int i = 0; i less that 8; i++) { printf("%d ", array_name[i]); // print data of array with a space in between } printf("\n"); // move to the next line after printing all elements
Copy code
Example-
for (int x = 0; x less than 4; x++)
{
printf("num[%d] = %d\n", x, num[x]);
}
Copy code
Access array elements
For accessing an array, you can use an index or array subscript. The array starts with index=0, which means arr[0] represents the first element in the array arr.
In the below example, there are n=15 elements in an array, then myarr[15-1], i.e. myarr[14], can be used to access an nth element of an array.
int myarr[15];
printf("%d \n", myarr[0]); /* first element of array mydata*/
printf("%d \n", myarr[14]); /* last (14th) element of array mydata*/
Regards,
Md. Ahsan Arif
University of Scholars
#cprogramming #cprogrammingtutorial #cprogrammingtutorialforbeginners #universityofscholars #ius #banglatutorial #bangladesh #bangla
If the array size is four, we iterate from index=0 to index=3 in the array. We have written a printf statement(to display the output) in the For loop. So the message Enter number will print four times, and then we have written scanf statement(to input the values). This statement will ask the user to input the value. And now, that value entered by the user will be stored in an array.
Reading out data from an Array in C
Suppose we want to display the output then again, we have to use the For loop and write a printf statement in it.
Syntax:
for(int i = 0; i less that 8; i++) { printf("%d ", array_name[i]); // print data of array with a space in between } printf("\n"); // move to the next line after printing all elements
Copy code
Example-
for (int x = 0; x less than 4; x++)
{
printf("num[%d] = %d\n", x, num[x]);
}
Copy code
Access array elements
For accessing an array, you can use an index or array subscript. The array starts with index=0, which means arr[0] represents the first element in the array arr.
In the below example, there are n=15 elements in an array, then myarr[15-1], i.e. myarr[14], can be used to access an nth element of an array.
int myarr[15];
printf("%d \n", myarr[0]); /* first element of array mydata*/
printf("%d \n", myarr[14]); /* last (14th) element of array mydata*/
Regards,
Md. Ahsan Arif
University of Scholars
#cprogramming #cprogrammingtutorial #cprogrammingtutorialforbeginners #universityofscholars #ius #banglatutorial #bangladesh #bangla