+ 2
For Loop Explanation Help!!! Please
public static void main(String[] args) { // TODO Auto-generated method stub int power=1000; int X=2881; int[]store=new int[4]; for(int i=0;i<4;i++) { store[i]=X/power; X=X-(X/power)*power; power/=10; } for(int i=0; i<4;i++) { System.out.println(store[3-i]); } } } I don’t understand store[i]=X/power; X=X-(X/power)*power; power/=10; And why is printing out 1882 Thank you
5 Réponses
+ 8
No X is not equal to 0
Here (2881/1000) will be solved first which gives answer as 2.
So the equation becomes X=2881-(2)*1000
=2881-2000
=881
So after 1st loop X=881
Hope this will help you😊
+ 1
Joseph Jostar what exactly you are not understanting in the code?
0
Ulisses Cruz
thank you
i am confused with X=X-(X/p)*p
X=2881
so it will be
2881-(2881/1000)*1000
so X= 0 ?
i know p=1000
and it will each time ivide 10 for 4 times it will be 1000,100,10,1
0
Anju thank you