1# Python program to convert decimal to binary
2
3# Function to convert Decimal number
4# to Binary number
5def decimalToBinary(n):
6 return bin(n).replace("0b", "")
7
8# Driver code
9if __name__ == '__main__':
10 print(decimalToBinary(8))
11 print(decimalToBinary(18))
12 print(decimalToBinary(7))
13
14Output:
151000
161001
1public class DecimalToBinaryExample2{
2public static void toBinary(int decimal){
3 int binary[] = new int[40];
4 int index = 0;
5 while(decimal > 0){
6 binary[index++] = decimal%2;
7 decimal = decimal/2;
8 }
9 for(int i = index-1;i >= 0;i--){
10 System.out.print(binary[i]);
11 }
12System.out.println();//new line
13}
14public static void main(String args[]){
15System.out.println("Decimal of 10 is: ");
16toBinary(10);
17System.out.println("Decimal of 21 is: ");
18toBinary(21);
19System.out.println("Decimal of 31 is: ");
20toBinary(31);
21}}
1#include <iostream>
2#include <stdlib.h>
3
4int main ()
5{
6 int i;
7 char buffer [33];
8 printf ("Enter a number: ");
9 scanf ("%d",&i);
10 itoa (i,buffer,10);
11 printf ("decimal: %s\n",buffer);
12 itoa (i,buffer,16);
13 printf ("hexadecimal: %s\n",buffer);
14 itoa (i,buffer,2);
15 printf ("binary: %s\n",buffer);
16 return 0;
17}
18
1# Python program to convert decimal to binary
2
3# Function to convert Decimal number
4# to Binary number
5def decimalToBinary(n):
6 return bin(n).replace("0b", "")
7
8# Driver code
9if __name__ == '__main__':
10 print(decimalToBinary(8))
11 print(decimalToBinary(18))
12 print(decimalToBinary(7))
13
1//Java Solution for Decimal To Binary Conversion
2
3import java.util.*;
4public class DecimalToBinary {
5
6 public static void main(String[] args) {
7 // TODO Auto-generated method stub
8 Scanner sc = new Scanner(System.in);
9 int dec = sc.nextInt();
10 StringBuffer sb = new StringBuffer();
11 while(dec!=0)
12 {
13 sb.append(dec%2);
14 dec=dec/2;
15 }
16 System.out.println(sb.reverse());
17
18 }
19
20}
21