Advertisement
skb50bd

10018 w/ Comments & Details

May 16th, 2015
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.72 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. /* Function for Determining the Reverse Number */
  5. long long int rev(long long int x) //The Parameter takes the Number as Input (as "x"), sent by the Main Function
  6. {
  7.     long long int rn = 0; //the reversed number will be stored in "rn"
  8.  
  9.     for (x ; x>0 ; x/= 10) //loop for calculation of the reversed number
  10.         rn = ((rn * 10) + (x % 10)); //simple mathematics logic
  11.  
  12.     return rn; //returns the reversed number.
  13. }
  14.  
  15. int main()
  16. {
  17.     int T, a; //"T" is for TestCase, "a" is for number of attempts.
  18.     long long int P, rP; //"P" is the Number entered by user, "rP" is the reversed number of "P".
  19.  
  20.     cin >> T; //takes input of the Number of TestCases
  21.     while (T--)
  22.     {
  23.         cin >> P; //input of the original number entered by user
  24.         a = 1; //sets number of attempt to 1, as it's the first attempt
  25.         rP = rev(P); //calls the reverse function and sends the value of "P".
  26.                 //in return it gets the reversed number of "P" and assigns it to "rP"
  27.         while (1)
  28.         {
  29.             P = P + rP; //adds up the original number and the reversed number
  30.                     //and assigns the sum to "P"(the original Number)
  31.             rP = rev(P); //reverses the sum and assigns it to "rP"
  32.             if (P - rP == 0){ //checks if the sum ("P") is Palindrome
  33.                 cout << a << " " << P << endl; //(prints the number of attempts
  34.                                 //and the Palindrome Number (Current Value of "P"
  35.                 break; //if Palindrome, this breaks the loop as we don't need it anymore.
  36.             }
  37.             else
  38.                 a++; //if the value of "P" is NOT Palindrome, it increases the number of attempts
  39.                     //and continues the reverse and add process
  40.                 if(a>999) //if the number of attempts is NOT Less than 1000, this breaks the loop
  41.                     break;
  42.         }
  43.     }
  44.    
  45.     return 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement