+ 1
How the output is 00301?
Java arrays https://code.sololearn.com/ceijNeDy5IoG/?ref=app
12 Answers
+ 3
alagammai uma ,
Because you used
a[n] = 0
Instead of
n = 0
In range-based for loops
(int n : a)
every iteration n gets assigned to the corresponding value in the array a.
So there is no need for [] here, as n will be equal to the next value in the array every iteration...
+ 3
alagammai uma
for(int n : a)
This is called enhanced for loop and here n will be each value of an array a
But when you do a[n] then here n will work as index
For the array {1, 2, 3, 4, 1}
n = 1
a[n] = 0 means a[1] = 0
Now array will be {1, 0, 3, 4, 1}
Now on next iteration n = 0 so a[0] = 0
Now array will be {0, 0, 3, 4, 1}
Again on next iteration n = 3 so a[3] = 0
Now array will be {0, 0, 3, 0, 1}
+ 2
Ãnjaan , mate read the question again.
+ 2
Ãnjaan
Don't give answer without reading question.
+ 1
No problem mate...
+ 1
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ thank you
+ 1
Ãnjaan , your program neither solves the problem in his code or answers his question.
0
There are a few ways to make certain elements of array equal 0 to output 00301..
I included 2 of them in that code:
https://code.sololearn.com/cL8fjkA2fNev/?ref=app
0
Aleksei Radchenkov y in my code not all elements r replaced as 0?
0
Aleksei Radchenkov ok thank you
0
Right answer is here.
Check it!
public class Program
{
public static void main(String[] args)
{
int[] a={1,2,3,4,1};
for(int n:a)
{
System.out.println(n);
}
}
}
0
Thik hai bahi ye
It's right