Stumpped.. Need help

I need this program to display the kaperkar numbers from 0-9999. I can not get it to display anything other than 1. Where am I going wrong? Also, we can't use Boolean variables in this either.

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
  #include <iostream>
using namespace std;

int main()

{

	int num = 0;
	int unit=0;
	int numSquare=(num*num);
	int firstHalf;
	int secondHalf;
	int result=0;

		cout<< "The Following are Kaperkar Numbers from 0 to 10000"<<endl;
		

		for (num>0;num<10000;num++)
		
		{		

		if (numSquare < 10)
		{
		unit=1;
		firstHalf = numSquare/unit;
		secondHalf = numSquare%unit;
		result = firstHalf + secondHalf;	
		}

		else

		{

			if (numSquare < 100)
			{
			unit=10;
			firstHalf = numSquare/unit;
			secondHalf = numSquare%unit;
			result = firstHalf + secondHalf;			
			}

			else

			{

				if (numSquare < 1000)
				{
				unit=100;
				firstHalf = numSquare/unit;
				secondHalf = numSquare%unit;
				result = firstHalf + secondHalf;					
				}

				else

				{
					if (numSquare < 10000)
					{
					unit=1000;
					firstHalf = numSquare/unit;
					secondHalf = numSquare%unit;
					result = firstHalf + secondHalf;								
					}

					else

					{
						if (numSquare < 100000)
						{
						unit=10000;
						firstHalf = numSquare/unit;
						secondHalf = numSquare%unit;	
						result = firstHalf + secondHalf;										
						}

						else

						{
							if (numSquare < 1000000 )
							{
							unit=100000;
							firstHalf = numSquare/unit;
							secondHalf = numSquare%unit;
							result = firstHalf + secondHalf;												
							}

							else

							{
								if (numSquare < 10000000 )
								{
								unit=1000000;
								firstHalf = numSquare/unit;
								secondHalf = numSquare%unit;		
								result = firstHalf + secondHalf;														
								}

								else

								{
									if (numSquare < 100000000)
									{
									unit=10000000;
									firstHalf = numSquare/unit;
									secondHalf = numSquare%unit;
									result = firstHalf + secondHalf;															
									}

								}								
							}
						}
					}
				}
			}
		}



}

do
{
	cout<<num<<endl;
}
while (num==result);




return 0;

}
Last edited on
numsquare is zero in your code. Always.
for (num>0
this looks incorrect.
it is extremely likely you meant num = 0.
1
2
3
4
5
do
{
	cout<<num<<endl;
}
while (num==result);

This just makes no sense at all. It will either write out once, or be an infinite loop, writing out the same thing forever and ever.
ok so if I change the do..while to:

if (num==result);
cout<<num<<endl;

and set num=0; instead of num>0; in the for loop, it still only prints 10000.

where am I going wrong with this? I have been working on this since yesterday and can't figure out where I am going wrong.
debugging 101.

1) add print statements where the problem is.
2) locate problem
3) fix problem
4) does the program work? No-> goto 1, Yes, goto 5
5) done!

did you move numsquared = num*num into the loops?


¿how can you do 1 before 2? ¿why don't recommend to simply use a debugger? ¿why use goto instead of a simple loop?


> it still only prints 10000.
¿why are you surprised?
1
2
3
4
5
6
7
8
for (num=0;num<10000;num++){
   //code that doesn't print
}
//outside the loop, her num is 10000

if (num==result)
   ; //<-- empty body for the conditional
cout<<num<<endl; //allways execute 
bother yourself with a diagram flow or a pseudocode.
you never compute the square and you only split at half.
Stuff like this I usually map out in a scripting language of choice. Not sure how to change syntax highlighting to a different lang here, but below you'll find some working code in Ruby.
1. Convert square to a string (e.g. std::to_string )
2. Confirm your method for incrementally splitting is working ( 2025: "2" and "025", "20" and "25", "202" and "5")
3. Convert each part back to int
4. Check for edge cases and early break-outs (per the definition, "1" is apparently valid, and when split, second part can't be zero)

Of course you can also try slicing up the number with integer division and modulus as you did, but try to logically merge it in a loop so that it's incremental. e.g. for(int unit=10; unit<square; unit*=10) or so

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
#!/usr/bin/env ruby
$VERBOSE=true

# In mathematics, a non-negative integer is called a "Kaprekar number" for a given base if the 
# representation of its square in that base can be split into two parts that add up to the original 
# number, with the proviso that the part formed from the low-order digits of the square must be 
# non-zero—although it is allowed to include leading zeroes. For instance, 
# 45 is a Kaprekar number, because 452 = 2025 and 20 + 25 = 45. The number 1 is Kaprekar in every base, because 12 = 01   in any base, and   0 + 1 = 1.

# Find all Kaprekar from 0 to 9999


# A function to test for Kaprekar. Requires n's square to be at least two digits.
def is_kap? n
  s = (n*n).to_s

  # Try different 2-part string splits and test sum
  found = false
  1.upto(s.size-1) {|i|
    one,two = s[0,i].to_i, s[i..-1].to_i
    
    if one>n || two==0
      break
    end  
    
    if one+two == n
      found=true
      break 
   end  
  }
  found
end

# 1 is also strangely Kaprekar because apparently its square can be represented "01" and split "0"+"1"  #logic
arr = [1]
(4..9999).each {|n|
  arr<<n if is_kap?(n)
}
p arr

# [1, 9, 45, 55, 99, 297, 703, 999, 2223, 2728, 4879, 4950, 5050, 5292, 7272, 7777, 9999]
Topic archived. No new replies allowed.