Array

C++ program to read and print elements of an one dim array

C++ program to read and print elements of an one dimensional array

In this tutorial, we will discuss the concept of C++ program to read and print elements a one dimensional array

In this topic, we are going to learn how to read and print elements of one dimensional array in C++ programming language

using loops

We have already discuss “What is an array“, “type of arrays” and “how to access it”

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 through the indices(indexes)

Read and print elements a one d array

Here,

  • e1,e2,e3,e4 and e5 represent the 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 an array

 

Code to read and print elements in an one dim array

Read and print elements Using for loop

In this program, we are briefing input elements in an array and then print them using for loop in C++ language

Program 1

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

int main()
{
    int i,len;
    cout<<"Enter the Array length: ";
    //Ask input for array length
      cin>>len;
      //Reading the input for array length
    int num[len];
//Array declaration
      cout<<"\n";//move to next line

       cout<<"Enter the Number for array :";
       //Ask input for elements(marks)
    for(i=0; i<len; i++){//input using for loop
      cin>>num[i];
      //Reading the array elements from user
    }
    cout<<"\ndisplay the number\n";
      for(i=0; i<len; i++){//display elements using for loop
      cout<<num[i];
      cout<<("\n");
    }
getch();
    return 0;
}


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

Output

Program 2

In this program, we are briefing input elements in an array and then print them using for loop in C++ language

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

int main()
{
    int i,len;
    cout<<"Enter the Array length: ";
    //Ask input for array length
      cin>>len;
      //Reading the input for array length
    int num[len];
//Array declaration
      cout<<"\n";//move to next line


       //Ask input for elements(marks)
    for(i=0; i<len; i++){//input using for loop
            cout<<"Enter the number "<<"num["<<i<<"] :";
      cin>>num[i];
      //Reading the array elements from user
    }
    cout<<"\ndisplay the number\n";
      for(i=0; i<len; i++){//display elements using for loop
      cout<<"marks["<<i<<"] :"<<num[i];
      cout<<("\n");
    }
getch();
    return 0;
}



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

Output

 

Read and print elements Using while loop

In this program, we are briefing input elements in an array and then print them using while loop in C++ language

Program 1

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

int main()
{
    int i,len;
    cout<<"Enter the Array length: ";
    //Ask input for array length
      cin>>len;
      //Reading the input for array length
    int num[len];
//Array declaration
      cout<<"\n";//move to next line

       cout<<"Enter the Number for array :";
       //Ask input for elements(marks)
       i=0;
    while(i<len){//input using while loop
      cin>>num[i];
      //Reading the array elements from user
      i++;
    }
    cout<<"\ndisplay the number\n";
    i=0;
      while(i<len){//display elements using while loop
      cout<<num[i];
      cout<<("\n");
      i++;
    }
getch();
    return 0;
}


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

Output

Program 2

In this program, we are briefing input elements in an array and then print them using while loop in C++ language

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

int main()
{
    int i,len;
    cout<<"Enter the Array length: ";
    //Ask input for array length
      cin>>len;
      //Reading the input for array length
    int num[len];
//Array declaration
      cout<<"\n";//move to next line

i=0;
       //Ask input for elements(number)
    while( i<len){//input using while loop
            cout<<"Enter the number "<<"num["<<i<<"] :";
      cin>>num[i];
      //Reading the array elements from user
       i++;
    }
    cout<<"\ndisplay the number\n";
    i=0;
      while(i<len){//display elements using while loop
      cout<<"marks["<<i<<"] :"<<num[i];
      cout<<("\n");
      i++;
    }
getch();
    return 0;
}




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

Output

 

Read and print elements Using do-while loop

In this program, we are briefing input elements in an array and then print them using do-while loop in C++ language

Program 1

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

int main()
{
    int i,len;
    cout<<"Enter the Array length: ";
    //Ask input for array length
      cin>>len;
      //Reading the input for array length
    int num[len];
//Array declaration
      cout<<"\n";//move to next line

       cout<<"Enter the Number for array :";
       //Ask input for elements(marks)
       i=0;
    do{//input using do-while loop
      cin>>num[i];
      //Reading the array elements from user
      i++;
    }while(i<len);
    cout<<"\ndisplay the number\n";
    i=0;
      do{//display elements using do-while loop
      cout<<num[i];
      cout<<("\n");
      i++;
    }while(i<len);
getch();
    return 0;
}



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

Output

 

Program 2

In this program, we are briefing input elements in an array and then print them using do-while loop in C++ language

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

int main()
{
    int i,len;
    cout<<"Enter the Array length: ";
    //Ask input for array length
      cin>>len;
      //Reading the input for array length
    int num[len];
//Array declaration
      cout<<"\n";//move to next line

i=0;
       //Ask input for elements(number)
    do{//input using do-while loop
            cout<<"Enter the number "<<"num["<<i<<"] :";
      cin>>num[i];
      //Reading the array elements from user
       i++;
    }while( i<len);
    cout<<"\ndisplay the number\n";
    i=0;
      do{//display elements using do-while loop
      cout<<"marks["<<i<<"] :"<<num[i];
      cout<<("\n");
      i++;
    }while(i<len);
getch();
    return 0;
}





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

Output

 

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

Operator in C++ language

Data type in C++ language

Variable in C++ language

 

 

 

 

C program to read and print elements of an one dim array
Program to read and print string of one dim array in Java
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