In this post, we will learn about How to take an array as input in C++.
Firstly, to take an array as input, you should know about "for loop". For taking an array as input we have to first take input the size of the array and then, we should declare the array of that given size.
Example:
int n; //Here we first declared the size of the input array
cin>>n;
int Array[n]; //Here we declared an array of the input size
Now to take the elements of the array as input we have to use for loop:
Syntax of for loop: for(initialization; condition,operation)
Now to take elements as input, do as follows:
for(int i=0;i<n;i++){
cin>>Array[i];
}
In this way, we can take input an array.
Let's take a look at whole code at once:
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
};

Comments
Post a Comment