I am stuck in a problem where the user inputs the total number of digits used in numbering the pages and the program gives the total number of pages in the book.
(i.e. a book of 10 pages needs 11 digits to number because the numbers 1-9 are written with one digit and the number 10 is written with two digits; an 11 pages book requires 13 digits ...).
I tried it by using a while loop but it does not run as it exceeds the execution time.
My C code looks like this:
#include <stdio.h>
int main() {
int n, pages = 9;
scanf("%d", &n);
if (n <= 9) {
printf("%d",n);
}
else if (n % 2 == 0) {
printf("Invalid input");
}
else {
while (n < 9) {
pages++;
n -= 2;
}
}
return 0;
}
Can anyone please help me with this problem?