Facebook
From Miczeq, 7 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 257
  1. #include <cstdio>
  2. #include <conio.h>
  3. #include <cstring>
  4. #include <math.h>
  5.  
  6. int main()
  7. {
  8.         printf("Podaj wartosc(2): ");
  9.         char line[256];
  10.         int i;
  11.  
  12.         if (fgets(line, sizeof(line), stdin))
  13.         {
  14.                 if (sscanf_s(line, "%d", &i))
  15.                 {
  16.                         printf("Podana wartosc to: %s", line);
  17.                 }
  18.         }
  19.  
  20.         printf("\nDlugosc: %d", strlen(line) - 1);
  21.  
  22.         int result = 0;
  23.  
  24.         int l = strlen(line) - 2;
  25.  
  26.         for (int i = 0; i < strlen(line) - 1; i++)
  27.         {
  28.                 if (line[l] == '1')
  29.                 {
  30.                         result += i == 0 ? 1 : pow(2, i);
  31.                 }
  32.                 l--;
  33.         }
  34.  
  35.         printf("\nDziesietnie: %d", result);
  36.  
  37.         _getch();
  38.         return 0;
  39. }