Home / Programs / C Program to Print Array Elements
🚀 Programming Example

C Program to Print Array Elements

👁 1,496 Views
💻 Practical Program
📘 Step Learning
C Program to Print Array Elements

📌 Information & Algorithm

The following is a step-by-step explanation of the code:

  1. The program starts by including the stdio.h library, which provides functions for input and output, such as printf and scanf.
  2. In the main function, an integer array "arr" of size 50 is defined and an integer variable "num" is also defined.
  3. The program then prompts the user to enter the number of elements they want to add to the array using the printf function and scans the input using the scanf function which stores the input in the variable num.
  4. Using a for loop, the program iterates through the array and prompts the user to enter the values of each element using the scanf function.
  5. Another for loop is used to iterate through the array and print out the values of each element using the printf function.
  6. The program then returns 0, indicating a successful execution.

💻 Program Code

/*
C Program to Print Array Elements  
 Author: atnyla Developer
 */

#include<stdio.h>
 
int main() {
   int i, arr[50], num;
 
   printf("\nEnter no of elements :");
   scanf("%d", &num);
 
   //Reading values into Array
   printf("\nEnter the values :");
   for (i = 0; i < num; i++) {
      scanf("%d", &arr[i]);
   }
 
   //Printing of all elements of array
   for (i = 0; i < num; i++) {
      printf("\narr[%d] = %d", i, arr[i]);
   }
 
   return (0);
}

                        

🖥 Program Output

Enter no of elements :5

Enter the values :1234
5461
15454
125454
584662

arr[0] = 1234
arr[1] = 5461
arr[2] = 15454
arr[3] = 125454
arr[4] = 584662 
                            
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.