Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. const int ILLEGAL_INPUT = -1;
  4. int MixNewNum(int num);
  5. void main()
  6. {
  7.     int num;
  8.     int res;
  9.     cout << "Please enter a natural numbers: ";
  10.     cin >> num;
  11.     res = MixNewNum(num);
  12.     if (res != ILLEGAL_INPUT)
  13.         cout << "The new number is : " << res << endl;
  14.     else
  15.         cout << "Eror!!The input is illegal! "<< endl;
  16. }
  17. int MixNewNum(int num)
  18. {
  19.     if (num <= 0)
  20.         return ILLEGAL_INPUT;
  21.     int res;
  22.     int digit1, digit2;
  23.     int temp_num;
  24.     int mult;
  25.     mult = 1;
  26.     res = 0;
  27.     if (num > 9)
  28.     {
  29.         while (num > 9)
  30.         {
  31.             digit1 = num % 10;
  32.             num /= 10;
  33.             digit2 = num % 10;
  34.             num /= 10;
  35.             temp_num = (digit1 * 10) + (digit2);
  36.             res = res + (temp_num*mult);
  37.             mult *= 100;
  38.         }
  39.        
  40.         res = res + (num*mult);
  41.  
  42.     }
  43.     else
  44.     {
  45.         res = num;
  46.     }
  47.     return res;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement