Guest User

Untitled

a guest
Jun 18th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. int main()
  5. {
  6.     string x,y;
  7.     freopen("myDearInput.in", "r", stdin);
  8.     while(cin>>x>>y && x!="0" && y!="0")
  9.     {
  10.  
  11.         if ( x.size() < y.size())
  12.         {
  13.             for (int b=0 ; b <=(y.size()-x.size()) ;b++ )
  14.                 x.insert(x.begin(), '0');
  15.         }
  16.  
  17.  
  18.         else if ( x.size() > y.size())
  19.         {
  20.             for (int b=0 ; b <=(x.size()-y.size()) ;b++ )
  21.                 y.insert(y.begin(), '0');
  22.         }
  23.         string  h=""; int carry=0 ,s=0 ,a=0;
  24.  
  25.         for(int i=(x.size()-1) ;i>=0 ;i--)
  26.         {
  27.  
  28.             int f=x[i]-48 , j=y[i]-48 ;
  29.             s=f+j+carry;
  30.             if (s>9)
  31.             {
  32.                 h.insert(h.begin() ,((s-10)+48));
  33.                 carry=1;
  34.                 a++;
  35.                 if (i==0)
  36.                     h.insert(h.begin() ,((s/10)+48));
  37.  
  38.             }
  39.             else
  40.             {
  41.                 h.insert(h.begin() ,(s+48));
  42.                 carry=0;
  43.             }
  44.  
  45.         }
  46.  
  47.         if (a==0)
  48.             cout<<"No carry operation.";
  49.         else if (a==1)
  50.             cout<<"1 carry operation.";
  51.         else
  52.             cout<<a<<" carry operations.";
  53.         cout<<endl;
  54.     }
  55.     return 0;
  56. }
Add Comment
Please, Sign In to add comment