Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Jul 9, 2024
Easy

Program to Print Array in C & C++

Table of contents
Learn to use AI Tools & ChatGPT to excel as Microsoft SDE
11 Jul, 2024 @ 01:30 PM
Speaker
Pranav Malik
SDE 2 @

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.Fibonacci Series 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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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;
}

 

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;
}

 

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;
}

 

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;
   }


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 do a free certification of Basics of C++.

2. For Loop

In this, we will learn how to print 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]<<” ”;
   }


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;
}


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;
   }


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;
}


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-

Refer to our guided paths to learn more about DSA, Competitive Programming, Javascript, System Design, etc. Enrol in our courses, refer to the mock test and problems; look at the interview experiences and interview bundle for placement preparations.

Happy Coding!