+ 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

20th Mar 2019, 10:46 AM
Joseph Jostar
Joseph Jostar - avatar
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😊
21st Mar 2019, 6:05 AM
Anju
Anju - avatar
20th Mar 2019, 11:48 AM
Hubert Dudek
Hubert Dudek - avatar
+ 1
Joseph Jostar what exactly you are not understanting in the code?
20th Mar 2019, 10:57 AM
Ulisses Cruz
Ulisses Cruz - avatar
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
20th Mar 2019, 7:42 PM
Joseph Jostar
Joseph Jostar - avatar
0
Anju thank you
22nd Mar 2019, 5:17 AM
Joseph Jostar
Joseph Jostar - avatar