Program Discussion :: Bitwise Operator
20 / 15
Write an efficient program to covert decimal number into binary number
Answer:
#include
using namespace std;
int main()
{
long bin, dec = 0, rem, num, base = 1;
cout > num;
bin = num;
while (num > 0)
{
rem = num % 10;
dec = dec + rem * base;
base = base * 2;
num = num / 10;
}
cout
Asked In ::
Language:
Varun
1 Jul, 2017 11:07 PM
<!DOCTYPE html>
<html>
<body>
<?php
function Dec_bin() {
$no = 80;
echo "Given decimal no is : " . $no."<br/>";
$binary_no= decbin ($no) ;
echo "Binary equivalent of given integer no is : " . $binary_no . "<br/>";
}
Dec_bin() ;
?>
</body>
</html>
Language:
Shreya
7 Jul, 2017 9:30 AM
#include <stdio.h>
int convert(int);
int main()
{
int dec, bin;
printf("Enter a decimal number: ");
scanf("%d", &dec);
bin = convert(dec);
printf("The binary equivalent of %d is %d.\n", dec, bin);
return 0;
}
int convert(int dec)
{
if (dec == 0)
{
return 0;
}
else
{
return (dec % 2 + 10 * convert(dec / 2));
}
}
Language:
Nikhil
7 Jul, 2017 9:30 AM
#include<iostream>
using namespace std;
int main()
{
long bin, dec = 0, rem, num, base = 1;
cout << "Enter the binary number(1s and 0s) : ";
cin >> num;
bin = num;
while (num > 0)
{
rem = num % 10;
dec = dec + rem * base;
base = base * 2;
num = num / 10;
}
cout << "The decimal equivalent of " << bin << " : " << dec << endl;
return 0;
}
Language:
Puneet
16 Dec, 2018 12:03 PM
import java.util.Scanner*;
class GFG
{
static void decToBinary(int n)
{
int[] binaryNum = new int[1000];
int i = 0;
while (n > 0)
{
// storing remainder in binary array
binaryNum[i] = n % 2;
n = n / 2;
i ;
}
// printing binary array in reverse order
for (int j = i - 1; j >= 0; j--)
System.out.print(binaryNum[j]);
}
public static void main (String[] args)
Scanner is= new Scanner(System.in);
int n= is.nextInt();
{
decToBinary(n);
}
}
Language:
Siddhartha Paul
19 Jun, 2019 2:16 PM
import java.io.*;
public class DecimalToBinary
{
public static void main(String[] args)throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int dNum,count=0;
int x;
String s ="";
System.out.print("Enter decimal number: ");
dNum=Integer.parseInt(br.readLine());
while(dNum>0)
{
x=dNum%2;
if(x==1)
{
count+=1;
}
s=s+""+x;
dNum=dNum/2;
}
StringBuffer sb=new StringBuffer(s);
System.out.println("Corresponding Binary number: "+sb.reverse());
}
}