Here is my algorithm in C to convert an integer to a binary number:
# include \u0026lt;stdio.h>
# include \u0026lt;math.h>
# include \u0026lt;stdlib.h>
main () {
int num1, i = 0, choice;
double base = pow (2, i);
printf ("Enter an integer to convert Binary: ");
scanf ("% d ", & num1);
while (base<=num1)
{
printf(" %lf", base);
i++;
base= pow(2,i);
}
i--;
printf("\n i=%d\n\n", i);
base= pow(2,i);
while (num1>0)
{
if (base
{
num1-=base;
i--;
base= pow(2,i);
printf("1");
}
else if (base>num1)
{
i--;
base= pow(2,i);
printf("0");
}
else if (base=num1)
{
num1-=base;
printf("1");
while (i>0)
{
printf("0");
i--;
}
}
}
printf("\n\n.");
system("pause");
return 0;}
0 comments:
Post a Comment