Advertisement
Ronnie72428

Binary to decimal

Feb 8th, 2021
542
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.35 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main(){
  4.  
  5.     long
  6.         num,
  7.         decimal_val = 0,
  8.         base = 1,
  9.         rem;
  10.  
  11.     scanf("%ld", &num);
  12.  
  13.     while (num > 0){
  14.  
  15.         rem = num % 10;
  16.         decimal_val = decimal_val + rem * base;
  17.         num = num / 10 ;
  18.         base = base * 2;
  19.     }
  20.     printf("%ld", decimal_val);
  21.  
  22.     return 0;
  23. }
  24.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement