0

Can somebody explain this code in depth?

So I copied this code online. https://code.sololearn.com/cRi02XQcFArk/#cpp I want to understand it. But i'm extremely confused. I just started learning c++ about 3 or 4 days ago. Somebody said c++ was pretty much JavaScript(How?. They seem entirely different.) I left some comments in the code that ask some questions. One of the main ones are why do we keep dividing by 10? Why did we need the remainder of the number divided by 10? lol amverysconfused

6th Oct 2018, 8:16 PM
Daniel Cooper
Daniel Cooper - avatar
5 Answers
+ 2
I answered each question you had directly in the code, I hope this helps in addition to what Ulisses Cruz has already pointed out before: https://code.sololearn.com/cGF0Ggk1q0cP/?ref=app Oh, and maybe that somebody meant Java, not Javascript. That would make more sense.
6th Oct 2018, 10:39 PM
Shadow
Shadow - avatar
+ 2
Daniel Cooper if you want to count the number of digits of an integer n, you can do that by dividing n by 10 and incrementing a counter, until n == 0. For example: Imagine n = 1234 and counter = 0: n = n / 10; counter++; // now n = 123 and counter = 1 n = n / 10; counter++; // now n = 12 and counter = 2 n = n / 10; counter++; // n = 1 and counter = 3 n = n / 10; counter++ ; // n = 0 and counter = 4 another point: while(x) // loop while x != 0
6th Oct 2018, 10:25 PM
Ulisses Cruz
Ulisses Cruz - avatar
+ 2
Another point: if n = 1234, r = n % 10 // r = 4 that's a way to get the rightmost digit of n
6th Oct 2018, 10:29 PM
Ulisses Cruz
Ulisses Cruz - avatar
+ 2
Satnam Singh Nope. I showed this code to him, and at one point he mentioned it again. Lol Thanks my dude. This really helps
7th Oct 2018, 3:21 AM
Daniel Cooper
Daniel Cooper - avatar
+ 2
And thanks to you too Ulisses Cruz
7th Oct 2018, 3:26 AM
Daniel Cooper
Daniel Cooper - avatar