t1nman

lab1_2

Apr 30th, 2012
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.43 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. /* Converting binary number to decimal */
  5.  
  6. int conv(long bin);
  7.  
  8. main()
  9. {
  10.     long binary;
  11.     int decimal;
  12.  
  13.     puts("enter binary number");
  14.     scanf("%ld", &binary);
  15.    
  16.     printf("%d\n", conv(binary));  
  17.  
  18.     return 0;
  19. }
  20.  
  21. int conv(long bin)
  22. {
  23.     int i = 0, num, decimal = 0;
  24.    
  25.     while (bin != 0) {
  26.         num = bin % 10;
  27.         decimal += pow(2,i) * num;
  28.         ++i;
  29.         bin /= 10;
  30.     }
  31.  
  32.     return decimal;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment