-3

It is quite simple, indeed.

int main()
{
    int n,i,j;
    n = 20;
    i = 0;
    char ch[8];

    do
    {
        ch[i] = (n%2) + '0';
        n /= 2;
        // SIMPLE WAY
        if(n != 0)
            i++;
    }
    while(n != 0);

    for(j=0; j<=i; j++)
    {
        printf("%c",ch[i-j]);
    }

    return 0;
}

But i don't like this WAY

I tried the below WAY but code is bad

int main()
{
    int n,i,j;
    n = 20;
    i = 0;
    char ch[8];

    do
    {
        ch[i] = (n%2) + '0';
        n /= 2;
    }
    while(n != 0 && i++); // THIS

    for(j=0; j<=i; j++)
    {
        printf("%c",ch[i-j]);
    }

    return 0;
}

How to get the value incremented only when the loop is true with BEST WAY? OR just correct 2nd way while(n != 0 && i++)

  • 1
    What do you mean by "best way"? (...and why is it always capitalized?) – Yksisarvinen Oct 28 '18 at 21:15
  • **best way:** I do not like normal ways That's why I always look for genius and fairly easy ways **always capitalized:** Draw attention –  Oct 28 '18 at 21:24
  • 1
    What do you have against the "normal way"? Perhaps it's the "normal way" for a reason? – Joe C Oct 28 '18 at 21:51
  • for (int n = 20; n > 0; n /= 2) ch[i++] = (n%2) + '0'; followed by for (int j = 0; j < i; j++) – Hans Passant Oct 28 '18 at 21:51
  • @HansPassant [`for (int n = ...; n > 0`](https://stackoverflow.com/questions/53036012/post-increment-in-do-while-loop-if-condition-is-true-best-way/53036133?noredirect=1#comment92972941_53036012) has different print out when [n==0](https://stackoverflow.com/questions/53036012/post-increment-in-do-while-loop-if-condition-is-true-best-way/53036133?noredirect=1#comment92972591_53036057). – chux - Reinstate Monica Oct 28 '18 at 21:54
  • I could point out that 20 isn't 0, but that would be entirely too duh. Never slows down anybody in the [c++] tag though, odd isn't it? Just address the author to point out corner-cases. – Hans Passant Oct 28 '18 at 21:55
  • Mohammed Esafi, Do you want a C or C++ answer? - best to chose only one. – chux - Reinstate Monica Oct 28 '18 at 21:58
  • @chux C or C++ I thought there will not be any difference –  Oct 28 '18 at 22:25
  • 1
    The _best_ answer can differ between languages. Tagging a question as `C` and `C++` when only one is really needed attracts down-votes and votes-to-close for being too broad or unclear. – chux - Reinstate Monica Oct 28 '18 at 22:29
  • Even stack overflow??? There's already a billion of tutorials that have C/C++ tags, polutting C/C++ ecosystem with cancer, and it's spreading even to SO? Come on now. SO should just prevent users from putting more than one distinct programming language or we'll have why my C code doesn't compile in g++ all over again. – Purple Ice Oct 29 '18 at 10:25
  • @pur: There are valid reasons to tag a question with both C and C++. [Proposed update to C and C++ tag usage wikis](https://meta.stackoverflow.com/q/374306/1889329) has more information, in case you care. – IInspectable Oct 29 '18 at 13:08
  • This question has nothing to do with C++ so I don't care. I know that there's topics that could be *tag as many languages as possible* worthy, but we don't tag all programming languages, do we? Imagine if someone tagged every single language that has arrays when the problem they were having was something to do with arrays in C. Don't you think it's just kind of silly? Might aswell start tagging `C` `C++` and `C#` every time you have problems with C. Makes no sense whatsoever. But wait, they all have C in them, must be same thing of course! – Purple Ice Oct 29 '18 at 13:18

5 Answers5

6

How to get the value incremented only when the loop is true with BEST WAY?
OR just correct 2nd way while(n != 0 && i++)

I recommend neither approach.

There is no need for a special test to see if code should increment i.

do {
  ch[i] = (n%2) + '0';
  n /= 2;
  i++;
} while(n != 0);
// Just decrement after the loop
i--;

for(j=0; j<=i; j++) {
    printf("%c",ch[i-j]);
}

Alternatively do not decrement at all

do {
  ch[i] = (n%2) + '0';
  n /= 2;
  i++;
} while(n != 0);

for(j=0; j<i; j++) {
  printf("%c",ch[i-1-j]);
}

Or use a do ... while for printing also.

do {
  ch[i++] = (n%2) + '0';
  n /= 2;
} while(n);

do {
  printf("%c",ch[--i]);
} while (i);

Notes:

char ch[8]; is too small for an int outside the range [-255 ... 255]. For 32-bit int, use char ch[32]; or wider. In general, use char ch[sizeof(int) * CHAR_BIT];

ch[i] = (n%2) + '0'; will certainly incur unexpected results when n < 0. Consider unsigned types instead.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
1

Well, you can always do:

do {
   // code
} while(n != 0 && ++i);

This way, i is incremented right after the first part of && evaluates to True, and && is applied to n != 0 and the updated version of i.

ForceBru
  • 43,482
  • 10
  • 63
  • 98
1

In C++, you might do:

std::cout << std::bitset<8>(n); // 00010100 for n = 20

if leading 0 are ok.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
-1

It can be like this:

int n = 20;
int i = 7;
char ch[] = "00000000";
while (n) // if n already = 0 then do nothing
{
    ch[i--] = (n % 2) + '0'; // assign then increment
    n /= 2;                // shift to right same as n >>= 1;
}
printf ("%08s", ch);
Andrey Sv
  • 247
  • 3
  • 6
  • 1
  • Yes, but question about only this part of code. Of course good practice is initializing variable for exemple `char ch[8] = {0};` .And last part of code can be more simple like `printf("%08s",ch);` – Andrey Sv Oct 28 '18 at 21:45
  • 1) Even with initializing, then code is still first printing `printf("%c",0);` for `n < 128`. 2) `printf("%08s",ch);` is undefined behavior when all 8 `char` are written as `ch` is not then a _string_ as it lacks a _null character_. IAC, `"%08s"` prints in the wrong order as desired. – chux - Reinstate Monica Oct 28 '18 at 21:49
-2
do
{ .......... }while(n && i++ ? 1 : n);

this stuff about && actions called short-circuit evaluation