what is wrong in my program

I have a homework which sais:

”Read integers from the keyboard until you enter the same number twice in succession. Read the numbers, to display the form (ABAC) (have four digits and the first and third number are the same) with the property that the sum of two numbers is a number of identical digits.
example, two such numbers are: 1217 (with figures sum 11) and 6861 (the sum of digits 22)”

now... this is what i've done so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//Problema 20
//Pagina 32

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
    unsigned int x,y,a,b,c,d,m,n,p,q,s;
    s=(11 || 22 || 33 || 44 || 55 || 66 || 77 || 88 || 99);
    cout<<"Give x:"; cin>>x;
    cout<<"Give y:"; cin>>y;
    while (x!=y)
    {
        if (x%10000==x)
        {
            a=x/1000;
            b=x/100%10;
            c=x/10%10;
            d=x%10;
            if ((a==c) && (a+b+c+d==s))
                cout<<x;
        }
        if (y%10000==y)
        {
            m=x/1000;
            n=x/100%10;
            p=x/10%10;
            q=x%10;
            if ((m==p) && (m+n+p+q==s))
                cout<<y;
        }
        x=y;
        cout<<"Give y:"; cin>>y;
    }
    getch();
}


is anything wrong ? please help me
here is a function to add up the digits in an integer:
1
2
3
4
5
6
7
8
int add_up_digits(int x){
	int y=0;
	while(x){
		y+=x%10;
		x/=10;
	}
	return y;
}

then check whether it is 11, 22, etc. by checking whether the remainder when dividing by 11 is 0.
Last edited on
yes.
mostly line 10 and the use of 's'.
also that you ask for two numbers from user.

basically your code should be this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int x = 0;
while(true){
   int temp;
   cin >> temp;
   if(temp == x) return 0;
   else x = temp;

   if(x < 1000 || x >= 10000) continue;//error. are there 4 digits?
   
   //do the splitting. it would be nice to use an array and a for loop here, but do whatever you like.
   if(/*first digit*/ != /*third digit*/) continue;//error
   s = /*the sum of the digits*/;
   if(s%10 != s/10) continue;//error
   cout << x;
}
i solve it by myself, but thx anyway. now, it looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//Problema 20
//Pagina 32

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
    unsigned int x,y,a,b,c,d,m,n,p,q,s,u,v;
    cout<<"Dati x:"; cin>>x;
    cout<<"Dati x:"; cin>>y;
    while (x!=y)
    {
        cout<<"\nINCEPUTUL BUCLEI !!!";
         if ((x-x%1000)/1000>0 && (x-x%1000)/1000<10)
        {
            cout<<"\nAM INTRAT IN PRIMUL IF";
            a=(x-x%1000)/1000; cout<<"\na="<<a;
            b=(x%1000)/100; cout<<"\nb="<<b;
            c=(x%100)/10; cout<<"\nc="<<c;
            d=(x%10); cout<<"\nd="<<d;
            s=a+b+c+d; cout<<"\ns="<<s;
            if (s%100==s)
            {
                cout<<"\nVERIFICAM DACA S ARE 2 CIFRE SI CE VALORI AU";
                u=s/10; cout<<"\nu="<<u;
                v=s%10; cout<<"\nv="<<v;
                if ((a==c) && (u==v))
                     cout<<"\nx="<<x;
            }
        }
        if ((y-y%1000)/1000>0 && (y-y%1000)/1000<10)
        {
            cout<<"\nAM INTRAT IN AL DOILEA IF";
            m=(y-y%1000)/1000; cout<<"\nm="<<m;
            n=(y%1000)/100; cout<<"\nn="<<n;
            p=(y%100)/10; cout<<"\np="<<p;
            q=(y%10); cout<<"\nq="<<q;
            s=m+n+p+q; cout<<"\ns="<<s;
            if (s%100==s)
            {
                cout<<"\nVERIFICAM DACA S ARE 2 CIFRE SI CE VALORI AU";
                u=s/10; cout<<"\nu="<<u;
                v=s%10; cout<<"\nv="<<v;
                if ((m==p) && (u==v))
                     cout<<"\ny="<<y;
            }
        }
        x=y; cout<<"\nLa sf. buclei, avem x="<<x;
        cout<<"\nDati x:"; cin>>y;
        cout<<"\nSFARSITUL BUCLEI !!!";
    }
    getch();
}


PS: im in the 9th grade at school, and is my 1st year of studying informatic. (especially C/C++)
Last edited on
Topic archived. No new replies allowed.