C++ code for print array using for loop

C++ code for print array using for loop

In this article, we will discuss the concept of C++ code for print array using for loop

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

Code to Display array elements

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

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

Program 1

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

int main()
{
    int marks[]={24,67,89,97,45};
cout <<"Array elements are: ";
    int i;
    for(i=0; i<5; i++){
        cout<< marks[i] << endl;
    }
getch();
    return 0;
}

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

Array elements are: 24
67
89
97
45

 

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

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

Program 2

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

int main()
{
    int marks[5];
    marks[0]=83;
    marks[1]=73;
    marks[2]=63;
    marks[3]=53;
    marks[4]=43;
cout <<"Array elements are: ";
    int i;
    for(i=0; i<5; i++){
        cout<< marks[i] << endl;
    }
getch();
    return 0;
}

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

Array elements are: 83
73
63
53
43

 

Program to print Strings of an array using for loop – #1

In this code, we are going to learn how to display array of Strings in using for 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]="Janakan";
   str[1]="Saravanan";
   str[2]="Kamalan";
   str[3]="Kumaran";
   str[4]="Akilan";
  //Single one D array initiation
    cout<<"\nStudents names are:\n";
    for(int i=0;  i<5; i++){
            cout<<" str["<<i<<"]: ";
        cout<<str[i]<<"\n";
        //Display the students name using for loop
    }
    getch();
    return 0;
}

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

Students names are:
str[0]:Janakan
str[1]:Saravanan
str[2]:Kamalan
str[3]:Kumaran
str[4]:Akilan

 

 

Program to print Strings of an array using for loop – #2

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

Program 1

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

int main()
{
    string str[20]{"Murali","Sorubi","Sumathi","Narani","Kamali"};
    //Single one D array declaration and  initiation
  /* str[0]="Janakan";
   str[1]="Saravanan";
   str[2]="Kamalan";
   str[3]="Kumaran";
   str[4]="Akilan";*/  //Single one D array
    cout<<"\nStudents names are:\n";
    for(int i=0;  i<5; i++){
            cout<<" str=["<<i<<"]: ";
        cout<<str[i]<<"\n";
        //Display the students name using for loop
    }
    getch();
    return 0;
}

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

Students names are:

str[0]:Murali
str[1]:Sorubi
str[2]:Sumathi
str[3]:Narani
str[4]:Kamali


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

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

Program 1

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

int main()
{
    int i;

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

char_array[0]='X';
char_array[1]='U';
char_array[2]='B';
char_array[3]='D';
char_array[4]='S';

    cout<<"\nDisplay the characters\n";
      for(i=0; i<5; i++){//display elements using for loop
      cout<<"char_array_Index["<<i<<"]:"<<char_array[i];
      cout<<("\n");
    }
getch();
    return 0;
}





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

Display the characters

char_array_index[0]:X
char_array_index[1]:U
char_array_index[2]:B
char_array_index[3]:D
char_array_index[4]:S

 

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

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

Program 1

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

int main()
{
    int i;

    char char_array[]{'M','A','A','L','A'};
//Array declaration - char array

/*char_array[0]='A';
char_array[1]='K';
char_array[2]='Y';
char_array[3]='D';
char_array[4]='M';*/
    cout<<"\ndisplay the characters\n";
      for(i=0; i<5; i++){//display elements using for loop
      cout<<"char_array_Index["<<i<<"]:"<<char_array[i];
      cout<<("\n");
    }
getch();
    return 0;
}





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

Display the characters

char_array_index[0]:M
char_array_index[1]:A
char_array_index[2]:A
char_array_index[3]:L
char_array_index[4]:A

 

 

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

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

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.