2 like 3 dislike
19.5k views

Write a program to convert a decimal number (<256) to binary. Use +-*/% operations only. Loops are not permitted

寫一個把十進位轉換成二進位數的程式。請使用 +-*/% 運算子,不允許使用迴圈。

Example input:

50

Example output:

110010

 

[Exercise] Coding (C) - asked in Chapter 5: Selection Statements by (12.1k points)
ID: 27668 - Available when: Unlimited - Due to: Unlimited

edited by | 19.5k views

119 Answers

0 like 0 dislike
Hidden content!
** * * * * *******


*

{
* ** * int * * ***


**** ** ** * ** *** *** *** **** *


* *** * * * b = a % 2;
* ** * * c = a / 2 % 2;
** * * *** d = a / 2 / 2 % 2;
* * * ** ** e = a / 2 / 2 / 2 % 2;

**** * *** *** f = a / 2 / 2 / 2 / 2 % 2;
* * * * * g = a / 2 / 2 / 2 / 2 / 2 % 2;
* * * * h = a / 2 / 2 / 2 / 2 / 2 / 2 % 2;
* ** *** i = a / 2 / 2 / 2 / 2 / 2 / 2 / 2 % 2;
* ** * * j = a / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 % 2;


* ***** * * *** * *
*** ** * * {
**** ** * * *** * ** * * * ** *** *** ** ** * ** * ** *
**** * ** * }
** ** **** ** * * ***** * *
* ** **** {
** ***** * * ** * *** ** * ** *** * ** ** * **** * **
** ** * * }
*** * * * * *** ** * * ** *
*** * * ** {
* * * ** **** ****** * * * ** * ** *** **
*** * ** * }
* * ** * * * *** * * * * *
** ** ** * {
* ** * * * * * ** ** **** ** * * **** * * **
* * * *** }
* * * ** **** else **** ** * * * * **
* * * {
**** * ** * * ** * * * * ** * ** * *
* * * * }
** * ** **** *** * ** * * *
* * * * {
*** * * * ** * * ** * ****** * ** * *
** ***** ** }
* *** * ** ** * *** * * * ***
***** * *** ** {
* ** ** * ***** * * *** **** **
*** **** ** ** }
** * * ** * * **
* * * {
*** ** ** ** * ** ** * *** * * * ** b);
* *** * ** }

}
answered by (-285 points)
0 0
prog.c: In function 'main':
prog.c:23:17: error: stray '\343' in program
  else if(i==0 &&\xe3\x80\x80h!=0)
                 ^
prog.c:23:18: error: stray '\200' in program
  else if(i==0 &&\xe3\x80\x80h!=0)
                  ^
prog.c:23:19: error: stray '\200' in program
  else if(i==0 &&\xe3\x80\x80h!=0)
                   ^
prog.c:43:17: error: stray '\343' in program
  else if(d==0 &&\xe3\x80\x80c!=0)
                 ^
prog.c:43:18: error: stray '\200' in program
  else if(d==0 &&\xe3\x80\x80c!=0)
                  ^
prog.c:43:19: error: stray '\200' in program
  else if(d==0 &&\xe3\x80\x80c!=0)
                   ^
0 like 0 dislike
Hidden content!
* ** ***** ** * **


* * *

{
*** *** *** * int * **** * *


* * ** * * *** * **** * * ** *


* * * * b = a % 2;
** ** c = a / 2 % 2;
* * ***** d = a / 2 / 2 % 2;
* * ** e = a / 2 / 2 / 2 % 2;

* * **** f = a / 2 / 2 / 2 / 2 % 2;
* * * * ***** g = a / 2 / 2 / 2 / 2 / 2 % 2;
** * * * *** h = a / 2 / 2 / 2 / 2 / 2 / 2 % 2;
* * * * * i = a / 2 / 2 / 2 / 2 / 2 / 2 / 2 % 2;
**** ** * * ****** j = a / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 % 2;


** * * ** ** * * ****** *
* **** {
** *** ** ** ** ****** * * **** ** * ****** ** * *** **
** ** ** ** }
* *** * ** ** * * * * * **
** * * *** {
** ** * * ** * ** * * * ***** *** ** * ** ** * ** *** ** *
*** ** *** * * }
**** ** ** **** ** **** * *
***** *** * {
* * ** ***** * ** * * * * * ** * * ** * ** * *
* ** * *** }
* *** * *** *** ** * * *** ***
* {
* ** * * ** ** * * * ** * * * ******* * * *
**** * *** * }
* * ** * ** else * *** ***** ** ***
* * * * * ** *** {
* ** * *** *** ** ** * * * ** ***** ********* ** ** *** ***
** ** * ***** }
*** * * * ** * * ** ** * ** *** ** *
* * * {
** * **** *** * * * ** *** * **** **** ** *
* * * ** }
* * * * ** ** *** * *** **
** ** ** * {
***** * * *** * * * * *** * ***
* *** * *** * }
* *** **
* *** {
* * ** * * * * **** ** **** * * b);
* * * * ** **** }

}
answered by (-285 points)
0 0
prog.c: In function 'main':
prog.c:23:17: error: stray '\343' in program
  else if(i==0 &&\xe3\x80\x80h!=0)
                 ^
prog.c:23:18: error: stray '\200' in program
  else if(i==0 &&\xe3\x80\x80h!=0)
                  ^
prog.c:23:19: error: stray '\200' in program
  else if(i==0 &&\xe3\x80\x80h!=0)
                   ^
prog.c:43:17: error: stray '\343' in program
  else if(d==0 &&\xe3\x80\x80c!=0)
                 ^
prog.c:43:18: error: stray '\200' in program
  else if(d==0 &&\xe3\x80\x80c!=0)
                  ^
prog.c:43:19: error: stray '\200' in program
  else if(d==0 &&\xe3\x80\x80c!=0)
                   ^
0 like 0 dislike
Hidden content!
** ***** ** ** *


** *

* * * * * * ****


*** * * * ** ** * * ** * * * *


* ** ** ** b = a % *
* ** ** ** c = a / 2 %
* ** **** * d = a / 2 / 2 % *
*** * ** e = a / 2 / 2 / 2 %

* * ** ** f = a / 2 / 2 / 2 / 2 %
** * ** **** g = a / 2 / 2 / 2 / 2 / 2 %
* h = a / 2 / 2 / 2 / 2 / 2 / 2 % *
* * *** i = a / 2 / 2 / 2 / 2 / 2 / 2 / 2 % *
** * * ** * j = a / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 % *


*** *** * ** *** * * * * * ** * *
* ** * * ****** {
** * ** * *** *** * * * **** * * **** *** * * ** ** **
* ** ** }
* * * * 0 ** * * h * *
* *** {
***** * ** * ** ** * * * * * ** * * ***** ****** **
**** * *** }
** ** *** * * * * 0 ** *** **** g ** *
*** * * **** * {
* ** ** ** * * ** ** ** ** ****** * * * * **
*** * * ** }
** * ** * * * * 0 *** ** * f **
* ***** *** * {
* * * ** * * * * * ** * ** *** * ** ** ** *** **
*** *** }
** ** * * 0 ** ***** * e * *
* *** *** {
* *** * * * * * ** ** * * ***** * * * ** * ** * **
** * * ****** }
** * * ** * * ** 0 *** *** ** d **
* * * ***** {
* * ** ** ******* * ** * * ** * * ****** * * *
*** ** * * * }
*** ** ***** ** * * * 0 **** * * * *
** * * * {
** * ** ** *** * * * * ** *** * * ** *
* ** ** }
* * * **
** *** {
* ** * ** * * ** **** * ** *** ** *
* ** ** * }
answered by (-285 points)
0 0
prog.c: In function 'main':
prog.c:43:19: error: stray '\343' in program
  else if(d == 0 &&\xe3\x80\x80c != 0)
                   ^
prog.c:43:20: error: stray '\200' in program
  else if(d == 0 &&\xe3\x80\x80c != 0)
                    ^
prog.c:43:21: error: stray '\200' in program
  else if(d == 0 &&\xe3\x80\x80c != 0)
                     ^
0 like 0 dislike
Hidden content!
#include<stdio.h>



int main(void){


*** * *** ** * **** * a,b,c,d,e,f,g,h,i;


** * * ****** * ** * * **** * * ** * * * *
* * *** *** * ** *
* * * * ** ** ** * * *** **
* * *** ** * * ** *** * * *
* * * * * * ****** ** * * *
**** * * ** * * * * * ***
* * ** * ******* * * * * * **
** ***** ** * * **** ** * * ***
**** ** *** * ** ****** ** ** * *** * *






* ** * * * ** * * * ** ** * ** ** * * **** *















return 0;



}
answered by (-301 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 like 0 dislike
Hidden content!
** ** *** ** ****

int main()

{

  int *** * ** * * **
* ** * *** ** ** **** **** * * *
* ** ** ** * *
*** * ** * * *
** ***** *** **** **
** * * * **
* ****** *** * **
* * *******
* *** * * *** **
* *** ** * *
*** * ** * ** * ****** * *** * * *** * ** * **** ** ** ** ** ** * ****** * * ** * ***

  {
* * ** * *** * * * * * *** * * * **** * ** * **

  }
*** *** * * * * ** * ** ** * ** *** ** * * **** * * * ** * *** * ** ***

  {
* ********** * ** * * ** * ** * ** ** * * *

  }
** ** * * ** ** * ** * * * **** **** * * * ** * *** * **

  {
*** * **** * * * ** ****** ****** ** *** * * ******** * *

  }

  else ** * * ***** ****** ** * * ** ** *****

  {
* * * ** ** *** * ***** ** *** ** ***** **** * * ******* *** * ***

  }

  else * *** ** ** * * ** * *** ** *

  {
* *** * ** * *** * ** * ** *** *** ** * ** * * * *

  }

  else * *** * * **** *

  {
** * *** **** ** * * * **** ** *** * * * * *** * ** ***** ** *

  }
*** *** if((a0==0))

  {
** ** * * * * *** ** * * * ** ** * * ****** * * * * * * ** **** *

  }
* ******
*** * * * *** * * ** * * ** * * ** * * ** ** ** * * * *** **


* * * * ** * * * 0;

}
answered by (-255 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 like 0 dislike
Hidden content!
#include <stdio.h>



int main()



{
** * ***** * ** **** * ** n,a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0;
* * ** * ** ** * * ** ** * **** * * **


***** ** ** ** ***** * * *** (n!=0)
* ** ** * ** * * * *** * ** * * * * *** ***
* *** ** *** ***** *** * * * ******** *** *** *
** * ** * **** *** (n!=0)
* * * * * * * **** * * ** ** **
* * * * * * * * ** * *** * ** ***** ** **** **

    if (n!=0)
* ** ** ** * * * ** *** ** ** *** * ****
* ** * *** * * * * *** * ** * * ** ***** *
* *** * * **** *** (n!=0)
* * ** ** *** ** * * * ********* * ** * *
** ** ** ** * ** * * *** * * **** * *
* ** ** ** * * *** (n!=0)
***** ** *** * * * ** * **** ** *** **** * **
* ** *** ** * ****** ***** * * * * ** ** ** * **** **** *
** ** *** *** * *** (n!=0)
* *** * * **** *** * * **** * ** ** ** ** *
* ** ** ** * * * *** **** ** ******* *** *
** ** * * * ** ** *** (n!=0)
* * * *** * * * ** * * ** ** * * ***** * * *
**** **** ** * ** *** **** * * ** * ** **
** * ** * * ***** * * (n!=0)
*** ** * * * ** * ***** * * ***** * * * **** ** *
*** * ** ** **** * * (h)
** * * * * * * * *** * *** ** *** ******* ********** * * *
** *** *** * * * * **** (g)
* *** ** * * *** * *** * *** **** ** * * (h==0)
***** ***** *** * *** * * **** ** * * * *** * **
*** * ** * *** * * * *** (f)
** ** * * ** ** * **** * * * * * * * * (h==0 && g==0)
* *** ** *** * * * ** *** ****** ** * ** *
*** ** * * * *** (e)
* * * * *** ** * * * *** * * *** * ***** (h==0 && g==0 && f==0)
** ** * ** ***** * * *** * * ** ** * *
* * *** * ** ** *** *** (d)
* *** * * * * ***** * ****** * * *** * **** * * ***** (h==0 && g==0 && f==0 && e==0)
* *** * * ** *** ** * * * *** ** * * *** ** *
* * ** ** **** * * ** (c)
* ** ** * ****** ** ** * * * * *** ** * ** * (h==0 && g==0 && f==0 && e==0 && d==0)
** * * *** ** * * * * * ** * **
** ** ** ***** *** ** (b)
* * * * *** ** * * **** ** * *** * ** * (h==0 && g==0 && f==0 && e==0 && d==0 && c==0)
** * ** * *** * ** ** ** * ** *****
** *** ** * ** ** ** (a)
*** * * ******** ****** * ** * * * ** * * (h==0 && g==0 && f==0 && e==0 && d==0 && c==0 && b==0)
** * ** **** * * * ** *** * **** *


** ******* * **** * * ** 0;





}
answered by (-249 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 like 0 dislike
Hidden content!
#include<stdio.h>



int main(void){


** *** ** * ** ***** ** a,b,c,d,e,f,g,h,i;


* * ** ** **** * * *** ** ** *******
* ** ** * * * * **** ** ****
* * **** * ** *
* ** * * ** * ** * ** *
* ** * * ** ******* ** *** ** *
* *** ** ****** * * * *****
* * * ** * *** *** * * ***
* *** **** * ** * ** * *
**** * ** * * ** * * * *






* * * * * * ** ** **** ** *** ** * **** **** * *















return 0;



}
answered by (-301 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 like 0 dislike
Hidden content!
#include<stdio.h>



int main(void){


** *** * ** * *** **** ** a,b,c,d,e,f,g,h,i,j;


** ** * * * **** * * * ** **** *** * * ** * *
* ** * ** ** * * ***
***** * ***** ** * ** *** **
* *** * * **** * ***
* ********* ** * ** * *
*** * * * * * ***** *
** * ***** * ** * * **** * * * *
* ** *** * ** ******** *
* ** * ** ** ** * * * *** * **






* * ** * ** * * **** * * * * *** ** * ** * ***** ** **** *



































return 0;



}
answered by (-301 points)
0 0
prog.c: In function 'main':
prog.c:19:12: error: too many arguments for format [-Werror=format-extra-args]
     printf("%d%d%d%d%d%d%d",i,h,g,f,e,d,c,b);
            ^~~~~~~~~~~~~~~~
prog.c:5:27: error: unused variable 'j' [-Werror=unused-variable]
     int a,b,c,d,e,f,g,h,i,j;
                           ^
cc1: all warnings being treated as errors
0 like 0 dislike
Hidden content!
#include<stdio.h>



int main(void){


** ** ** ** * * * * *** *** a,b,c,d,e,f,g,h,i,j;


* ** ** ** ** ** ** * * * **** * *** *
* ** *** *** * *** *** **
* **** * * * ** * * *
** * ***** *** * * * * * ** *
* * * ** ** ** * ***** ****
* ** * * ** * * * * * ***
***** * * *** * * * **
** *** ******* * ** **** *
** ****** *** ** * ** * ** * *






* * **** * ** * * ****** ** * * * * * ** ** *



































return 0;



}
answered by (-301 points)
0 0
prog.c: In function 'main':
prog.c:19:12: error: too many arguments for format [-Werror=format-extra-args]
     printf("%d%d%d%d%d%d%d",i,h,g,f,e,d,c,b);
            ^~~~~~~~~~~~~~~~
prog.c:5:27: error: unused variable 'j' [-Werror=unused-variable]
     int a,b,c,d,e,f,g,h,i,j;
                           ^
cc1: all warnings being treated as errors
0 like 0 dislike
Hidden content!
#include<stdio.h>



int main(void){


** * * * ***** * *** a,b,c,d,e,f,g,h,i,j;


** ** * * * * ****** * **** ** ***** * ** ** ** ** **
*** *** *** *** * * **
* * * **** * * * ** *** * **
** ******* * ** * ****** * ***
* ** * ** * * * ** ** *** ****
******* * ** ** * * ****** ** **
* * * * * * ** * * * ** *
** ** *** * * * * * * ** ** ***
**** * ** *** * ** **






** ** *** * **** * ***** ** ** * * * ** * * * **** * *



































return 0;



}
answered by (-301 points)
0 0
prog.c: In function 'main':
prog.c:19:12: error: too many arguments for format [-Werror=format-extra-args]
     printf("%d%d%d%d%d%d%d",i,h,g,f,e,d,c,b);
            ^~~~~~~~~~~~~~~~
prog.c:5:27: error: unused variable 'j' [-Werror=unused-variable]
     int a,b,c,d,e,f,g,h,i,j;
                           ^
cc1: all warnings being treated as errors
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:172.69.130.71
©2016-2025

Related questions

2 like 0 dislike
77 answers
[Exercise] Coding (C) - asked Oct 19, 2017 in Chapter 5: Selection Statements by thopd (12.1k points)
ID: 27667 - Available when: Unlimited - Due to: Unlimited
| 13.5k views
12,783 questions
183,442 answers
172,219 comments
4,824 users