In: Computer Science
For c++ Write the code to initialize an array such that each element gets two times the value of its index (e.g. 0, 2, 4, 6, …)
SOLUTION:
C++ CODE:
#include<iostream>
using namespace std;
int main(){
int n;
cout<<"Enter the number of elements you want to
insert in array\n";
cin>>n;//taking input
int a[n];//declaring array with n elements
for(int i=0;i<n;i++){
a[i]=2*i;
}
cout<<"The elements in the array
are"<<endl;
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}cout<<endl;
}
SCREENSHOT OF THE CODE AND OUTPUT:
NOTE:
If you are satisfied with my answer please do upvote and
if you have any kind of doubts please post in the comment section.
I'll surely help you there.
Thank You:)