+ 1

Can anyone help me to understand this code?

#include <stdio.h> int main () {int x= 123; int sum= 0; while(x>0) { sum+= x%10; x = (x - x%10)/10; } printf("%d",sum); } It's output is 6.

18th Sep 2020, 12:43 PM
Nilesh Baraiya
Nilesh Baraiya - avatar
3 Answers
+ 4
This code will seprate the Entered number one by one adn sum +=x/10 will add all sepratated numbers write one copy how values changing you will understood. If you divide any Number by 10 then you will get last number as a Remainder
18th Sep 2020, 1:01 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 2
Put value and calculate how Values varying
18th Sep 2020, 2:23 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
0
đŸ—ĄïžJasprit What x = (x-x%10)/10 will do?
18th Sep 2020, 2:02 PM
Nilesh Baraiya
Nilesh Baraiya - avatar