Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
int num, sum = 0, r;
 Console.WriteLine("Enter a Number : ");
 num = int.Parse(Console.ReadLine());
while (num != 0)
            {
r = num % 10;
num = num / 10;
sum = sum + r;
}

let we take num = 123
r=123%10=12.3, then how is it calculate other two line, i didn't get, can you explain me,, i am new in programmimg

What I have tried:

i tried the same code and output got correct, but i am unable to understand the how the formula works
Posted
Updated 27-Mar-17 8:41am
v2

Actually % = mod

the result of 123 % 10 is 3 as mod equals the remainder only.

num is an int. and so can't hold any decimal places
the result of int / int is always an int (rounded down as the decimal place is just lost)

so 123 / 10 = 12


so what this does is take the last digit and add it to the total until there are no digits left:


sum = 0;
num = 123;


pass1:
r = 123%10 = 3
num = 123/10 = 12
sum = 3

pass2:
r = 12%10 = 2
num = 123/10 = 1
sum = 5

pass3:
r = 1%10 = 1
num = 123/10 = 0
sum = 6

pass4:
terminated as num = 0


I hope that helps

Andy ^_^
 
Share this answer
 
v2
Comments
Member 13087536 27-Mar-17 12:18pm    
thanks Andy
Andy Lanng 27-Mar-17 12:29pm    
No problem. I remember when I first came across this.

Don't forget to accept the answers so people can find the solution for similar issues (and so we can get those lovely points ^_^)
No, the modulus operator doesn;t work like that.
let we take num = 123
r=123%10=12.3,
Is not right:
r = 123 % 10 = 3
The Modulus operator '%' returns the remainder of the division, do 123 / 10 == 12 remainder 3:
In the case of modulus 10, it returns the least significant digit, since the input number is in base ten.
 
Share this answer
 
Comments
Member 13087536 27-Mar-17 12:22pm    
thanks
OriginalGriff 27-Mar-17 12:31pm    
You're welcome!
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Comments
[no name] 27-Mar-17 14:52pm    
Yeah good luck. He told me his questions were none of my business when I told him he should learn to use the debugger.
Patrice T 27-Mar-17 15:00pm    
The OP do what he wants, I know that my advice is a good one.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900