peterzig

DEC to BIN while/for

Nov 10th, 2015
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.43 KB | None | 0 0
  1. // decnabinarkewhile.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <conio.h>
  6. #include <math.h>
  7.  
  8.  
  9. int main()
  10. {
  11.     long int dec, n;
  12.     int bin[100], i = 1, j;
  13.     printf("Podaj liczbe w decymalu: ");
  14.     scanf("%ld", &dec);
  15.     n = dec;
  16.  
  17.     while (n != 0)
  18.     {
  19.         bin[i++] = n % 2;
  20.         n = n / 2;
  21.     }
  22.     for (j = i - 1; j > 0; j--)
  23.     {
  24.         printf("%d", bin[j]);
  25.     }
  26.     _getch();
  27.     return 0;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment