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

119 Answers

0 like 0 dislike
Hidden content!
#include ****** * * *

int main()
{
* **** ****** ***** **** ** number, n, remainder, binary = 0, place = 1;

* ** *** ****** *** ** * ** * * a number * *
* **** * ****** * ** ** ************* ** ******

* ****** * * * **** = number;

** ** * * ** * * * (n > 0)
* * *** * *** * *
* ** * ** *** **** **** ** ** * *** *** * * ** = n % 2;
****** * ** ** ** * ******** *** ** *** * * * += remainder * place;
* * * * ** ** * * * ** * * * *** **** *** ** *= 10;
* * ****** * * * ** ** ***** ** * ***** ** * * * * /= 2;
** *** *** * *** * ** **
** *** * **** * *****
* * * * * ** *** * *** * *** equivalent of %d is * * * number, binary);

* *** ****** **** * * ** * 0;
}
answered by (-168 points)
0 like 0 dislike
Hidden content!
#include<stdio.h>
int main()
{
** * * ** ** * *** **** * b1=0,b2=0,b3=0,b4=0,b5=0,b6=0,b7=0,b8=0,d;
* ** * * *** * * * ***** ** * * ** * * **
**** * * * * * ***** * *** * *** * **** *
* * * ** ** ** * * * ***** ** * ** * * ** *
* ** * * ****** * ** * * * * * **** * *
** * ** ** ** * ** * **** * ** ***
* * * **** *** ** * *** ** ** ** * * * *
** ** ** ***** * * *** **** ** * ***** ** ** *
** *** ** **** ** ** * **** * *** **** * *
* *** * *** * * *** ** * ** * * * *
* *** **** * * * * * ** * ** * * ** *** *
** * * * ** ** ** ******** * ** *** ** ***
* * *** ** *** * ** * ** * ** ** * * **
* * * ** * * *** * * * * **** ** ** ** **** *
* ****** * * * * ****** * * **** ** * **** ***
* *** * ********* * * * **** * * * * ****
****** ****** ***** ** ***** ** ** * * * ** * * ****
* * * ** ** * ** **** * * **** **** *
* * ** * ** *********** *
    {
**** ** * * ** ** * * *** * * *** ****** ***** **** * * * *** * ** ** * * *
    }
* * * ** *** *** ** * * if(b2!=0)
    {
** ****** * *** * * * * * * **** * * * * ** * **** *** ** * ** ** *
    }
***** **** * ** * **** * * if(b3!=0)
    {
* * * * ** * ** ** * ** ***** * * * * *** * ** * *** ** * * ** ** ***** ***
    }
* * *** * ** *** ** * * if(b4!=0)
    {
* ***** * ** * * *** * ** **** * ********* **** * * * * *
    }
* * ** * * ** ** * **** if(b5!=0)
* * *** ***** ** ******
* *** * ** * ** ** * * * ****** * * * * * * * ****
* * * * ** * ** ** **
* * * ******** ***** ** if(b6!=0)
    {
* * * ** ** ** * * *** ** ** * * * * ** **** * * * ** * **
    }
**** * ** ** ** * if(b7!=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,c,k;

* * * * * ** * * ** * *** * * * ** an interger in decimal\n");
** ** * * * ** *** * *** ** * **** ** &n);

* ** * * **** * * ** ***** ** * in binary is:\n", n);

** ** * **** ***** ** * (c = 4;c>=0; c--)
*** * ** * * *** * ***
** * * * * *** * ** * * ** * ** ** ** ** * ** ****
* ** *** ** * ** * * ** * * * * * ** (k & 1)
* ** * * ** *** **** ** ** *** * ***** * * *** * * ** * * * **
** * * ** *** * * * ** * ******* * * *** *
** ** ** * ** * ***** ** * * * **** ** **** * ** ** * **** * ****** *
* ****** * ** * * **
* * * * * **** * **** * ** * * *
*** * ** ** ** * * 0;
}
answered by (-167 points)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
Case 3: Wrong output
0 like 0 dislike
Hidden content!
#include * *** *

int convertTOBINARY(int);

int main()
{
* * * ** *** ** ** decimal, binary;
* * *** *** **** ***
* *** * ** * *** * * * * ** * &decimal);
* * * * * *** * * * * = * * * * *
* * *** ** **** * * * ** * ** ** ** *
*** ***** ** *
* ** * ** ** * * ** * * * 0;
}

int convertTOBINARY(int decimal)
{
* * * **** * * *** (decimal == 0)
* * * * * ** **** *** ***
* ** * * * ** * ****** ***** ** *** ** 0;
* *** *** *******
** ** *** ** ******* ****
*** * * * * ** *
*** *** * *** * * **** *** * * *** *** * * ** * (decimal % 2 + 10 * *** **** * * / 2));
*** * *** * *** * * ** **
}
answered by (-193 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()
{
   int n,a,b,c,d,e,f,g,h,bin;
   scanf *** * *** **** * **
* ** ** * ** ** *
**** ******************
*** * ** * ***
***** * *** * ** *** **
* * * * **** * * ****
* * ** **** * *
* *** ** * ** ***
** * * * *** *
* * *** ********
** **** * * *** *
*** ** ** * ** *
*** * * * ****** * * ***
** * ** * *
* ****** * **** ** * **
*** **** ** * * ***
* * *** *** **** ** * * * ** * * * * * * * *
* ** * *** **** * ** ******* * ** * **
*** *** ** * ** *** * 0;
}
answered by (-284 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()
{
   int n,a,b,c,d,e,f,g,h,bin;
   scanf *** ** * **** * **
* * * ** * * * * * *
** * ** * ** **** ** *
* * * * **** ***
**** * *** * * *** * *
** * ** ** **
*** ** **** * **
* **** * * ** * *
* ** * * * ** *
* ***** * * *** * **
** *** ** * * * *
***** ** * * * * *
*** *** *** * *
* ** ** * * *
* ** ** * *** *
**** * ***** * *
*** * **** ***** **** * ** * * *** * * ** * ***** ***** * ** **
* * * ** * **** ** ** *** *
* * ** * * * **** 0;
}
answered by (-32 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 *** ** * *
* *** * ** ** *** * ** * ***
if * *
{

n/=2;
* **
*
* * *
*
*
* *

* * *
* *
n/=2;
*
n/=2;
***
**
** **** * ** ** ** *** * ** ** * * ***** * * *****

}
** * 0;}
answered by (-304 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()
{
* ** ***** * * * *** * a,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0;
* ****** *** ******* ****** * * *** * * * *
* **** * *** ******** * (a>=256)
    {
* ** ** * ** ** ** ** * ** * ** * ** * * ** * ** * ** *** * * **
    }
* ***** * * * ******
    {
* **** ***** *** *** ** * * * * * **** ** * *
*** * * ******* ** * * * * * *** * ****** ** * * ** *
** *** * * * *** * * * * **** ** ** ** * *** ***
* *** *** *** **** ** * * ** **** ** * * * ** *** **
** ** ** * * * * * * * ** **** * ** * ** *
** * *** *** ****** * * * ***** ******* * *** * ** *
* ** **** ** ** **** * ** * * * **** * ** * * * *
* * ** ** * * ** * * * **** * * ** ** ***** * *** ***** * * **
    }
* * ** * ** * * * * (i==0)
* *** * *** * * * *
** * ** ** * * ** * ** ** ** *** ** ** * * *** ******** * **** * * * *** ***
* * * *** ** * *** * **
* *** * ** ** ** **** if (h==0)
    {
** ** *** * ** ***** * *** ** * *** * * ** * **** * **** * ****** * * *
    }
* * ** *** * * * *** * * if (g==0)
* ** * *** **** ** * *
* * ** ** **** ** * ********* * ** * * *** *** * ** ** * *** * **** * ******
*** * ** ** **
*** * ** **** ** * ** ****** if (f==0)
* * * * **** * ***** *
***** ** * * *** ***** * * ** * * ** * *** * *** * * * * ** *** * * ***** *
    }
** ** * * *** ** * * *** * if (e==0)
**** ** **
* ** * ****** * ******* * * *** ***** * * * ** * ** * ** * *** **
    }
* *** *** **** * *** **** *** *** if (d==0)
    {
** * ** * * *** * ** * * * ** * * * * ** ******* **** * *
    }
* * * ** * ** ** * * if (c==0)
*** * *** *****
* ** ***** ** **** ** *** ** ** * * ** ** * * ** * *
*** ** * ** ** * *
}
answered by (-214 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Wrong output
0 like 0 dislike
Hidden content!
#include<stdio.h>
int main()
{
* *** *** * ** ****** ** a,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0;
** *** ** * * ******* * * ** ******* * * *** *
* * * *** * ** (a>=256)
    {
********* *** * * ** *** * * *** * ** * *** ***
    }
* * * * * * * ** **
    {
** *** **** * * **** ** * ** * * * **
** * * ** * ** ** * * ** ** * * *
** ** * ****** ** * * * ** * * *** * ** ** * ***** ** *
* * ** * * **** ** ** ****** ** ** *** * ** * * * * **
****** ** *** * ** * * ** * *** ** * * * * * * *****
* *** *** * * * * ** * ** ****** ** * **** * * ** * *
* ******* ** * * *** * *** * * * *** * ** * * * *
** ***** * * ** * * * *** ** * * * * ** * *
    }
** * *** * *** * ** ** ** (i==0)
* ***** ** * * ** * *** * *
** * **** * * ** ***** **** *** ***** **** * ** * ** * ** * * * *** **
** * * ** * *
** * * *** * * * * **** if (h==0)
    {
****** ** ** * * * * ** **** **** ** * * * * ** ** * ** * *****
    }
* * * * * * * * * if (g==0)
* * * * ** **** * **
* * ******** * ** * ** * * *** ** ** * ******* * * ** **** ** * **
** * ** *** **** ** **
** * ** * **** ****** * if (f==0)
* * * *** * * ***** ***
***** * * * ** * ** * ****** * **** * * * * ** *** * *** ** * * *******
    }
***** * * ** * ** * ** if (e==0)
** * **** * * ****
******** **** * * **** ** * **** ** ***** ***** * * *** * ***
    }
** ***** * ** ** * ** ** if (d==0)
    {
* ** * * ** * * ***** ** * * *** * ** * * ****** *** * **
    }
* ** ** * * * *** if (c==0)
** * * * * * * * * *
* *** * ** * ** **** *** * ** ****** * ******* ** * *
** * ***** * * *
}
answered by (-214 points)
0 0
prog.c: In function 'main':
prog.c:47:18: warning: format '%d' expects a matching 'int' argument [-Wformat=]
         printf("%d,b");
                  ^
0 like 0 dislike
Hidden content!
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a,d1,d2,d3,d4,d5,d6,d7,d8;

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

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

** * ** ** * ** * ** **
* * * * *** ** * * * **** * * ***** ** ** ** * ** **** * *** * * * *** ***
    else if(d7!=0)
* ** *** * **** *** ** *** * * * * ** * * * * * **** ***** ** * ** * * * ****
* ** * * ** ** * * ** if(d6!=0)
* * *** *** ** * *** *** **** *** *** ** ** * * *** *** **** * *** ** *** * **** **
** ** **** ** *** *** if(d5!=0)
* *** *** * * ** * * ** * *** * **** * ** * *** *** * * ***
* * * * * * * * * * *** * if(d4!=0)
* *** * ** * * *** ** * * * * * **** ****** * ** ** **** ** * *
    else if(d3!=0)
* * *** ** * ** ******** **** * ****** * * ** *** * * * ***
    else if(d2!=0)
* * * ** ***** * ** ** ** **** ** ** * * ** *** ** ** * *** ***
    else
** *** * ** * *** ** * ** ** * * * ** *** ** * * * * *** **** ***

* * ** * ** * *** *** 0;
}
answered by (-16 points)
0 0
prog.c: In function 'main':
prog.c:10:8: warning: implicit declaration of function 'pow' [-Wimplicit-function-declaration]
     p1=pow(2,1);
        ^~~
prog.c:10:8: warning: incompatible implicit declaration of built-in function 'pow'
prog.c:10:8: note: include '<math.h>' or provide a declaration of 'pow'
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:104.23.197.94
©2016-2026

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
| 18.3k views
12,783 questions
183,442 answers
172,219 comments
4,824 users