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 for print array using for loop

Posted on October 31, 2021

Table of Contents

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

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

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