Write a program using C language to output each bit of a number (multiple methods)

Source: Internet
Author: User

Write a program using C language to output each bit of a number (multiple methods)
First:

# Include <stdio. h> int main () {int zheng, I; int m = 0; int yu [10]; int count = 0; scanf ("% d", & m ); zheng = m; for (I = 0; I <10; I ++) if (zheng! = 0) {yu [I] = zheng % 10; zheng = zheng/10; count ++ ;}for (I = 0; I <count; I ++) {printf ("% d", yu [I]);} return 0 ;}

 

Second:
# Include <stdio. h> int main () {int num = 0; int I = 0; int arr [10]; int left = 0; int right = 0; int j = 0; scanf ("% d", & num); while (num) {arr [I] = num % 10; num = num/10; I ++ ;} // reverse order right = I-1 for the elements in arr [4]; while (left <right) {int tmp = arr [left]; arr [left] = arr [right]; arr [right] = tmp; left ++; right --;} // output for (j = 0; j <I; j ++) {printf ("% d", arr [j]);} printf ("\ n"); return 0 ;}

 

Third: recursive
# Include <stdio. h> void print (int num) // recursion {if (num> = 10) print (num/10); printf ("% d", num % 10 );} int main () {int num = 10; scanf ("% d", & num); print (num); return 0 ;}

 


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.