c - program to print all decimal numbers from 0 to a given number in binary format -
I'm new to C I was writing a program to print a decimal number from 0 to all decimal numbers (as provided by the user) but my output was not expected. I do not understand what is the mistake I have written in the program ....
#include & lt; Stdio.h & gt; Int main () {int x, n; Printf ("Enter any number \ n"); Scanf ("% d", & amp; x); N = x; Printf ("Number in Binary Format \ n"); Whereas (n! = 0) {int num [8] = {0,0,0,0,0,0,0,0,0,0,0,0}}; Int y [8] = {0,0,0,0,0,0,0,0,0,0,0,0}; Int i = 7; While (x! = 0) {y [i] = x% 2; X = X / 2; I = i-1; } While (i! = 8) {printf ("% d", y [i]); I = I + 1; } Printf ("\ n"); N = n-1;
The problem is with its first loop
while (x! = 0) {y [i] = x% 2; X = X / 2; I = i-1; } When this loop is over, I will not not in your case, and your second loop is based on this assumption. Just put one
i = 0; This should work after your first loop.
Comments
Post a Comment