Table of contents
1.
Introduction
2.
Algorithm
3.
Pseudocode
4.
C program to Print the Elements of an Array
4.1.
1. Using For Loop
4.2.
C++
4.3.
2. Using While Loop
4.4.
C++
4.5.
3. Using Recursion
4.6.
C++
5.
C++ Program to Print an Array
5.1.
1. Using cout Function
5.2.
C++
5.3.
2. For Loop
5.4.
C++
5.5.
3. Using Recursion
5.6.
C++
5.7.
4. Using Array Iterators Functions
5.8.
C++
5.9.
5. While Loop
5.10.
C++
6.
Frequently Asked Questions
6.1.
How to print an array element?
6.2.
How to print an array of structures in C?
6.3.
How to print a sentence in C using array?
6.4.
What is an array in C syntax?
7.
Conclusion
Last Updated: Jan 10, 2025
Easy

Program to Print Array in C & C++

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Welcome to our blog on printing arrays in C and C++! Arrays are fundamental data structures in programming, allowing us to store and manipulate collections of elements efficiently. Printing arrays is a crucial task in many programs, whether for debugging, displaying results, or user interaction.

Program to Print Array in C & C++

Also, see Literals in C

Algorithm

The algorithm for printing elements of an array involves iterating through each element of the array and displaying its value. This can be achieved using various looping constructs such as for loops, while loops, or recursion.

Pseudocode

  1. Initialize a loop to iterate through each element of the array.
  2. Access each element of the array.
  3. Print the value of the current array element.
  4. Repeat steps 2 and 3 until all elements of the array have been printed.

C program to Print the Elements of an Array

1. Using For Loop

In this method, a for loop is employed to iterate through each element of the array. The value of each element is printed inside the loop.

  • C++

C++

#include <stdio.h>

int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);

printf("Printing array using for loop:\n");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}

return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output:

Printing array using for loop:
1 2 3 4 5

2. Using While Loop

This approach utilizes a while loop to traverse the array. Similar to the for loop method, the value of each array element is printed within the loop.

  • C++

C++

#include <stdio.h>

int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);
int i = 0;

printf("Printing array using while loop:\n");
while (i < size) {
printf("%d ", arr[i]);
i++;
}

return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output:

Printing array using while loop:
1 2 3 4 5

3. Using Recursion

In the recursion method, a recursive function is employed to print the elements of the array. The function recursively calls itself, printing each element until the base case (end of array) is reached.

  • C++

C++

#include <stdio.h>

void printArray(int arr[], int size, int index) {
if (index == size) {
return;
}
printf("%d ", arr[index]);
printArray(arr, size, index + 1);
}

int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);

printf("Printing array using recursion:\n");
printArray(arr, size, 0);

return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output:

Printing array using recursion:
1 2 3 4 5

C++ Program to Print an Array

In C++, array printing can be achieved using similar methods as in C, with the addition of more streamlined features provided by the C++ standard library. This includes range-based for loops and the use of the std::cout stream for output.

1. Using cout Function

In this, we will learn how to print array in C++ using cout. We can directly use the cout function to print the elements of an array This is not the recommended practice to print array in C++.

Array Image
  • C++

C++

#include <iostream>

using namespace std;

int main()
{
/*declaration and initialization of the array*/
   int  array[5]={31,51,90,67,23};
      cout<<array[0]<<” ”;
      cout<<array[1]<<” ”;
      cout<<array[2]<<” ”;
      cout<<array[3]<<” ”;
     cout<<array[4]<<” ”;
return 0;
   }
You can also try this code with Online C++ Compiler
Run Code


Output

31 51 90 67 23 


This increases the lines of code. 

You can try and compile with the help of online c++ compiler. You can also get a free certification in basic C++.

2. For Loop

In this, we will learn how to print an array in C++ using for loop. To print an array in C++, we can use for loop in the following way.

Array Image

Below is the index of an array. The starting index of an array is 0.

  • C++

C++

#include <iostream>

using namespace std;

int main()
{
/*declaration and initialization of the array*/


   int  array[6]={31,51,90,67,23,34};
   int i=0;
  for (int i=0; i < 6; i++){
  cout<<array[i]<<” ”;
   }
You can also try this code with Online C++ Compiler
Run Code


Output

31 51 90 67 23 34

3. Using Recursion

In this, we will learn how to print array in C++ using recursion. Recursion is an algorithm in which a function calls itself. Recursion can be used to print an array in C++.

Array Image
  • C++

C++

#include <iostream>


using namespace std;

void  print_array(int arr[] , int length){
/* static variable stores the previous value */
static int j;
if(j == length){
j=0;
cout<<endl;
return;
}
cout<<arr[j]<<" ";
j++;
print_array(arr,length);
}


int main()
{
/* declaration and initialization of the array */
   int  arr[]={31,51,90,67,23,34};
/* get the size of the array. */
   int n = sizeof(arr)/sizeof(arr[0]);
print_array(arr, n);
   return 0;
}
You can also try this code with Online C++ Compiler
Run Code


Output

31 51 90 67 23 34

4. Using Array Iterators Functions

In this, we will learn how to print array in C++ using array iterators. Array iterators functions can be used to print Array in C++. Iterators functions begin and end help to print array in C++.

Array Image
  • C++

C++

#include <iostream>

using namespace std;

int main()
{
/* declaration and initialization of the array */
   int  array[6]={31,51,90,67,23,34};
copy(begin(array), end(array),
experimental:: make_ostream_joiner(cout,” ”));
return 0;
   }
You can also try this code with Online C++ Compiler
Run Code


Output

31 51 90 67 23 34

5. While Loop

In this, we will learn how to print array in C++ using a while loop. To print array in C++, we can use the while loop to get the elements of an array.

Array Image
  • C++

C++

#include <iostream>

using namespace std;

int main()
{
//declaration and initialization of the array
   int  arr[7]={31,51,90,67,23,34,10};
   int i=0;
//get the size of the array.
   int n = sizeof(arr)/sizeof(arr[0]);
//loop to print the array
   while(i < n){
       cout<<arr[i]<<" ";
       i++;
   }
   return 0;
}
You can also try this code with Online C++ Compiler
Run Code


Output

31 51 90 67 23 34 10


Also see, Abstract Data Types in C++

Frequently Asked Questions

How to print an array element?

To print an array element in C, access the element by its index using square brackets [], then use the printf() function to display its value.

How to print an array of structures in C?

To print an array of structures in C, iterate through each structure element in the array and print each member using printf() or puts().

How to print a sentence in C using array?

To print a sentence in C using an array, declare a character array containing the sentence, then use printf() or puts() to display the array contents as a string.

What is an array in C syntax?

An array in C syntax is declared by specifying the data type of its elements followed by square brackets [] containing the array size or leaving it empty if the size is determined by the number of initializers.

Conclusion

In this blog, we have discussed Program to Print Array in C & C++. Mastering the techniques for printing arrays in C and C++ opens doors to efficient data handling and streamlined program output. Whether utilizing loops, recursion, or leveraging the features of the respective languages, understanding these methods empowers programmers to manipulate and display array data effectively.

To learn more about arrays. Please refer to our articles-

Live masterclass