Array

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)

 

Print Character array

Here,

  • e1,e2,e3,e4 and e5 represent the string elements of the array
  • 0,1,2,3 and 4 represent the index of the array which is used to access the array elements
  • 1,2,3,4 and 5 represent the length of the array

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

Data type 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
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

6 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

6 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

6 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

9 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

1 year ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

1 year ago