Home > Enterprise >  Finding Number of pages from the total number of digits used to number the pages
Finding Number of pages from the total number of digits used to number the pages

Time:02-14

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?

CodePudding user response:

else if(n%2==0)
{
    printf("Invalid input");
}

Please note that there are books with more than 100 pages:

A book with 102 pages has 198 digits!

 while(n!=0)
 {
     pages  ;
     n-=2;
 }

This loop will run forever:

Because n is initially an odd number, n-2 is also odd. So n will always be odd (maybe negative) if n-=2 is the only instruction that modifies n. n cannot become 0, so the loop will run forever.

You also have to consider that books with more than 100 pages or with more than 1000 pages may exist.

I would do it the other way round and count pages up from 1 to N. I would sum up the digits required for the given number of pages and stop when the number of digits is exceeded:

int n, pages=0, digits=0;
int realdigits=0, neededdigits, tooLarge;

/* "realdigits" is the number of digits required
 * by "pages" pages; loop until
 * "realdigits" >= "digits" */
while(realdigits < digits)
{
    pages  ;
    /*
     * Add some code that calculates:
     * neededdigits = Number of digits required by
     *                the number "pages"
     */
    realdigits  = neededdigits;
}
/* Case: "(pages-1)" pages need less digits
 * than "digits" but "pages" pages need more
 * digits... */
if(realdigits > digits)
{
    printf("Invalid number\n");
}
else
{
    printf("%d pages\n", pages);
}

I would use a loop based on a condition to calculate the number of digits required by the number pages:

neededdigits = 1;
tooLarge = 10; /* Number that is too large for digits */
while(pages >= tooLarge)
{
    neededdigits  ;
    tooLarge *= 10;
}

Note that in "C-like" programming languages (C, C , Java, C#, PHP ...) you may use the for keyword for "condition-based" loops ...

CodePudding user response:

In your posts body you do not ask a question (apart from "Can somebody help me?", which is not considered a question here). In your comments you ask for the main() function, or the logic, or the pseudo code. Sorry, that is not how StackOverflow is meant to be used.
But finally you ask how to arrive at the needed formula.
With How do I ask and answer homework questions? in mind I will help you with that.

  • make a table of the simplest case (1,2,3,4,5,6,7,8,9) pages and digits, the same
  • find the formula for that
  • check the next complex case (10,11,12,13,14,...99) pages and digits, there is a very simple relation
  • consider which influence the pages 1...9 have on the formula, think offset
  • consider the next complex case (100, 101, .... 999)
  • and proceed to the mentioned limit of 2 billion pages

CodePudding user response:

Consider using successive tests like this:

  • if the number <= 9, the number of pages is number, else set pages = 9 and subtract 9 from number.
  • if the number <= 90 * 2, the number of pages is pages number / 2, else set pages = 90 and subtract 90 * 2 from number.
  • if the number <= 900 * 3, the number of pages is pages number / 3, else set pages = 900 and subtract 900 * 3 from number.
  • etc.

You can write a loop to solve the problem without making an assumption on the range of the type used to store number and pages.

int number_of_pages(int number_of_digits) {
    int n1 = 9, n2 = 1, pages = 0;
    while (number / n2 > n1) {
        pages  = n1;
        number -= n1 * n2;
        n1 *= 10;
        n2  = 1;
    }
    return pages   number / n2;
}
  • Related