2 like 3 dislike
8.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 | 8.4k views

119 Answers

0 like 0 dislike
Hidden content!
#include<stdio.h>
int main()
{
* * * ***** ** * ** a,b,c=1,d=0;
** * ** * ****** * * * * ** ** *
*** * ****** ** ***** ** *
** ** ** * * **** ***
* * * * **** * *** * * *** ** * * * * * *********** ***
** * ** * *** * ** ** * *** *** ** ** **
** ** * * * **** * * * * ******** * * * * * ***
* * ****** *** * ** * * * ** * ** * * * * * * * **
    }
*** *** * ** * ******** * ** * ***** *
}
answered by (-214 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()
{
** * *** * *** * * a,b,c=1,d=0,result;
*** * ** *** *** * * * *********
* * ** * * *** * * ** * * ** ***
** * * **** * * * * * *
*** * **** * ** * * ** * * * *** * ** ** **** *
* * * *** * ** * *** ** ** * ** * * ***
***** * ** ** * ***** * * * *** * * ** * ********
* ** ** * * ***** *** ** * * * * * ** ****
    }
* ** ** * **** * * * ** *** * **** *
}
answered by (-214 points)
0 0
prog.c: In function 'main':
prog.c:4:21: error: unused variable 'result' [-Werror=unused-variable]
     int a,b,c=1,d=0,result;
                     ^~~~~~
cc1: all warnings being treated as errors
0 like 0 dislike
Hidden content!
#include <stdio.h>
int main()
{
** * * ***** ** * * * * a,b,c,d;
* * ** ***** ****** *** * ** * ** * ** * *** ** * * * ** * ****** * *
    if
* *** * **** * * ** ** * * * **** * ** && c==0 && d==0)

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

* ** ***** ** *** * * ** if

** ** *** * * * * * * * *** *** ** * * && d==0)

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

*** * * ***** * ** * if

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

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

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

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

* *** * * ** * * ** 0;
}
answered by (-196 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()
{
* * ***** * * ** ** ** * * **** a,b,c,d,e,f,g,h;
* ** ** * * ************ * **** ***** * * **
* *** * * ***** * (a>0&&a<256)
    {   b=a%2;
* ** * * *** * * * *** * ** *** **** * *** ** *
* *** * * * *** * **** *** ** * **** * *
* * * *** * **** * *** *** ***** ** * ** * *
* ** ** **** ** **** * ** * ** * * *** *****
*** *** * * ****** * ***** * * * ***** **
***** * *** *** *** ** * *** ** * * * * * * * ***** **
** * **** * *** * * * 0;}
*** * * **** * * ****** ** ** ** * * * ** ** *
* * * ** * *** * ** * ** 0;
}
answered by (-498 points)
0 0
prog.c: In function 'main':
prog.c:17:12: error: too many arguments for format [-Werror=format-extra-args]
     printf("%d%d%d%d%d%d",h,g,f,e,d,c,b);
            ^~~~~~~~~~~~~~
cc1: all warnings being treated as errors
0 like 0 dislike
Hidden content!
#include <stdio.h>
#include <stdlib.h>
int dec_num=0;
int bin_digit[8]={0,0,0,0,0,0,0,0};
int bin_num=0;
int main()
{
* * * ** * * ** * ********* ** ******* **
* * ***** * **** ** * *** **** = dec_num%2;
* ** * **** * ** ** ** = dec_num/2;

** **** * ** * * *** * * = dec_num%2;
**** ** * * * **** = dec_num/2;

** ***** * ** * * * * = dec_num%2;
* ** * * * ** = dec_num/2;

* * * * **** ** ***** * = dec_num%2;
* * * **** * *** * *** = dec_num/2;

*** * * * * ** *** ** ** ** * * *** = dec_num%2;
** **** * *** * * = dec_num/2;

* * * * * ******** ** * = dec_num%2;
**** ** * * * * ** ** ***** = dec_num/2;

* * * **** * **** ** * * *** ** * = dec_num%2;
** * ** * * * * * ** *** = dec_num/2;

****** * ** * **** * * * * * = dec_num%2;
* * * ** *** * ***** * ** = dec_num/2;

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

* **** **** *** ** *** * * **** * * * * ** ***** *
* * * ** * * * ** ** ** * 0;
}
answered by (-286 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>
#include <stdlib.h>
short dec_num=0;
int * ***** * * ***
int bin_num=0;
int main()
{
***** * ** **** ** * ******** * ** ***** **** * * * **** *
******* * *** ** *** * ** = dec_num%2;
*** ** ** ** ** * * * = dec_num/2;

*** * * ** * * * *** *** * * = dec_num%2;
** * * *** * * * ** ** = dec_num/2;

** *** *** * ** ** * *** * = dec_num%2;
** * *** ** ** * * * ** = dec_num/2;

*** ***** **** * * * * * = dec_num%2;
*** **** **** ** * *** = dec_num/2;

*** **** * * * *** *** ** * = dec_num%2;
******** ** * ** * **** = dec_num/2;

* * * *** ** *** * * ** * *** = dec_num%2;
** ***** ** * * *** * = dec_num/2;

** * * *** * * * ******* * = dec_num%2;
* ** * *** ** * * * *** = dec_num/2;

* *** * ******* * * *** * * = dec_num%2;
******** * ** * * * * = dec_num/2;

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

*** * * * *** ** ** ******** * *******
***** * * ** * *** * 0;
}
answered by (-286 points)
0 0
prog.c: In function 'main':
prog.c:8:13: error: format '%d' expects argument of type 'int *', but argument 2 has type 'short int *' [-Werror=format=]
     scanf("%d",&dec_num);
             ^
cc1: all warnings being treated as errors
0 like 0 dislike
Hidden content!
#include <stdio.h>
#include <stdlib.h>

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

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

int main()
{
**** ** * ** ***** * * a,b,c,d,e,f,g;
* * *** * ** * *** **** *** * * ** ** *
    if (a<256)
** * *** * * * ** **** ** * * * * **
* ** *** ** * * * * **** * ** **** ** * * * *
*** * ** ** * ** * * ** ** *** ****** * *** ** ** *
***** ** **** * * * * * * * *** * * * * *
****** ** * * * * * *** ********** * * ** * **** ** *****
** ** ****** * ** * * * ** *** * ** * * * * ***** * *
* * * * * * ** * * * **** *** ** **** **
* * * * * * * * 0;
}
answered by (-498 points)
0 0
prog.c: In function 'main':
prog.c:8:5: error: this 'if' clause does not guard... [-Werror=misleading-indentation]
     if (a<256)
     ^~
prog.c:10:9: note: ...this statement, but the latter is misleadingly indented as if it is guarded by the 'if'
         c=(a/2)%2;
         ^
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.70.100.232
©2016-2024

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