How To Add Elements In An Array In C++

Here in this tutorial, we will learn about how to enter or add elements in an array in C++. I am using Turbo C++ version 2.2 to execute the same.

Array is a collection of data in an organized manner, just like a queue. Creating array and entering elements into it is one of the basic C++ programs.

So, let’s get started.

Enter or Add Elements In The Array In C++

Let’s see the code of this program first.


void main()
int limit;
int arr[20];
cout<<"Enter The Total Number Of Elements: ";

for(int i=0;i<limit;i++)
cout<<"Enter Element "<<i+1<<": ";

cout<<"Elements In The Array Are "<<endl;
cout<<arr[i]<<" ";


Now let’s learn about the code lines that why they have been used.

First, there are the header files of the program, that have been included, namely iostream.h and conio.h. Conio.h header file is used to clear the screen and hold the output when program ends.

Next, is our main function, which is responsible for carrying out the required work. clrscr() function has been used to clear the screen before printing the output of the program, so that it looks good, otherwise, the output screen, is messy.

Then we have declared an integer value limit, which will be used to define the number of elements we wish to be in an array. Then we have declared an array of size 20. You can take up any size of your choice. Then we have asked the user to enter the number of elements that will be present in the array.

Next, is the for loop.

For Loop

Then, the user will enter the elements of the array one by one and then the for loop will terminate once the counter variable reaches the limit.

Then, the same array has been displayed to the user using the same methodology of for loop.

getch() function has been used to hold the output so that the user can see it.


Enter The Total Number Of Elements: 5

Enter Element 1: 1

Enter Element 2: 2

Enter Element 3: 3

Enter Element 4: 4

Enter Element 5: 5

Elements In The Array Are

1 2 3 4 5

Also read:

Leave a Reply