1. 程式人生 > >數字倒序輸出

數字倒序輸出

#include <stdio.h>

int reverseDigits( int n );

int main()
{ 
   int number; /* input number */

   printf( "Enter a number between 1 and 9999: " );
   scanf( "%d", &number );

   /* find number with digits reversed */
   printf( "The number with its digits reversed is: %d\n", 
          reverseDigits( number ) );

   return 0; /* indicate successful termination */

} /* end main */

/* reverseDigits returns number obtained by 
   reversing digits of n */
int reverseDigits( int n )
{ 
   int reverse = 0; /* reversed number */
   int part = 0;    /* temp variable for individual digits */

   while ( n > 1 ) {
      part = n % 10;
      reverse = reverse * 10 + part;
      n /= 10;
   }

   return reverse; /* return reversed number */

} /* end function reverseDigits */