+ 1
How can I print even numbers from an array?
For this Int arr[2][3] = {{1,2,3},{4,5,6}}; for (int first = 0; first < 2; first++) { for (int second = 0; second < 3; second++) cout << x[first][second] << " "; cout << endl; }
9 Antworten
+ 17
//here are some small errors :
● in declaration & initialization of array arr , datatype given will be int [not Int]
● in cout , array name is arr [not x]
//for checking even number :
● U can make use of %2==0 (checking remainder left after dividing by 2)
//here is a rough code :
int arr[2][3] = {{1,2,3},{4,5,6}};
for (int first = 0; first < 2; first++) {
for (int second = 0; second < 3; second++)
if(arr[first][second]%2==0)
cout << arr[first][second] << " ";
// cout << endl;
}
+ 14
Hy again ☺
● your code given in question is correct for that , just remove the small mistakes identified & writted in my previous comment 👍
//here is rough code :
int arr[2][3] = {{1,2,3},{4,5,6}};
for (int first = 0; first < 2; first++) {
for (int second = 0; second < 3; second++)
cout << arr[first][second] << " ";
cout << endl;
}
+ 1
Thanks sir it's working
+ 1
So far as I'm seeing this it might be much better to use a 1 dimensional array rather than a 2D one, but if you do need to use that setup then within the inside for loop include an if statement to check whether the modulo of that number with 2 is equal to 0, if that condition is true then output that number, so in practice " if(x[first][second] % 2 == 0) cout << x[first][second] << "\n"; "
I would strongly suggest however that you use a 1D array as it is much easier to iterate through and manage.
+ 1
I want to display number like that
1 2 3
4 5 6
How can I do?
+ 1
Hi Saad Mughal
To print number like this:
1 2 3
4 5 6
you have to put a new line in the outer for loop:
int arr[2][3] = {{1,2,3},{4,5,6}};
for (int first = 0; first < 2; first++)
{
for (int second = 0; second < 3; second++)
{
if(arr[first][second]%2 == 0)
cout << arr[first][second] << " ";
}
cout << endl;
}
+ 1
Saad Mughal You’re welcome! happy coding ☺️
0
I have an assignment to create 2D array
0
Sekiro thanks for helping it's working
https://code.sololearn.com/cn7yZusQJd3q/?ref=app