👤

Sa se genereze toate numerele din 4 cifre care sunt numere palindrom si care au suma cifrelor egala cu un numar S citit de la tastatura, algoritm in c++. VA ROG, URGENT!!! DAU COROANA, INIMI SI MA ABONEZ + TOATE PUNCTELE MELE!!!!

Răspuns :

Răspuns:

#include <iostream>

using namespace std;

int sum_cif(int x)

{

   int s=0;

   while(x)

   {

       s=s+x%10;

       x/=10;

   }

   return s;

}

int palindrom(int x)

{

   int cp=x, inv=0;

   while(x)

   {

       inv=inv*10+x%10;

       x=x/10;

   }

   if(cp==inv)

       return 1;

   else

       return 0;

}

int main()

{

   int s=0, i;

   cin>>s;

   for(i=1001; i<=9999; i++)

       if(palindrom(i)==1 && sum_cif(i)==s)

           cout<<i<<" ";

   return 0;

}

Explicație: