Advertisement
Five_NT

[C++]Cel mai apropiat vecin al unui nr.

Nov 7th, 2013
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. int n, j, s, nrs, nrd, i, x;
  6.  
  7. void citire();
  8. int suma(int n);
  9.  
  10. int main()
  11. {
  12.     citire();
  13.     int ok=0;
  14.     x=suma(n);
  15.     i=n+1;
  16.     while(suma(i)!=x)
  17.     {
  18.         i++;
  19.         nrd=i;
  20.     }
  21.     cout<<"Cel mai apropiat numar din dreapta:"<<" "<<nrd<<'\n';
  22.     j=n-1;
  23.     while(suma(j)!=x)
  24.     {
  25.         j--;
  26.         if(j==1)
  27.         {
  28.             ok=1;
  29.             break;
  30.         }
  31.     }
  32.     if(ok==0) nrs=j;
  33.     else cout<<"Nu exista!";
  34.     if(ok==0)
  35.     {
  36.         if(nrd-n < n-nrs)
  37.             cout<<nrd;
  38.         else if(nrd-n==n-nrs)
  39.                 cout<<nrd<<" "<<nrs;
  40.             else cout<<"Cel mai apropiat numar din stanga:"<<" "<<nrs<<'\n';
  41.     }
  42.     return 0;
  43. }
  44. void citire()
  45. {
  46.     cout<<"Intoruceti nr.: ";
  47.     cin>>n;
  48. }
  49. int suma(int n)
  50. {   s=0;
  51.     while(n)
  52.     {
  53.         s=s+n%10;
  54.         n/=10;
  55.     }
  56.     return s;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement