Codeforcoding

C++ example to print elements of an array

C++ example to print elements of an array

In this tutorial, we will discuss the concept of C++ example to print elements of an array

In this topic, we are going to learn how to print integer array elements in C++ programming language  using for, while and do-while loops.

In this blog, We have already discuss that “What is an array“, type of arrays and how to access it(one dim, two dim and three dim arrays)

Arrays are the special type of variables to store Multiple type of values(int, string,char, etc)under the same name in the continuous memory location. we can be easily accessed using the indices(indexes)

 

C++ example to print elements of an array
Print Character array

Here,

Display integer array elements in C++

Program to Display  integers of an array in C++ using for loop – #1

In this program, we are briefing print  array of integers using for loop in C++ language

Program 1

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int arr[6]={21,32,45,57,65,78};
    cout << "Array elements of given array" << endl;
    for(int i=0; i<6; i++){
        cout<<arr[i]<<" ";
    }
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Array elements of given array
21  32  45  57  65  78

 

Program to Display  integers of an array in C++ using for loop – #2

In this program, we are briefing print  array of integers using for loop in C++ language

Program 2

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int arr[6];
      arr[0]=43;
    arr[1]=51;
    arr[2]=57;
    arr[3]=75;
    arr[4]=89;
    arr[5]=98;

    cout << "Array elements of given array" << endl;
    for(int i=0; i<6; i++){
        cout<<arr[i]<<" ";
    }
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Array elements of given array
43  51  57  76  89  98

 

Program to Display  integers of an array in C++ using while loop – #1

In this program, we are briefing print  array of integers using while loop in C++ language

Program 1

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int arr[6]={210,302,458,577,635,678};
    cout << "Array elements of given array" << endl;
    int i=0;
    while(i<6){
        cout<<arr[i]<<" ";
        i++;
    }
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Array elements of given array
210 302 458 577 635 678

 

 

Program to Display  integers of an array in C++ using while loop – #2

In this program, we are briefing print  array of integers using while loop in C++ language

Program 1

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int arr[6];
    arr[0]=100;
     arr[1]=90;
      arr[2]=80;
       arr[3]=70;
        arr[4]=60;
         arr[5]=50;
    cout << "Array elements of given array" << endl;
    int i=0;
    while(i<6){
        cout<<arr[i]<<" ";
        i++;
    }
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Array elements of given array
100 90  80  70  60  50

 

Program to Display  integers of an array in C++ using do-while loop – #1

In this program, we are briefing print  array of integers using do-while loop loop in C++ language

Program 1

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int arr[6];
    arr[0]=1001;
     arr[1]=902;
      arr[2]=803;
       arr[3]=704;
        arr[4]=605;
         arr[5]=506;
    cout << "Array elements of given array" << endl;
    int i=0;
   do{
        cout<<arr[i]<<" ";
        i++;
    } while(i<6);
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Array elements of given array
1001 902  803 704 605 506

 

 

Program to Display  integers of an array in C++ using do-while  loop – #2

In this program, we are briefing print  array of integers using do-while loop in C++ language

Program 2

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int arr[6]={111,222,333,444,555,666};
    cout << "Array elements of given array" << endl;
    int i=0;
   do{
        cout<<arr[i]<<" ";
        i++;
    } while(i<6);
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Array elements of given array
111  222  333 444 555 666

 

Similar post

Code to read and print integers of an array in Java

Code to read and print strings of an array in Java

Code to read and print characters of an array in Java

Code to read and print integers of an array in C++

Code to read and print strings of an array in C++

Code to read and print characters of an array in C++

Code to read and print integers of an array in C

Code to read and print strings of an array in C

 

 

Suggested for you

Array in Programming language

One dimensional array in C language

One dimensional array in C++ language

Two dimension array in Java language

Two dimension array in C language

Two dimension array in C++ language

Three dimension array in Java language

Three dimension array in C language

Three dimension array in C++ language

 

for loop in C++ language

while loop in C++ language

Do-while loop in C++ language

Variable in C++ language

Operator in C++ language

 

 

 

Program to create and print an array of string in C
C++ exercise to print Strings of an array
Exit mobile version