Write a C program to convert decimal number into binary number?

1 answer

Answer

1147489

2026-04-24 16:45

+ Follow

#include<iOStream>

using namespace std;

int revDigit(int n){

int r,s=0;

while(n>0){

r=n%10;

n=n/10;

s=(s*10)+r;

}

return s;

}

main(){

long n,r,r1,s=0,s1=10;

cout<<"Enter a number."<<endl;

cin>>n;

while(n>0){

r=n%2;

n=n/2;

s=(s*10)+r;

}

//since we read the binary equivalent in a bottom up manner, we need to reverse it.

s1=revDigit(s);

cout<<"Binary equivalent:\t"<<s1<<endl;

system("pause");//this line is required if you are using dev c++

}

ReportLike(0ShareFavorite

Copyright © 2026 eLLeNow.com All Rights Reserved.