Answer to Question #280890 in C++ for Rahul Sigh

Question #280890

Any number is called beautiful if it consists of 2N digits and the sum of the first N digits is equal to the sum of the last N digits. Your task is to find the count of beautiful numbers in the interval from L to R (including L and R).


Beautiful numbers do not have leading zeroes.


Input format


•  The first line contains an integer T denoting the number of test cases.

•  The first line of each test case contains two space-separated integers L and R denoting the range interval [L, R].


Output format


For each test case, print the count of beautiful numbers in a new line.


Constraints


1 T ≤ 2000001 ≤ L ≤ R ≤ 100000000
  SAMPLE INPUT                  SAMPLE OUTPUT

     1                                                                                            9

1 100

1
Expert's answer
2021-12-20T10:00:19-0500
#include <bits/stdc++.h>


using namespace std;


int main()
{
    int l, r;
    cin >> l >> r;
    for (int j = l; j <= r; j++)
    {
        string n = to_string(j);
        int sm1 = 0, sm = 0;
        for (int i = 0; i < n.size(); i++)
        {
            if (i < n.size() / 2)
                sm1 += n[i] - '0';
            sm += n[i] - '0';
        }
        if (sm1 == sm - sm1)
            cout << "Beatiful\n";
        else
            cout << "Ugly\n";
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog