Array

C++ program to print n elements in an one dim array

C++ program to print n elements in one dim array

In this article, we will discuss the concept of C++ program to print n elements in one dim array

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

Code to print integer in an array

Print n elements in one dim array using for loop – #1

In this code, we are going to learn how to print integer elements in single dimensional array using for loop in C++ language

Program 1

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

int main()
{
    int arr[25,78,90,54,67,91};

    cout << "Array elements of given array" << endl;
    for(int i=0; i<6; i++){
        cout<<arr[i]<<" ";
    }
    getch();
    return 0;
}

When the code is executed, it produces the following result

Array elements of given array
25    78      90     54      67       91

 

Print n elements in one dim array using for loop – #2

In this code, we are going to learn how to print integer elements in single dimensional array using for loop in C++ language

Program 2

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

int main()
{
    int arr[6];
    cout << "Enter elements of the array" << endl;
    for(int i=0; i<6; i++){
        cin>>arr[i];
    }

    cout << "\nArray elements of given array" << endl;
    for(int i=0; i<6; i++){
            cout<<"element of arr["<<i<<"]:";
        cout<<arr[i]<<" ";
        cout<<"\n";
    }
    getch();
    return 0;
}

When the code is executed, it produces the following result

Enter elements of the array
36
87
80
98
68
72


Array elements of given array

Element of arr[0]: 36
Element of arr[1]: 87
Element of arr[2]: 80
Element of arr[3]: 98
Element of arr[4]: 68
Element of arr[5]: 72

 

Print n elements in one dim array using while loop – #1

In this code, we are going to learn how to print integer elements in single dimensional array using while loop in C++ language

Program 1

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

int main()
{
    int arr[6]{250,780,900,540,670,910};

    cout << "Array elements of given array" << endl;
    int i=0;
    while( i<6){
        cout<<arr[i]<<" ";
         i++;
    }
    getch();
    return 0;
}

When the code is executed, it produces the following result

Array elements of given array
250    780      900     540      670       910

 

Print n elements in one dim array using while loop – #2

In this code, we are going to learn how to print integer elements in single dimensional array using while loop in C++ language

Program 2

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

int main()
{
    int arr[6];
    cout << "Enter elements of the array" << endl;
    int i=0;
    while(i<6){
        cin>>arr[i];
         i++;
    }

    cout << "\nArray elements of given array" << endl;

   i=0;
    while( i<6){
            cout<<"Display elemnt of arr["<<i<<"]:";
        cout<<arr[i]<<" ";
        cout<<"\n";
         i++;
    }
    getch();
    return 0;
}

When the code is executed, it produces the following result

Enter elements of the array
122
233
344
455
566
677




Array elements of given array

Element of arr[0]: 122
Element of arr[1]: 233
Element of arr[2]: 344
Element of arr[3]: 455
Element of arr[4]: 566
Element of arr[5]: 677

 

Print n elements in  one dim array using do-while loop – #1

In this code, we are going to learn how to print integer elements in single dimensional array using do-while loop in C++ language

Program 1

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

int main()
{
    int arr[6]{50,80,90,40,70,10};

    cout << "Array elements of given array" << endl;
    int i=0;
    do{
        cout<<arr[i]<<" ";
         i++;
    }while( i<6);
    getch();
    return 0;
}

When the code is executed, it produces the following result

Array elements of given array
50    80      90   40      70       10

 

Print n elements in one dim array using do-while loop – #2

In this code, we are going to learn how to print integer elements in single dimensional array using do- while loop in C++ language

Program 2

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

int main()
{
    int arr[6];
    cout << "Enter elements of the array" << endl;
    int i=0;
    do{
        cin>>arr[i];
         i++;
    }while(i<6);

    cout << "\nArray elements of given array" << endl;

   i=0;
    do{
            cout<<"Display elemnt of arr["<<i<<"]:";
        cout<<arr[i]<<" ";
        cout<<"\n";
         i++;
    }while( i<6);
    getch();
    return 0;
}

When the code is executed, it produces the following result

Enter elements of the array
4
7
9
3
5
6


Array elements of given array
Element of arr[0]: 4
Element of arr[1]: 7
Element of arr[2]: 9
Element of arr[3]: 3
Element of arr[4]: 5
Element of arr[5]: 6


Suggested for you

For loop in C language

while loop in c language

do-while loop in c language

Operator in C language

Variable in C language

Data type in C language

One dimensional array in C language

 

For loop in C++ language

while loop in C++ language

Do while loop in C++ language

Operator in C++ language

Variable in C++ language

Data type in C++ language

One dimensional array in C++ language

 

For loop in Java language

While loop in Java language

Do-while loop in Java language

Operator in Java language

Variable in Java language

Data type in Java language

class and main method in Java

One dimensional array in Java

 

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

C code to take and print array elements using while loop

C code to take 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

4 weeks ago

PHP Star triangle Pattern program

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

4 weeks 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.