Advertisement
MouseyN1

Operatii cu cifre (functii)

Sep 28th, 2012
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. int SumaCifre(int nr)
  4. {
  5.     int cifra, sCifre(0);
  6.     while(nr)
  7.     {
  8.         cifra = nr % 10;
  9.         sCifre += cifra;
  10.         nr /= 10;
  11.     }
  12.     return sCifre;
  13. }
  14.  
  15. int NrCifre(int nr)
  16. {
  17.     int contorCifre(0);
  18.     while(nr)
  19.     {
  20.         contorCifre++;
  21.         nr /= 10;
  22.     }
  23.     return contorCifre;
  24. }
  25.  
  26. int Invers(int nr)
  27. {
  28.     int inv(0), cifra;
  29.     while(nr)
  30.     {
  31.         cifra = nr % 10;
  32.         inv = inv * 10 + cifra;
  33.         nr /= 10;
  34.     }
  35.     return inv;
  36. }  
  37.  
  38. int Palindrom(int nr)
  39. {
  40.     int inv(0), cifra, aux;
  41.     aux = nr;
  42.     while(aux)
  43.     {
  44.         cifra = aux % 10;
  45.         inv = inv * 10 + cifra;
  46.         aux /= 10;
  47.     }
  48.     if(inv == nr)
  49.         return 1;
  50.     else return 0;
  51. }
  52.  
  53. int main()
  54. {
  55.     int a;
  56.     cin >> a;
  57.     cout << "Suma cifrelor este: " << SumaCifre(a) << endl;
  58.     cout << "Numarul de cifre: " << NrCifre(a) << endl;
  59.     cout << "Inversul numarului este: " << Invers(a) << endl;
  60.     if(Palindrom(a))
  61.         cout << "Numarul introdus este palindrom.";
  62.     else cout << "Numarul introdus nu este palindrom.";
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement