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

119 Answers

0 like 0 dislike
Hidden content!
#include <stdio.h>
int main()
{
** * *** ** * ** **** ** * a,b,c,d,e,f,g,h,i;
***** * ** * * * * ** * **** ** * **** *
* ***** * * * ** * ****
* ***** * * * * * ***
* *** * ****** * * ** ***
**** ** * * * *** * ****** * ***
* ***** * *** *** * * * ******
**** ** ** *** * ** * *** *
* * ** * *** **** ***** *
****** * * * ** * * * * ****
* ** **** * **** * **** (i!=0)
    {
* * *** ** ** *** * **** *** *** ** * * * ** * * * * ** **** * * *** * * * *
    }
* * * ** ********** ** ** if (h!=0)
    {
** * ***** ** ** *** ** * ** * ** *** * * **** * ** * * ***** ** * **
    }
**** * ** * *** **** * if (g!=0)
*** * *** *** *** **
**** * * * ** ** *** ** ** * * *** * * ** * * * ** * * * * * * * **
* * * *** *** ***** *
** * ** ** ** * * ** * **** if (f!=0)
*** * ** ** *** *
******* * *** ****** * ** ** ***** * * * ** ** **** * *
** ** *** **** ** * *
* ** ** ****** ** **** *** if (e!=0)
* * ** * ** *
** * * * * * * * * ** *** * ** *** * * **** * ***
    }
* ****** ******* * * * ** * * if (d!=0)
** * ** ** * *** *
* * ** ** * * * ** * *** *** *** * * * * *** * ** **** ** * *
* * * * ** **** ****** * *
** *** * * * * *** *** if (c!=0)
    {
* * * * ** ** * *** * * * ** **** * * ** ** ** ** * * *** ***
    }
** * * ** ***** *** *** *
    {
*** * **** * *** *** * * * * *** ***** * * ** *****
    }
* * *** ** ** ** 0;
}
answered by (-229 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,c,d,e,f,g,h,i;
* ** * *** * ** ****** * * **** *
** * ** * ******* * *
****** ** * ** ** ** * *
*** ** ** ** ** ** *** **
*** * * * ***** ** ** * ** **
***** ** ***** * *** ** * *
****** ** *** ** ** * *** *
* * * * ** * * * **** ** *
*** ** * ** * * ** ** * ** ***
***** ******* * * * ** * (i!=0)
    {
** * ****** ** * * * *** * * ** **** ** **** ****** * * * * ** ****** * *** * *
    }
**** ** * *** ** * * * * if (i==0)
    {
* *** **** * * ******* * * **** * ***** ** ***** ** **** *** * ***** *** ** ** *
    }
* ** * *** ** * *** *** *** if (h==0)
    {
* **** ** ** ** ** * *** ** ** *** **** ** * ***** * ***** **** ** *
    }
* * ****** * if (g==0)
* ** * * * ** * ** *
* ** ****** ** *** * ***** * *** * * * ** * * *** ** ** * ** * **** **
* ***** * ** ** * * **
*** ** ******* * ** **** * if (f==0)
* *** * * ***
** **** * ** * *** ** ***** ** * ***** * *** *** * *
    }
* ** * * ** ** * *** if (e==0)
* ** **** **** **
** * ** * * ***** *** ** * * * * **** *** * * * * * ** * ** ** * *
*** * ** * *****
** ****** * * if (d==0)
    {
*** * ** * ** * ******* * **** ***** * * *** **** * * ** ***
*** *** * *** ** * * *
* ** ***** **** * * ** * if (c==0)
    {
***** * * * **** * ** * ** ** * * * * ** * ** *** * * * * ** *
* ** * * **** *** **
* ** ** ** * ** * * ** * * * 0;
}
answered by (-229 points)
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
Case 3: Correct output
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>
int main()
{
    int a,b,c,d,e,f,g,h;
** ** *** * * ** * ** * ** **** **** * ** *
**** * * * **** **
** * ** *** * ** * * ** *
*** * * * * * ** *** * * *
* ** ** * ** ********
* * * *** *** **** * * ** **
** * * * * ** * * ** * ** * *
** * * * *** **** * * *** * ** ** **** *
}
answered by (-329 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Wrong output
Case 3: Wrong output
0 like 0 dislike
Hidden content!
#include <stdio.h>

int convert(int);

int main()
{
* **** * * * * * **** *** dn, bn;

* * ** **** ** * * ******** *** * * * ** * &dn);

** * * ** * * * ** * * ** *
* * * * * ** *** * * * * ** ** ** **** = convert(dn);
*** * **** ** * ** *** * ** * ***** ** * ** * *** ** ** ** bn);
* * * *** * **** *
* * * * * * *** **** 0;
}

int convert(int dn)
{
*** * **** * * ****** * (dn == 0)
** ** * ***** *** *** * **
* *** *** ***** ** * * ** * *** * * * * ** 0;
*** ***** ** * ******
** *** *** * *
***** * **** * *** ***
*** *** ** *** * **** ** ** *** ***** * * (dn % 2 + 10 * convert(dn / 2));
**** * *** ** * ** *** * ****
}
answered by (-168 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 <stdio.h>
#include <stdlib.h>

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

int main()
{
    int a,b,c,d,e,f,g,h,i;
* ** ** * ** ** * ** **** ** *
* * ** * *** * ******** ** ** *
** ** ** *** * ** ****
** *** ** * * * ** **** ***
* ** ** * * **** * ** * *
***** * * * * * ** * **** * * ****
** * ** * ***** * *** *** *
* * ** **** * *** * * **
* * ** * *** ** * * **** * *** *
** * **** ** ** ** **** *** *** * * * ** ** * ** ** * *
** ** *** * *** ** 0;
}
answered by (-108 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,a0,a1,a2,a3,a4,a5,a6,a7;
* * ** ** * ** * ** * ***** * * *** ** *

** * * * ** * * * * ***
* * ** * * *** ** * ***
**** ** * **** * * *
* *** * * ** * * * *
*** ***** * * ** ** ****
*** **** * ** ***** * *
* **** *** * *** * * * ** * *
* * ** * * ** ******* *
*** *** *** ***** ** **
    n%=8;
** ** ***** * *** * *
* * ** *** * ** * *
* * ** ** * * * *** * *** *
* * * ** * *** **
** * * * ** *** * ** * ** ***

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

* * * * **** ** * * * ***** 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()

{
    int n,a0,a1,a2,a3,a4,a5;
* ** *** * ***** * ** * * * ******* *** **** *******

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

* * ** * * * * * * **** * * ** *** ** * * * *** **
    
*** ** ** * * * ** ** 0;
}
answered by (-249 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Wrong output
Case 3: Wrong output
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:172.70.127.103
©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
| 8.7k views
12,783 questions
183,442 answers
172,219 comments
4,824 users