C C++ program to convert decimal to binary

 

Flowchart

Decimal to binary conversion flowchart

 

Coding in C

#include<stdio.h>
int main()
{
int decimal, binary=0, digit, base=1;
printf(“Enter decimal number: “);
scanf(“%d”,&decimal);
while(decimal!=0)
{
digit=decimal%2;
binary=digit*base+binary;
base=base*10;
decimal=decimal/2;
}
printf(“Binary number = %d”,binary);
return 0;
}

 

Coding in C++

#include<iostream>
using namespace std;
int main()
{
int decimal, binary=0, digit, base=1;
cout<<“Enter decimal number: “;
cin>>decimal;
while(decimal!=0)
{
digit=decimal%2;
binary=digit*base+binary;
base=base*10;
decimal=decimal/2;
}
cout<<“Binary number = “<<binary;
return 0;
}

 

Output

Decimal to binary conversion output

 

Published by

admin

I am a computer teacher, programmer and web developer