i need help fast please

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;
}
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.
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;
}
and there?

You modify ”x” and “x1” in two differen ways, so you need two variables.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main()
{
    int x, x1;
    std::cout << " x = "; std::cin >> x;
    x1 = 0;
    while(x != 0)
    {
        x1 = x1 * 10 + x % 10;  // you modify x1 here...
        x = x / 10;             // ...and x here
    }
    std::cout << "Inversul numarului este " << x1;
    return 0;
}


Please, do *not* duplicate posts:
http://www.cplusplus.com/forum/beginner/223010/#msg1028167
Topic archived. No new replies allowed.