For example, enter 1234. Print on the screen 1 2 3 4
Code Show:
Method One:
#define _crt_secure_no_warnings 1#include<stdio.h> #include <math.h>//implementation prints a number of each bit int main () {int num = 1234 ; int count = 0;//The number of digits used to hold the number int tmp = Num;int y = 0;while (tmp) {++count;tmp/= 10;} while (num) {printf ("%d", y = Num/pow (10,count-1)), num = num-y * POW (10,count-1);--count;} System ("pause"); return 0;}
Analysis: For a given number or number of inputs, the number of bits of data is calculated from high to low once output ~ First while loop. The 2nd while loop is used to print each bit, assuming we don't define a TMP variable. The first while runs out. The given number becomes 0. The second while will not go in. So. Set a new variable to hold a copy of the data.
How does a 2nd while print? Take num = 1234 as an example.
num = 1234, printing y = 1234/(10^3) = 1. num = num-1*1000 = 234;count = 4,.
num = 234. Print y = 234/100 = 2;num = num-2*100 = 34;
num = 34. ......
num = 4, ...
num = 0, exiting the Loop ~
Method Two:
int main () {char Arr[5];int num = 1234;int i = 0;while (num) {arr[i] = num% + ' 0 '; num/= 10;i++;} while (I >= 1) {printf ("%c", Arr[i-1]); i--;} System ("pause"); return 0;}
Analysis: Storing each bit with a character array is more space-saving than using an integer array store. Take num = 1234 as an example.
First while
num= 1234,arr[0] = ' 4 '; i = 1;
num = 234, arr[1] = ' 3 '; i = 2;
num = arr[2] = ' 2 '; i = 3;
num = 4, arr[3] = ' 1 '; i = 4;
num = 0, exiting the loop
2nd while loop, arr[3] = arr[4-1]; output ~
Method Three: Recursive implementation
void Print_num (int n) {if (n > 9) print_num (N/10);p rintf ("%d", n% 10);} int main () {print_num (1234); system ("pause"); return 0;}
Note: The last two pieces of code did not introduce a header file. In each piece of code. Test data I give directly, of course, we can also input test data keyboard ~ ~
C Language Implementation output a number of each bit