Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

c++: Kaprekar number test loop (list all Kaprekar numbers under 10,000) This is what I have so far, I can't seem to figure out where

c++: Kaprekar number test loop (list all Kaprekar numbers under 10,000)

This is what I have so far, I can't seem to figure out where it isn't working at...

#include

using namespace std;

int main(){

//declare variables

int num, squaredNum, firstHalf, secondHalf, unit, test;

//initialize number

num = 1;

//while loop

while(num < 10,000){

squaredNum = num * num;

if (squaredNum < 100){

unit = 10;

}

else if (squaredNum < 1000){

unit = 100;

}

else if (squaredNum < 10000){

unit = 1000;

}

else if (squaredNum < 100000){

unit = 10000;

}

firstHalf = squaredNum/unit;

secondHalf = squaredNum%unit;

test = firstHalf + secondHalf;

if (test == num){

cout << num << " is a kaprekar number!" << endl;

num++; //increment number

}else {

num++; //increment & continue to next number

}

}//end while loop

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions