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

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.