1 # include <stdio. h> 2 # include <math. h> 3 4/* 5: Judge the number of digits of a positive integer, and output Their digits in the forward and backward order. 6 */7 8 int 9 invert (INT); 10 11 void12 Order (INT, INT); 13 14 int 15 main (void) {16 int n = 56789; 17 printf ("Original: % d \ n", n); 18 int bitcont = invert (n); 19 printf ("\ nbits: % d \ n", bitcont ); 20 order (n, bitcont); 21 return 0; 22} 23 24 // output each bit in reverse order and record the number of digits. 25 int 26 invert (int n) {27 int bitcount = 0; // number of records. 28 do 29 {30 + + bitcount; 31 printf ("% d", N % 10); // output each bit in reverse order. 32 N/= 10; 33} while (n); // The condition for termination is "N is 0 ". 34 return bitcount; 35} 36 37 // output each digit in positive order. 38 void39 Order (int n, int bitcount) {40 int TMP = 0; // storage level to be subtracted, for example, when n is "1234", the storage "1000 "; when N is "234", the storage is "100 ". 41 int h = 0; // store the highest bit. 42 while (n) {43 TMP = (INT) Pow (double) 10, -- bitcount); 44 h = N/tmp; 45 printf ("% d ", h); 46 N-= H * TMP; 47} 48}
Output:
Original: 567899 8 7 6 5 bits: 55 6 7 8 9 press any key to continue...
C-output each node in reverse/forward order.