program that converts decimal to binary? i have compiled a progam but it prints
ID: 3613765 • Letter: P
Question
program that converts decimal to binary? i have compiled a progam but it prints wrong i.e binary of 10 is 1010 but my program prints 0101? plz help my program is as follows# include <stdio.h>
int main (void) { int num; clrscr();
printf ("Enter the no: to be converted intobinary "); scanf ("%d",&num);
while (num != 0) { printf ("%d",num % 2); num = num /2; } getch(); return 0; } i have compiled a progam but it prints wrong i.e binary of 10 is 1010 but my program prints 0101? plz help my program is as follows
# include <stdio.h>
int main (void) { int num; clrscr();
printf ("Enter the no: to be converted intobinary "); scanf ("%d",&num);
while (num != 0) { printf ("%d",num % 2); num = num /2; } getch(); return 0; } # include <stdio.h>
int main (void) { int num; clrscr();
printf ("Enter the no: to be converted intobinary "); scanf ("%d",&num);
while (num != 0) { printf ("%d",num % 2); num = num /2; } getch(); return 0; }
Explanation / Answer
Your code is producing binary in reverse order int n; while (num != 0) { n = num %d; //printf ("%d",num % 2); num = num /2;
}
//Now reverse it while(n != 0) { printf("%d", n %10); n = n/10; }
getch(); return 0; }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.