program from decimal to binary code example
Example 1: c program from decimal to binary
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#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");
}
Example 2: decimal to binary
import java.util.*;
public class DecimalToBinary {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int dec = sc.nextInt();
StringBuffer sb = new StringBuffer();
while(dec!=0)
{
sb.append(dec%2);
dec=dec/2;
}
System.out.println(sb.reverse());
}
}