I just started teaching myself C from KN King C programming: a modern approach (2ndEdn).
I like it, but I hope to post an odd question here if the advice is appropriate, because, unfortunately, I don't have a teacher, and some bits raise more questions than they answer!
I ask a question about entering an integer and displaying it in octal. It says that there is an easy way to do this, but this happens later in the book. I came up with the following:
// Convert a number to octal
int n, n2, n3, n4, n5, n6;
printf("Enter a number between 0 and 32767: ");
scanf("%d", &n);
n6 = n % 8;
n5 = (n / 8) % 8;
n4 = ((n / 8) / 8) % 8;
n3 = (((n / 8) / 8) / 8) % 8;
n2 = ((((n / 8) / 8) / 8) / 8) % 8;
printf("%d%d%d%d%d", n2, n3, n4, n5, n6);
This works fine, but I donβt know how to do math and I wonder if there is a more efficient way to do this, or I did it the only way possible ...
If anyone else has a book, this is Q4 p .71.
Thank you for your time. Andrew
P.S , , ""!