Advertisement
dmilicev

reverse_digits_of_number_optimized.c

Dec 16th, 2019
219
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.10 KB | None
  1. /*
  2.  
  3.     reverse_digits_of_number_optimized.c
  4.  
  5.     by Walter Whitman
  6.  
  7.     https://web.facebook.com/waltersmind
  8.  
  9.  
  10.     Here is the optimized code for integer reversal,
  11.  
  12.     optimized to the max in a short amount of time.
  13.  
  14. */
  15.  
  16. #include <stdio.h>
  17.  
  18.  
  19. // return NewValue, which is reversed digits of ValueToReverse
  20. unsigned long long int reverse_digits_of_number_optimized( unsigned long long int ValueToReverse, unsigned long long int *NewValue )
  21. {
  22.     while(ValueToReverse > 0)
  23.     {
  24.         *NewValue = *NewValue * 10 + (ValueToReverse % 10);
  25.         ValueToReverse /= 10;
  26.     }
  27.  
  28.     return( *NewValue );
  29.  
  30. } // reverse_digits_of_number_optimized()
  31.  
  32.  
  33. int main(void)
  34. {
  35.     unsigned long long int ValueToReverse = 123456789;
  36.     unsigned long long int NewValue = 0;
  37.  
  38.  
  39.     printf("\n Enter ValueToReverse = ");
  40.     scanf("%llu",&ValueToReverse);
  41.  
  42.  
  43.     printf("\n ValueToReverse: %llu \n", ValueToReverse );
  44.  
  45.     //NewValue = reverse_digits_of_number_optimized( ValueToReverse, &NewValue );
  46.     reverse_digits_of_number_optimized( ValueToReverse, &NewValue );
  47.  
  48.     printf("\n NewValue:       %llu \n", NewValue );
  49.  
  50.  
  51.     return 0;
  52.  
  53. } // main()
Advertisement
RAW Paste Data Copied
Advertisement