Write C Program to convert decimal number to binary using function


Introduction

I have used Code::blocks 12 compiler for debugging purpose. But you can use any C programming language compiler as per your availability.


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

Write C Program to convert decimal number to binary using function


  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. //Function declaration
  5. long long convertDecimalToBinary(int n);
  6.  
  7. int main()
  8. {
  9. int n;
  10. printf("Enter a decimal number: ");
  11. // Inputting numbers from user
  12. scanf("%d", &n);
  13. printf("%d in decimal = %lld in binary", n, convertDecimalToBinary(n));
  14. return 0;
  15. }
  16.  
  17. long long convertDecimalToBinary(int n)
  18. {
  19. long long binaryNumber = 0;
  20. int remainder, i = 1, step = 1;
  21.  
  22. while (n!=0)
  23. {
  24. remainder = n%2;
  25. //Printing decimal number to binary
  26. printf("Step %d: %d/2, Remainder = %d, Quotient = %d\n", step++, n, remainder, n/2);
  27. n /= 2;
  28. binaryNumber += remainder*i;
  29. i *= 10;
  30. }
  31. return binaryNumber;
  32. }
  33.  

Result

C language

     





Related Topics




Popular Posts