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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132`` `````` #include 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"<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<
Last edited on
numsquare is zero in your code. Always.
for (num>0
this looks incorrect.
it is extremely likely you meant num = 0.
 ``12345`` ``````do { cout<

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?
 ``12345678`` ``````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<
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

 ``123456789101112131415161718192021222324252627282930313233343536373839404142`` ``````#!/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<
Topic archived. No new replies allowed.