Convert Decimal To Binary C Code Example


Example 1: binary to decimal in c


#include 
#include
int convert(long long n);
int main() {
long long n;
printf("Enter a binary number: ");
scanf("%lld", &n);
printf("%lld in binary = %d in decimal", n, convert(n));
return 0;
}

int convert(long long n) {
int dec = 0, i = 0, rem;
while (n != 0) {
rem = n % 10;
n /= 10;
dec += rem * pow(2, i);
++i;
}
return dec;
}

Example 2: c program from decimal to binary


#include 
#include
#include

#define D 10

int main()
{
int i, n, k, vet[D];

printf("FROM DECIMALS TO BINARIES\nEnter decimal: ");
scanf("%d", &n);

k = 0;

while (n != 0)
{
if ((n % 2) == 1)
vet[k] = 1;
else
vet[k] = 0;

n /= 2;

k++;
}

printf("Transformed into binary: ");

for(i = k - 1; i >= 0; i --)
printf("%d", vet[i]);

printf("\n\n");

system("pause");
}

Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable