C Language Implementation output a number of each bit

Source: Internet
Author: User
Tags pow

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.