FiddleComputers

NF05 TD02

Sep 18th, 2019 (edited)
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.16 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. void exo7()
  6. {
  7.     //Convert binary to decimal
  8.     int n;
  9.     printf("Entrer un nombre en binaire.\n");
  10.     scanf("%d", &n);
  11.     int old_n = n;
  12.  
  13.     int decimalNumber = 0, i = 0, temp;
  14.     while(n != 0)
  15.     {
  16.         temp = n%10;
  17.         n /= 10;
  18.         decimalNumber += temp*pow(2, i);
  19.         i++;
  20.     }
  21.  
  22.     printf("%d in decimal is %d\n\n", old_n, decimalNumber);
  23.  
  24.     printf("Entrer un nombre decimal entier.\n");
  25.     scanf("%d", &decimalNumber);
  26.     int old_dec = decimalNumber;
  27.  
  28.     i = 1, n = 0;
  29.     while(decimalNumber != 0)
  30.     {
  31.         temp = decimalNumber%2;
  32.         decimalNumber /= 2;
  33.         n += temp * i;
  34.         i *= 10;
  35.     }
  36.  
  37.     printf("%d in binary is %d", old_dec, n);
  38. }
  39.  
  40. void exo8()
  41. {
  42.     int decimal, octal = 0, i = 0, temp;
  43.  
  44.     printf("Entrer un nombre decimal entier.\n");
  45.     scanf("%d", &decimal);
  46.  
  47.     printf("%d in octal is ", decimal);
  48.  
  49.     while(decimal != 0)
  50.     {
  51.         temp = decimal%8;
  52.         decimal /= 8;
  53.         octal += temp*pow(8, i);
  54.         i++;
  55.     }
  56.  
  57.     printf("%d", octal);
  58. }
  59.  
  60. int main()
  61. {
  62.     exo8();
  63.     return 0;
  64. }
Add Comment
Please, Sign In to add comment