fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main(){
  5.  
  6. int a;
  7. int b;
  8. int c[255];
  9. char opt[100];
  10.  
  11. printf("10進数の値を入力してください > ");
  12. scanf("%d", &a);
  13. printf("計算式を表示しますか? y(yes) or n(no) > ");
  14. scanf("%s", opt);
  15.  
  16. if(strcmp(opt, "y") == 0){
  17. printf("\n計算式: \n");
  18. }
  19.  
  20. int i = 0;
  21. while(a > 0){
  22. b = a / 2;
  23. c[i] = a % 2;
  24.  
  25. if(strcmp(opt, "y") == 0){
  26. printf("%d ÷ 2 = %d 余り %d\n", a, b, c[i]);
  27. }
  28.  
  29. a = b;
  30. i++;
  31. }
  32.  
  33. printf("\n2進数: ");
  34.  
  35. int j;
  36. for(j = i-1; j >= 0; j--){
  37. printf("%d", c[j]);
  38. }
  39.  
  40. printf("\n");
  41. return 0;
  42. }
Success #stdin #stdout 0s 9432KB
stdin
10
y
stdout
10進数の値を入力してください > 計算式を表示しますか? y(yes) or n(no) > 
計算式: 
10 ÷ 2 = 5 余り 0
5 ÷ 2 = 2 余り 1
2 ÷ 2 = 1 余り 0
1 ÷ 2 = 0 余り 1

2進数: 1010