Advertisement
huyhung94

Chuyển đổi nhị phân

Mar 12th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. int main()
  5. {
  6.     long n;
  7.     printf("Nhap so nhi phan: ");
  8.     scanf("%d", &n);
  9.     printf("So nhi phan: %d = %d trong he thap phan.", n, bin2dec(n));
  10.     return 0;
  11. }
  12. int bin2dec(long n)
  13. {
  14.     int result = 0, i = 0, temp;
  15.     while (n!=0)
  16.     {
  17.         temp = n % 10;
  18.         n /= 10;
  19.         result += temp * pow(2, i);
  20.         ++i;
  21.     }
  22.     return result;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement