Skip to content

Instantly share code, notes, and snippets.

@CraigRodrigues
Last active July 28, 2016 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CraigRodrigues/33af36647355ce95020c26cc67811691 to your computer and use it in GitHub Desktop.
Save CraigRodrigues/33af36647355ce95020c26cc67811691 to your computer and use it in GitHub Desktop.
CS50x Coding Contest 2016 Practice - Decimal to Binary Conversion
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cs50.h"
int main(void)
{
int i = 0;
int remainder;
int binary[50]; //this is an array placeholder for the binary digits. Once we put each one in there we will print it out again but backwards.
int decimal = GetInt();
while (decimal != 0)
{
binary[i] = decimal % 2;
decimal = decimal / 2;
i++;
}
for (int j = i - 1; j >= 0; j--)
printf("%d", binary[j]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment