answersLogoWhite

0

Repeatedly divide the number by 10 and store the remainder (the modulo).

By way of an example, if the number were 12345:

12345 % 10 = 5 (first digit)

12345 / 10 = 1234

1234 % 10 = 4 (second digit)

1234 / 10 = 123

123 % 10 = 3 (third digit)

123 / 10 = 12

12 % 10 = 2 (fourth digit)

12 / 10 = 1 (fifth digit)

This algorithm forms the basis of number reversals. The following function demonstrates the most efficient way of reversing any number in the range -2,147,483,648 to 2,147,483,647, inclusive.

int RevNum( int num )

{

const int base = 10;

int result = 0;

int remain = 0;

do

{

remain = num % base;

result *= base;

result += remain;

} while( num /= base);

return( result );

}

User Avatar

Wiki User

12y ago

Still curious? Ask our experts.

Chat with our AI personalities

DevinDevin
I've poured enough drinks to know that people don't always want advice—they just want to talk.
Chat with Devin
BeauBeau
You're doing better than you think!
Chat with Beau
TaigaTaiga
Every great hero faces trials, and you—yes, YOU—are no exception!
Chat with Taiga

Add your answer:

Earn +20 pts
Q: How do you separate digits in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp