DECIMAL TO BINARY CONVERSION
Consider the following I/O samples.
SAMPLE INPUT : 13
SAMPLE OUTPUT: 1101
#include <stdio.h>
int main()
{
int n, base=1, rem, ans=0;
scanf("%d",&n);
while(n)
{
rem=n%2;
ans=rem*base+ans;
n=n/2;
base=base*10;
}
printf("%d",ans);
return 0;
}
If you have any feedback about this article and want to improve this, please comment in the comment section.
C PROGRAM:
#include <stdio.h>
int main()
{
int n, base=1, rem, ans=0;
scanf("%d",&n);
while(n)
{
rem=n%2;
ans=rem*base+ans;
n=n/2;
base=base*10;
}
printf("%d",ans);
return 0;
}
OUTPUT:
The given source code in C program is short and simple to understand. The source code is well tested in DEV-C++ and is completely error free.
If you have any feedback about this article and want to improve this, please comment in the comment section.
Comments
Post a Comment