CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. Junior Member Join Date
Apr 2009
Posts
5

## Squared Digit Length

I'm a beginner at C++ and I seem to be having problems with loops and I can't tell where I'm going wrong.

I need to find the squared digit length of an integer. This means taking an input integer and add up the squares of its digits. This results in another integer. I have to repeat the loop until the result becomes either 1 or 4.

For example if I enter a value of 85 the following would be the calculations:

8&#178; + 5&#178; = 89
8&#178; + 9&#178; = 145
1&#178; + 4&#178; + 5&#178; = 42
4&#178; + 2&#178; = 20
2&#178; + 0&#178; = 4

Therefore the squared digit length of 85 is 5 because it took 5 steps before the result became either 1 or 4.

So far I've figured out the code for splitting the digits.

int squared_digit(int n)
{
int result = 0;
while (n > 0)
{
int LD = n&#37;10;
result = result + (LD*LD);
n = n/10;
}
return result;
}

However, I don't know how to loop the result. So if I type in 85 I only get:

8&#178; + 5&#178; = 89

But the rest of the calculations won't loop.
8&#178; + 9&#178; = 145
1&#178; + 4&#178; + 5&#178; = 42
4&#178; + 2&#178; = 20
2&#178; + 0&#178; = 4  Reply With Quote

2. Senior Member Join Date
Apr 2007
Location
Mars NASA Station
Posts
1,436

## Re: Squared Digit Length

I not really understand your problem.  Reply With Quote

3. Junior Member Join Date
Apr 2009
Posts
5

## Re: Squared Digit Length

I don't know how to explain it any better....

Basically I type in a number

for example 85

stage 1. Split the integer into 8 and 5
stage 2. Then square the digits and add them together (8^2 = 64) + (5^2=25) = result
stage 3. Repeat stages 1 and 2 to the result

I keep repeating stages 1 and 2 until the result becomes 1.

My problem is I can't get it to repeat stages 1 and 2.  Reply With Quote

4. Senior Member Join Date
Apr 2007
Location
Mars NASA Station
Posts
1,436

## Re: Squared Digit Length

The condition is

Code:
```while ( result != 1 && result != 4)
{
// Split the number into and square.
}```
Hope this help  Reply With Quote

5. Senior Member Join Date
Apr 2007
Location
Mars NASA Station
Posts
1,436

## Re: Squared Digit Length

Do you want arbitrary length or fixed length of number ?

Your loop stop due to 8&#178; + 9&#178; = 145 arbitrary length  Reply With Quote

6. Senior Member Join Date
Apr 2007
Location
Mars NASA Station
Posts
1,436

## Re: Squared Digit Length

Here it is.

Code:
```#include <iostream>
#include <vector>

using namespace std;
// ========================================

// ========================================

typedef unsigned long ulong;
void SplitAndSquare(ulong&);
void Split(const ulong&, vector<int>&);

// ========================================
int main()
{
ulong digit(85);

SplitAndSquare(digit);

return 0;
}
// ========================================
void SplitAndSquare(ulong& number)
{
ulong result(number);
vector<int> singleDigit;
int step(0);
while (result != 1 && result != 4)
{
Split(result, singleDigit);
result = 0;
for (size_t loop = 0;loop<singleDigit.size();++loop)
{
result += singleDigit[loop] * singleDigit[loop];
}

++step;
}
cout << "Length of square digit is " << step;
}
// ========================================
void Split(const ulong& number, vector<int>& singleDigit)
{
singleDigit.clear();
ulong modifier(number);
while (modifier)
{
singleDigit.push_back(modifier &#37; 10);
modifier /= 10;
}
}
// ========================================

// ========================================

// ========================================

// ========================================```  Reply With Quote

7. Junior Member Join Date
Apr 2009
Posts
5

## Re: Squared Digit Length

The length I mentioned means the number of times I have to split and square the initial value before I get either 1 or 4.

initial value = 85

1. 8² + 5² = 89 (Square Digit Length 1)
2. 8² + 9² = 145 (Square Digit Length 2)
3. 1² + 4² + 5² = 42 (Square Digit Length 3)
4. 4² + 2² = 20 (Square Digit Length 4)
5. 2² + 0² = 4 (Square Digit Length 5)

Function A:
int squared_digit(int n)
{
int result = 0;
while (n > 0)
{
int LD = n%10;
result = result + (LD*LD);
n = n/10;
}
return result;
}

That is to say it takes 5 loops of Function A, before 85 becomes 4.

5 loops = 5 Square Digit Length.

Does that help clarify anything?  Reply With Quote

8. Junior Member Join Date
Apr 2009
Posts
5

## Re: Squared Digit Length Originally Posted by Peter_APIIT Here it is.

Code:
```#include <iostream>
#include <vector>

using namespace std;
// ========================================

// ========================================

typedef unsigned long ulong;
void SplitAndSquare(ulong&);
void Split(const ulong&, vector<int>&);

// ========================================
int main()
{
ulong digit(85);

SplitAndSquare(digit);

return 0;
}
// ========================================
void SplitAndSquare(ulong& number)
{
ulong result(number);
vector<int> singleDigit;
int step(0);
while (result != 1 && result != 4)
{
Split(result, singleDigit);
result = 0;
for (size_t loop = 0;loop<singleDigit.size();++loop)
{
result += singleDigit[loop] * singleDigit[loop];
}

++step;
}
cout << "Length of square digit is " << step;
}
// ========================================
void Split(const ulong& number, vector<int>& singleDigit)
{
singleDigit.clear();
ulong modifier(number);
while (modifier)
{
singleDigit.push_back(modifier % 10);
modifier /= 10;
}
}
// ========================================

// ========================================

// ========================================

// ========================================```
I don't understand this, I haven't learnt C++ of this level yet.  Reply With Quote

9. Junior Member Join Date
Apr 2009
Posts
5

## Re: Squared Digit Length Originally Posted by Peter_APIIT Do you want arbitrary length or fixed length of number ?

Your loop stop due to 8² + 9² = 145 arbitrary length
I don't understand the concept of arbitrary length. I haven't learnt this yet.  Reply With Quote

10. ## Re: Squared Digit Length

You need nested loops here.

Set your result initially to your n. Then have an outer loop using while(result/10 > 0). The inner loop should do the brunt of the math using % and / starting with the least digit, then working until you have zero left-over.

Peter's solution is set-up like a long integer library (that is to say, an integer library for anything that needs to be greater than your standard 32- or 64-bit datatype). If your n value stays within your standard int type, his solution is way more complex than you need.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
• 