Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- /* Function for Determining the Reverse Number */
- long long int rev(long long int x) //The Parameter takes the Number as Input (as "x"), sent by the Main Function
- {
- long long int rn = 0; //the reversed number will be stored in "rn"
- for (x ; x>0 ; x/= 10) //loop for calculation of the reversed number
- rn = ((rn * 10) + (x % 10)); //simple mathematics logic
- return rn; //returns the reversed number.
- }
- int main()
- {
- int T, a; //"T" is for TestCase, "a" is for number of attempts.
- long long int P, rP; //"P" is the Number entered by user, "rP" is the reversed number of "P".
- cin >> T; //takes input of the Number of TestCases
- while (T--)
- {
- cin >> P; //input of the original number entered by user
- a = 1; //sets number of attempt to 1, as it's the first attempt
- rP = rev(P); //calls the reverse function and sends the value of "P".
- //in return it gets the reversed number of "P" and assigns it to "rP"
- while (1)
- {
- P = P + rP; //adds up the original number and the reversed number
- //and assigns the sum to "P"(the original Number)
- rP = rev(P); //reverses the sum and assigns it to "rP"
- if (P - rP == 0){ //checks if the sum ("P") is Palindrome
- cout << a << " " << P << endl; //(prints the number of attempts
- //and the Palindrome Number (Current Value of "P"
- break; //if Palindrome, this breaks the loop as we don't need it anymore.
- }
- else
- a++; //if the value of "P" is NOT Palindrome, it increases the number of attempts
- //and continues the reverse and add process
- if(a>999) //if the number of attempts is NOT Less than 1000, this breaks the loop
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement