Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
C++ code to print array using for loop

C++ Code to print array using while loop

Posted on October 31, 2021

Table of Contents

  • C++ Code to print array using while loop
    • Code to Display array elements
      • Program to print integers of an array using while loop – #1
      • Program to print integers of an array using while loop – #2
      • Program to print strings of an array using while loop – #1
      • Program to print strings of an array using while loop – #2
      • Program to print characters of an array using while loop – #1
      • Program to print characters of an array using while loop – #2

C++ Code to print array using while loop

In this article, we will discuss the concept of C++ Code to print array using while loop

In this post, we are going to learn how to write a program to  print in an array using while loop in C++ language

Code to Display array elements

Program to print integers of an array using while loop – #1

In this code, we are going to learn how to display array of  integers using while loop in C++ language

Program 1

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

int main()
{
    int arr[6]={202,402,602,802,902,1102};
    //Array declaration and initialization
    cout << "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

Elements of given array
202, 402, 602, 802, 902,1102

 

Program to print integers of an array using while loop – #2

In this code, we are going to learn how to display array of  integers using while loop in C++ language

Program 2

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

int main()
{
    int marks[5];
    marks[0]=31;
    marks[1]=13;
    marks[2]=63;
    marks[3]=71;
    marks[4]=78;
cout <<"Array elements are: ";
    int i=0;
    while( i<5){
        cout<< marks[i] << endl;
        i++;
    }
getch();
    return 0;
}

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

Array elements are: 31
13
63
71
78

 

 

Program to print strings of an array using while loop – #1

In this code, we are going to learn how to display array of  strings using while loop in C++ language

Program 1

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

int main()
{
    string str[20];
    //Single one D array declaration
   str[0]="Donald";
   str[1]="Obama";
   str[2]="Rigan";
   str[3]="Lingan";
   str[4]="Michel";
//String array initialization
    cout<<"\nGiven students names are:\n";
    int i=0;
    while( i<5){
            cout<<"Index"<<" str=["<<i<<"]: ";
        cout<<str[i]<<"\n";
        //Display the students name using while loop
        i++;
    }
    getch();
    return 0;
}


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

Given students names are:

index str[0]: Donald;
index str[1]: Obama;
index str[2]: Rigan;
index str[3]: Lingan;
index str[4]: Michel;

 

Program to print strings of an array using while loop – #2

In this code, we are going to learn how to display array of  strings using while loop in C++ language

Program 2

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

int main()
{
    string str[20]{"Muththu","Appu","Kumaru","Alaku",};
    //Single one D array declaration
    //and initialization

  /* str[0]="Donald";
   str[1]="Obama";
   str[2]="Rigan";
   str[3]="Lingan";
   str[4]="Michel";
//String array initialization*/
    cout<<"\nGiven students names are:\n";
    int i=0;
    while( i<4){
            cout<<"Index"<<" str=["<<i<<"]: ";
        cout<<str[i]<<"\n";
        //Display the students name using while loop
        i++;
    }
    getch();
    return 0;

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

Given students names are:

index str[0]: Muthu;
index str[1]: "Appu";
index str[2]: "Kumaru";
index str[3]: "Alaku";

 

Program to print characters of an array using while loop – #1

In this code, we are going to learn how to display array of  characters using while loop in C++ language

Program 1

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

int main()
{
    int i;

    char char_array[4];
//Array declaration - char array

char_array[0]='c';
char_array[1]='h';
char_array[2]='a';
char_array[3]='r';
//Array initialization - char array

    cout<<"\ndisplay the characters\n";
    i=0;
      while(i<4){//display elements using while loop
      cout<<"char_array["<<i<<"]:"<<char_array[i];
      cout<<("\n");//Move to next line
       i++;
    }
getch();
    return 0;
}

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

Display the characters

char_array[0]=c;
char_array[1]=h;
char_array[2]=a;
char_array[3]=r;


Program to print characters of an array using while loop – #2

In this code, we are going to learn how to display array of  characters using while loop in C++ language

Program 2

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

int main()
{
    int i;

    char char_array[4]{'K','I','N','D'};
//Array declaration - char array

/*
char_array[0]='k';
char_array[1]='i';
char_array[2]='n';
char_array[3]='d';
//Array initialization - char array*/

    cout<<"\ndisplay the characters\n";
    i=0;
      while(i<4){//display elements using while loop
      cout<<"char_array["<<i<<"]:"<<char_array[i];
      cout<<("\n");//Move to next line
       i++;
    }
getch();
    return 0;
}

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

Display the characters

char_array[0]=K;
char_array[1]=I;
char_array[2]=N;
char_array[3]=D


Suggested for you

For loop in C++ language

while loop in C++ language

do-while loop in C++ language

Operator in C++ language

One dimensional array in C++ language

Hollow world program in C++ language

 

Similar post

Java program to read and print array elements using for loop

Java program to read and print array elements using while loop

Java program to read and print array elements using Do-while loop

C code to read and print array elements using for loop

C code to read and print array elements using while loop

C code to read and print array elements using do-while loop

C++ program to read and print array elements using for loop

C++ program to read and print array elements using while loop

C++ program to read and print array elements using do-while loop

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes