2 like 3 dislike
13.4k 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 | 13.4k views

119 Answers

0 like 0 dislike
Hidden content!
#include <stdio.h>
int main()
{
* * *** **** * * * *** a,b,c,d;
* ** ****** ****** * ** * *** e,f,g,h;
** *** *** * *** ** * x;
* * * ** ** * * * * * *** * ***
* **** * * * * * * * *
* ** * * * * **** * ** * * *
*** * *** * * **** *** **** *
* ** *** *** **** * * ** ****
****** * *** ** ** *** ** * *
***** * * ** *** **** * ** * *
* * * * ** ** * ** * * * ** * **** *
*** * * ** * * * ** *** * * ***

** * ***** * * **** * * (a != 0)
    {
** ** * * *** * **** ***** * * ** * * * *
    }
***** *** * * ** * ** * *** if (b != 0)
    {
* **** * ** ** * ** * * ***** ***** * * * *** * **
***** * *** * *
* * ** * **** ******** if (c != 0)
**** **** ***** * ** *
* ** **** * ** * * ** ** * * * *** ** * ** ** * *
*** **** * ** * * *
***** * ** ** * ** * * if (d != 0)
*** * * * ******** **
** * *** * ** *** ** ****** * ** *** * * * * ** * *****
*** **** *** * * *
** ** ** * ****** * * *** if (e != 0)
    {
* ** ** * * *** ** *** * ***** * * * * *
* *** *** * * * * *
* ** ** ** * ** * *** * * * * if (f != 0)
    {
** * * ** ***** ** * ** *** * * * **** * **
*** ** ** * * * * **
** ** * * * ** ***** if (g != 0)
** *** ** * * * **
* ** * * * * * *** * * ** * * * * ***** ***
    }

return  0;
}
answered by (-329 points)
0 0
prog.c: In function 'main':
prog.c:5:15: error: variable 'h' set but not used [-Werror=unused-but-set-variable]
     int e,f,g,h;
               ^
cc1: all warnings being treated as errors
0 0
prog.c: In function 'main':
prog.c:5:15: error: variable 'h' set but not used [-Werror=unused-but-set-variable]
     int e,f,g,h;
               ^
cc1: all warnings being treated as errors
0 like 0 dislike
Hidden content!
#include <stdio.h>
int main()
{
* ** **** *** *** *** * * a,b,c,d,e,f,g,h,x;
* * ******* * * * * ** **** * * ** *** * **
**** * ** * * ** * * * *
******* * ** ** * ***** * *
* ** * ** ** * * ** * *
* * * ** * * * **** *
** **** * *** * * * * * * *** ******
*** * * * * * ** **** *** * ******
*** * * ** *** * * **** * * * ** *
***** ** * * **** * ** * ******** * **

* ***** * * *** (a != 0)
* * * * * * *** * * *
* *** * *** * ********* ** ** * ** * * *** ***** ** *** *
    }
* ** * *** ** * * * * if (b != 0)
    {
* ** ** *** **** ** ** ** * *** *** * * ** * *
** **** ***** * * * **
***** * * *** * *** ** * if (c != 0)
* * *** ****** * * *
* ** * ** * * ** * * ** *** *** * * * ** * *
* * * * * ** * ** ** ** * *
* ** * * **** * * *** ** if (d != 0)
    {
*** ** ** *** * ********** * * * ****** * * * **** ** ** *** *******
* ********* * * **
*** * ** * *** *** if (e != 0)
***** *** ** ** * * * **
* * ** ** * ***** ** *** **** ** * ***** * **
    }
* *** ******* ** *** ****** if (f != 0)
    {
* *** ** * * ****** *** ** ** **** * * * ** *
    }
* * * * ** * *** **** if (g != 0)
*** ** * *** * *** *
**** *** ** ** ** ** **** * *** ** * ***
** * * * * * *** *** *

return  0;
}
answered by (-329 points)
0 0
prog.c: In function 'main':
prog.c:4:23: error: variable 'h' set but not used [-Werror=unused-but-set-variable]
     int a,b,c,d,e,f,g,h,x;
                       ^
cc1: all warnings being treated as errors
0 0
prog.c: In function 'main':
prog.c:4:23: error: variable 'h' set but not used [-Werror=unused-but-set-variable]
     int a,b,c,d,e,f,g,h,x;
                       ^
cc1: all warnings being treated as errors
0 like 0 dislike
Hidden content!
#include <stdio.h>

int main()
{
** ** * * ****** **** d,sum,sum2,sum3,sum4,sum5,sum6,sum7,sum8;
** * * ** * ** * ** * **** ** * *** ****
** **** ** * ** * * * ** = d%2;
** * * **** ** * **** = (d/2)%2;
* ** **** * * * * *** ** *** = (d/4)%2;
* * ** *** ** * * * ** ****** = (d/8)%2;
* * **** **** **** * * = (d/16)%2;
* ***** * ** * * *** *** * ** = (d/32)%2;
* * * *** * * *** ** * = (d/64)%2;
* ** * ** * ** ** ** * = (d/128)%2;
    if (sum7 == 0 && sum8 == 0)
* ** * * ** * *** * * *** * ** * *
* * * * *** * * * ***** * * * * * ***** ** ** * * * * ** * ***** *** ** * * *** ** **** * * * ** * **
** ******* * ** ** * * **** * *******
*** * * * * ** * ** if (sum7 == 1 && sum8 == 0)
**** * **** * **** *** * ***** ** * * ** ** * * **** **
*** * **** *** * *** * *** ** * **** ** * * * ** *** * * * *** * *** * ** * * * * * * * * ** ** **
* *** * *** * ** ****** * *** * * * ** **
*** * ** * ** * **
** * **** * * * ** ** * * * ** * * *** * * *** *** ***** * * ** ** * ** * * ** *** * * ****


* * ** * ***** **** 0;
}
answered by (-323 points)
0 0
prog.c: In function 'main':
prog.c:21:20: error: too many arguments for format [-Werror=format-extra-args]
             printf("%d%d%d%d%d%d",sum7,sum6,sum5,sum4,sum3,sum2,sum);
                    ^~~~~~~~~~~~~~
cc1: all warnings being treated as errors
0 0
prog.c: In function 'main':
prog.c:21:20: error: too many arguments for format [-Werror=format-extra-args]
             printf("%d%d%d%d%d%d",sum7,sum6,sum5,sum4,sum3,sum2,sum);
                    ^~~~~~~~~~~~~~
cc1: all warnings being treated as errors
0 like 0 dislike
Hidden content!
* * * * ***
int * **
** ***
***** ***** * * ***** * **
***
** **
* *
* * * *** **
** * **
* ** ** **
* *** * * ** ** *
* *** ** ** **

if (a != 0)
{
* ** ** * ** ** * *** ** ** *
}
* ** if (b != 0)
{
*** ** * * * ** * *** ****
}
** * if (c != 0)
{
* **** * * ** **** ** * * *
}
* * if (d != 0)
{
* * * * ** ***** * * *** ** ** * ****
}
* if (e != 0)
{
** *** *** * * * **** ***
}
if (f != 0)
{
* * * **** ** * *
}
** * if (g != 0)
{
**** ******* ******* *
}

**** * ***
}
answered by (-329 points)
0 0
prog.c: In function 'main':
prog.c:3:20: error: variable 'h' set but not used [-Werror=unused-but-set-variable]
 {int a,b,c,d,e,f,g,h,x;
                    ^
cc1: all warnings being treated as errors
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;

* *** * * ** * *** * * **
* *** ** *** {
* * ** * ** ** ** * * ** ** * * * ** * * *** * *** ****
* * * * * **** }
*** * * * * * ** ********
** ** * * {
* * * * * * **** ** **** * * ***** *** ** * * *
***** * * ** * }
**** * ***** * * ***** * *
** ** ** {
* * * ** * * * ** *** * * *** * * * ** ** ** **
* * * * * }
** * ** * * * *
** * * {
** * **** ** * **** * ** * * * ** ** **
* * ** **** }
* *** * * ** *** **** * **
** * * * ** * {
*** ** ** * ** * * * * ** * * ** ** ** *
******* ** ** * }
** * ** ** ** *** * * ** **
*** ** {
* ** * ** * **** *** * * **** ** * * **** ** *
* *** * }
** * * * * * * * * * *** *
** * * {
* **** * * ** * * ** ** * *** **** * **
***** * * }
**** * * ** **
* * * ** {
* * ** * * * **** ** * * * ** ***** * b);
* ** * }
}
answered by (-285 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 0
-----------Re-judge-----------
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 0
-----------Re-judge-----------
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 0
-----------Re-judge-----------
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
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 * **
* ** * ***
* *** * ** ****** ***
*
***
* *** * **
*** *
* * **** * *
* ** *
**** *** ** * ******
* *** ** * ** ****

if (h == 0)
{
* ***** * * *** ** ***** *
}
* * if (g == 0)
{
* *** * * ** * * **** ** * *
}
else if (f == 0)
{
* ****** * * * * * * * * ****
}
** if (e == 0)
** * ** ** **** * * * ** *
* * if (d == 0)
{
*** * **** ** * ** * ***
}
* if (c == 0)
{
* * *** * *
}
* * if (b == 0)
{
* * **** **** *** * *
}
*
{

* * * * **** ***** *** *** * * * * * *
}
* * * * * ** *
}
answered by (-329 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>
#include <stdlib.h>
int main()
{
** ** * ** ** * *** a,b,c,d,e,f,g,h,i,j;
* * * * * ** * ** ** * ** ** * * *
** ******** ****** **
* ** ****** * ** * ***
** * ** ** * * * * * ** ******
* * ** ** * ** ** **
** * ** ** * * * * ** *** ***
** ** ** **** **** ** * *** ** **
** * ** * ** * *** * * ******
** ** ****** * * *** * * * *
*** * ** * ** * ** * ** ** **** * ** *
** ** ** * * ** * * ** *
    {
* * ** * **** ** **** * ***** ** * * * ***** * *** ** * * **** ** * **** *
    }
* ** * ** ** * ** ** * ** * if(i==0)
    {
*** *** *** ***** ** ** ** ***** * * ** * *** *** * **** * **** * *** ** **
    }
** * ** * ** * *** * if(h==0)
** *** ** ** **
*** *** * * ** * ** ** ** **** * * ** *** ** ** * * ** **** * *
    }
* * * **** *** * * if(g==0)
    {
* *** ** * ****** * ** ** ** * * **** * * * *** * * * *
* * * * ** * ** **
* ** ** * ** ** * * if(f==0)
** *** ** * * *** * ***
***** * **** *** ** ** *** * * ** * ** * ** *** ** * * * ** ** *** * *
*** * * *** ****** ***** **
** * * ******* ** ** *** if(e==0)
** * ** * * ** **
***** * * * ** *** ** * * * * *** * * ** * *
    }
* * ** * ** if(d==0)
** * * * ** * * ***
* ** ** **** * * *** * * * * **** * *** *** ** ** * * **** ******* ** * *
    }
* **** *** * * ** * * *
**** * * * ****** *** *
** * * **** * * *** *** * * ** * ** **** *****
    }

* * * ** * **** *** * * ** 0;
}
answered by (-498 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 0
-----------Re-judge-----------
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 0
-----------Re-judge-----------
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
0 0
-----------Re-judge-----------
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()

{
* * * ** * ** * input,a,b,c,d,e,f,g,h;
*** * ** * *** * ** ** * * * ***
* * ** **** * * * ** * * * ****** * ** ****
* *** * ******* ** ** *** (input!=0)

    {
* *** * ** * *** *** * ** **** * * ** * * = input%2;
** * *** * * * *** * * ** ** ** * * **** * * = input/2;

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

    {
** * *** * ****** * * * *** * ** * * ** = input%2;
* * **** * * * * * * * ** *** * *** *** ** ** = input/2;

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

    {
** ** ** * * * ** *** * * ** * * * ** ** ** ** * * = input%2;
** ** ** * *** ** ****** ** * ***** * = input/2;
* *** ****** ******* * **
* * * **** * ** * ** ****** ** * *****
** ** ** **** **** **
** ** *** ** * * ** * * ** * = input%2;
*** ** *** * ** **** ** ** ** * * ** **** * = input/2;
** * ** ***** ** *
* **** * ** ** * *** * **** * *** *
* * * ***** ** *
** * *** ** * * ****** * * * * * ** ** ** = input%2;
* * ** * * *** ** * * * ** ** * * * * ** = input/2;
* *** ** *** * *** * * * *
*** * * *** * ***** ***
*** * *** * * ** * * * * **
* * ***** * ******* * **** * **** * * ** * = input%2;
* *** * * ** * ** * * * * **** *** *** = input/2;
* * *** ** ** *** *
******* * ** ** *** * *** * *****
* * ** ** ******* **
*** ****** ***** * ** ** * ** ** *** ** = input%2;
*** ** *** * * * * *** ** * * *** * * ***** * = input/2;
* ** ** **** * **** *
** * * * *** * * * * *** ** *

    {
* * ** ** * * * * * ******* * * * * * ** * *** = input%2;

    }




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


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

}
answered by (-281 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 ** * * * ** *


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


**** * ******* 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)
                   ^
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:172.70.100.187
©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
| 9k views
12,783 questions
183,442 answers
172,219 comments
4,824 users