0

I write the code using do while loop that takes a positive integer n, and then displays the polynomial for the octal representation of that integer. Using successive division, as demonstrated in the binary conversion example from the lesson to do this. For example, for n = 157, the program should output the following:

157 = + (5 * 8^0) + (3 * 8^1) + (2 * 8^2)

When this part is working properly, “surround” this code with a loop that gets the value of n from the user, and exits the loop when the value is negative. I used " a" variable for remainder, "i" for power of 8, but when doing the nested loop, i am not getting the n to repeatedly inputed. Please HELP My code is

#include <stdio.h>
#include <math.h>

int main()
{
    setvbuf(stdout, NULL,_IONBF,0 );
    int n, i = 0, a;
    do 
    {
        printf("enter the value of n");
        scanf("%i \n", &n);

        do 
        {
            a = n % 8;
            n /= 8;
            printf("+(%i *8^ %i)", a, i);
            i++;
        } while (n >= 1);
    }while(n>0);
    return 0;
}
tyChen
  • 1,404
  • 8
  • 27
bbb
  • 1
  • 1
  • 1
    Put another `printf()` after the inner loop, and you will see your error: `n` is most probably zero. That makes the outer loop quit. – the busybee Feb 02 '21 at 10:48
  • Maybe related: [c - Why does scanf ask twice for input when there's a newline at the end of the format string? - Stack Overflow](https://stackoverflow.com/questions/15740024/why-does-scanf-ask-twice-for-input-when-theres-a-newline-at-the-end-of-the-form) – MikeCAT Feb 02 '21 at 11:04
  • The duplicate I propos has two loops after another, not nested like here, and they are `while` not `do while`. I think the problem is however exactly the same. `n >=1` and `n>0` after all are the same for integers (signed or not). – Yunnosch Feb 02 '21 at 11:05
  • thank you i will try – bbb Feb 02 '21 at 15:38

0 Answers0