Decimal to Binary using while statement in C program

//using while statement
#include<stdio.h>
#include<conio.h>
main()
{
int n,m,r,p=1,t=0;
printf("Enter any decimal number:");
scanf("%d",&n);
m=n;
while(n!=0)
{
r=n%2;
t=t+(r*p);
n=n/2;
p=(p*10);

}
printf("Decimal Number %d=Binary Number:%d",m,t);
}

DECIMAL To BINARY USING RECURSION
// C Program to Convert a Number Decimal System to Binary System using Recursion

#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));
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Enter Captcha Here : *

Reload Image