tags:

views:

91

answers:

3

Hi,

How would I check for descending digits in an integer? For instance, if the user entered 98765, I would need to verify this and say the digits are descending and vice versa. I cannot convert the integer to a string, and there is no limit on the length. Oh, the numbers have to be >= 0.

I know I need to split the integer up, but I'm not quite sure how. Please help!

+2  A: 

Not to give the whole answer, but consider this:

num = 98765;
digit = num % 10;

What will this code do? How could you use this to get each digit?

JoshD
A: 

I won't give you the code (since it's homework), but:

  1. Use the modulus operator to get n mod 10, which will give you the lowest digit.
  2. If that lowest digit is one above the previous lowest digit, continue the loop; else quit the loop and say that the number is not descending.
  3. Divide n by 10.
  4. Go to step 1.
Reinderien
+1  A: 

Look for the modulo operator. That will let you pick the rightmost value off the int. Once you have, divide the result by 10, and repeat. As you do so, compare the value you pick off to the value you picked off last time.

Tony Ennis