Extracting Digits ~ Programming is Fun
 Programming is Fun!

www.anandsingh.net
Subscribe For Free Updates!

We'll not spam mate! We promise.

Friday, September 12, 2014

Extracting Digits

Write a program to extract each digit from an int, in the reverse order. For example, if the int is 1542, the output shall be "2,4,5,1", with a comma separating the digits.
Hints: Use n % 10 to extract a digit; and n = n / 10 to discard the last digit.
int n = ....;
while (n > 0) {
   int digit = n % 10;  // Extract the last digit
   ......
   .....
   n = n / 10;          // Drop last digit and repeat the loop
}


Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →

0 comments:

Post a Comment