Nov 12, 2017 at 2:21pm UTC
please explain me why I need to use x1 here:
#include <iostream>
using namespace std;
int main()
{int x,x1;
int s,p,nr;
cout<<" x= ";cin>>x;
x1=x;
s=0;
p=1;
nr=0;
if(x==0)nr=1;
while(x1!=0)
{
s=s+x1%10;
p=p*(x1%10);
nr++;
x1=x1/10;
}
cout<<"Suma cifrelor lui "<<x<<" este "<<s<<endl;
cout<<"Produsul cifrelor lui "<<x<<" este "<<p<<endl;
cout<<"Numarul cifrelor lui "<<x<<" este "<<nr;
return 0;
}
Nov 12, 2017 at 2:49pm UTC
At this line, x1=x1/10;
the value of x1 is altered.
But you want to print out the value of x later. Therefore a copy of x is used instead of the original x.
Nov 12, 2017 at 3:10pm UTC
and there?
int x,x1;
cout<<" x =";cin>>x;
x1=0;
while(x!=0)
{
x1=x1*10+x%10;
x=x/10;
}
cout<<"Inversul numarului este "<<x1;
return 0;
}