1
2// C++ program to convert a decimal
3// number to binary number
4
5#include <iostream>
6using namespace std;
7
8// function to convert decimal to binary
9void decToBinary(int n)
10{
11 // array to store binary number
12 int binaryNum[32];
13
14 // counter for binary array
15 int i = 0;
16 while (n > 0) {
17
18 // storing remainder in binary array
19 binaryNum[i] = n % 2;
20 n = n / 2;
21 i++;
22 }
23
24 // printing binary array in reverse order
25 for (int j = i - 1; j >= 0; j--)
26 cout << binaryNum[j];
27}
28
29// Driver program to test above function
30int main()
31{
32 int n = 17;
33 decToBinary(n);
34 return 0;
35}
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
1# Function to convert decimal to binary
2# using built-in python function
3def decimalToBinary(n):
4 # converting decimal to binary
5 # and removing the prefix(0b)
6 return bin(n).replace("0b", "")
7
8# Driver code
9if __name__ == '__main__':
10 # calling function
11 # with decimal argument
12 print(decimalToBinary(77))