Write a C++ program to Input 6 even numbers in an array and then display them one by one.
I wrote this C++ program:
#include <iostream>
using namespace std;
int main() {
int num[6];
int i, ;
for (i = 1; i <= 12; i++) {
if (i % 2 == 0) {
num[i] = i;
cout << num[i] << endl;
}
}
return 0;
But the mistake here is I have to store all the even values in consecutive memory locations like num[1], num[2], num[3] to num[5] but with this method the values will store in even arrays only like num[2], num[4], num[6], etc.
What could be the real solution? My scope is only in C++.