How to scan an array and how to print it on screen.
How to Scan and Print an Array in C Programming Language
To scan an array and print it on the screen in C language, you can use a for loop to iterate over the array elements and scanf to read the input values from the user, and printf to display the output values on the screen. Here's an example program that shows how to do this:
input:
10 | 20 | 30 | 40 | 50 |
output:
10 | 20 | 30 | 40 | 50 |
How to Scan and Print an Array | Example Code
#include
void main()
{
int a[5],ele,i;
ele=sizeof(a)/sizeof(a[0]);
printf("enter the lelements of an array:\n");
for(i=0;i<ele;i++)//for scanning
scanf("%d",&a[i]);
printf("array elelments are:\n");
for(i=0;i<ele;i++)//for printing
printf(" %d",a[i]);
printf("\n");
}
In this program, we first declare an integer array of size 5. Then, we use a for loop to scan the array elements from the user input using the scanf function. Next, we use another for loop to print the array elements on the screen using the printf function.
When the program runs, it prompts the user to enter 5 integers. After the user enters the values, the program prints the array elements on the screen.
Here's an example of how the program output might look like:
Enter 5 integers: 10 20 30 40 50
The array elements are: 10 20 30 40 50
Note that you can adjust the size of the array and the number of elements you scan and print by changing the loop conditions in the program.
Further Reading:
what is an array
how to declare and initialization an array
Dynamic Declaration of an array
How to scan an array and how to print it on screen.
Enroll Now:
[ C-Programming From Scratch to Advanced 2023-2024] "Start Supercharging Your Productivity!"
Contact Us:
- For any inquiries, please email us at [info@electro4u.net].
- Follow us on insta [ electro4u_offical_ ] for updates and tips.
Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!