Advertisement
savovaap_

Decimal to Binary

Mar 29th, 2023
731
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void decimal_to_binary(int decimal) {
  5.     int binary[32],index=0;
  6.     while (decimal>0) {
  7.         binary[index]=decimal%2;
  8.         decimal=decimal/2;
  9.         index++;
  10.     }
  11.    
  12.     printf(" Binary representation: ");
  13.     for (int i=index-1;i>=0;i--) {
  14.         printf("%d",binary[i]);
  15.     }
  16. }
  17.  
  18. int main() {
  19.     int decimal;
  20.     printf(" ");
  21.     scanf("%d",&decimal);
  22.     printf("Decimal number: %d", decimal);
  23.     decimal_to_binary(decimal);
  24.     return 0;
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement