[.net] [C#] Cycle Through Digits

Started by
3 comments, last by Uphoreum 17 years, 4 months ago
Is there a way to cycle through the digits of, say, an int value and read each digit as if it were one number?
Advertisement
Should be something like ...
int foo = 987654321;string bar = foo.ToString();for(int i=0; i<bar.Length; ++i){   Console.WriteLine(bar.Substring(i,1));}

Regards,
ViLiO
Richard 'ViLiO' Thomasv.net | Twitter | YouTube
So you have to convert it to a string first?
Quote:Original post by Uphoreum
So you have to convert it to a string first?

You don't have to, no [smile]

You could do it like this...
int foo = 987654321;while (foo > 0){   int digit = foo % 10;   foo /= 10;   Console.WriteLine(digit);}

Regards,
ViLiO
Richard 'ViLiO' Thomasv.net | Twitter | YouTube
Ohhh... I see. Thanks!

This topic is closed to new replies.

Advertisement