Skip to content

Instantly share code, notes, and snippets.

@CraigRodrigues CraigRodrigues/decimaltobinary.c Secret
Last active Jul 28, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.