fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3. long long convertDecimalToBinary(int n);
  4.  
  5. int main()
  6. {
  7. int n;
  8. printf("Enter a decimal number: ");
  9. scanf("%d", &n);
  10. printf("%d in decimal = %lld in binary", n, convertDecimalToBinary(n));
  11. return 0;
  12. }
  13.  
  14. long long convertDecimalToBinary(int n)
  15. {
  16. long long binaryNumber = 0;
  17. int remainder, i = 1, step = 1;
  18.  
  19. while (n!=0)
  20. {
  21. remainder = n%2;
  22. printf("Step %d: %d/2, Remainder = %d, Quotient = %d\n", step++, n, remainder, n/2);
  23. n /= 2;
  24. binaryNumber += remainder*i;
  25. i *= 10;
  26. }
  27. return binaryNumber;
  28. }
Success #stdin #stdout 0s 9432KB
stdin
56
stdout
Enter a decimal number: Step 1: 56/2, Remainder = 0, Quotient = 28
Step 2: 28/2, Remainder = 0, Quotient = 14
Step 3: 14/2, Remainder = 0, Quotient = 7
Step 4: 7/2, Remainder = 1, Quotient = 3
Step 5: 3/2, Remainder = 1, Quotient = 1
Step 6: 1/2, Remainder = 1, Quotient = 0
56 in decimal = 111000 in binary