Suma cifrelor unui numar

Calculati suma cifrelor unui numar x citit de la tastatura

#include <iostream>

using namespace std;

int main()
{

int s;
long d,x;
cout<<“x=”;cin>>x;
d=x;
s=0;
while(d)
{
s=s+d%10;
d=d/10;
}
cout<<“s=”<<s;

return 0;
}

 

Leave a Comment

%d bloggers like this: