Number of natural numbers consisting of more than a number and less than b numerals



  • I tried to solve this task: "How many natural numbers made of at least a number and not more than a b numer." These numbers do not exceed 10,000. Here's my code:

    using namespace std;
    int main() {
       int a,b,c,d;
        cin >> a >> b;
        c=1;
        d=1;
        b=b+1;
        for (int i=1; i<a; i++) c=c*10;
        for (int i=1; i<b; i++) d=d*10;
    c--;
    d--;
        cout << d-c;
    }
    

    I checked, it works, but it doesn't work. I know my code can't work with the numbers of 10,000 signs, but I don't know how to fix it.



  • cin >> a >> b;
    for(int i = 0; i < b-a+1; ++i) cout << 9;
    for(int i = 0; i < a-1; ++i) cout << 0;
    

    Like that, no?


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2