I need help

From the keyboard is introduced a string of natural numbers until the value 0.The problem asks to display all of the triples of consecutive introduced numbers(ex: 2 5 15) that have the following property:
(n2==n1/s && n2==n1%s)&&(n3==n1/s && n3==n1%s),where n1,n2,n3 are forming the triplet and s is the sum of the digits of n1.
In our example we have 1 triplet of numbers:
n1=2 n2=5 n3=15...s=1+5=6...15/6=2 and 15%6=5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    int x,y,z,n,s;
    std::cout<<"x=";std::cin>>x;
    std::cout<<"y=";std::cin>>y;
    while(y!=0)
    {
        std::cout<<"z=";std::cin>>z;
        n=x;
        s=0;
        while(n>0)
        {
            s=s+n%10;
            n=n/10;
        }
        if(x/s==y && x%s==z) std::cout<<x<<" "<<y<<" "<<z<<std::endl;
        x=y;
        y=z;
    }

Please do not use functions or advanced stuff.Keep it as simple as possible and give me as many explanations.
I am here just to note few things:
1. In your problem's definition it is said that s is the sum of the digits of n1, but in the given example s is the sum of the digits of n3?
In our example we have 1 triplet of numbers:
n1=2 n2=5 n3=15...s=1+5=6...15/6=2 and 15%6=5

2. Your if should be like this
if(y==x/s && y==x%s)&&(z==x/s && z==x%s), you missed y==x%s and z==x/s
3. How is 15%6=5? 15%6=3

So your code is OK when you enter x=15, y=2 and z=3 (without y==x%s and z==x/s) . Please read the problem again :)
Last edited on
Topic archived. No new replies allowed.