Hello I am trying to calcuate the square digit length of a number. I have written a code but I am getting an error with the "pow" function & I have no idea what is wrong?

Can you help me?

#include <iostream>
#include <cmath>
using namespace std;

int squared_digit_length(int n1);

int main()
{
	int n1= 0;

	cout << "Enter a number: ";
	cin >> n1;

	squared_digit_length(n1);

	return 0;
}

int squared_digit_length(int n1)
{
	int n2, co;
	while ((n2 != 1) || (n2 != 4))
	{
		co++;
	n2 = pow(n1,2);   // this is where I get my error "call of overloaded pow is ambiguous" ????
	}
	cout << n1 << "=" << n2 << "Count= " << co;

}

Also how would you reccommend I perform this task?
I have a number (85), and i want to pull it appart so I have 8 and 5 then I want to square each of those numbers (8^2 , 5^2) then add them (8^2 + 5^2).

I know how to do everything except separating the 85 into 8 and 5. Should I use a modulus calculation & make 85 an integer? If anyone could provide a solution in code (just the separating 85 part) I would be very greatful :)

Recommended Answers

All 6 Replies

I know how to do everything except separating the 85 into 8 and 5. Should I use a modulus calculation & make 85 an integer? If anyone could provide a solution in code (just the separating 85 part) I would be very greatful

Well that could be accomplished by just a combination of <number>%10 and <number>/10. (Of course the <number> should be an integer).

First you perform a mod by 10 on the number, that would give you the digit in the units place, next you divide the number by 10 and the quotient you again mod by 10 to get the number in the tens place, keep repeating it until the division by 10 gives a zero.

As far as your "pow()" call is concerned, the last time I used C++ and specially "pow()" in that, I recollect it needed the base to be either a "float" or a "double" while the exponent could be an "int".
Try casting your "n1" to either of these and check, Of course I could be wrong.

well to split the number is very easy task.
firtly you divide the number by 10 . and then myltiply the number by 10.
(you will not get the same number since you are using int data type)

int x = 85;
int y = x/10;              //here value of y is 8.
y *= 10;                // the value of y becomes 80
int z = x - y;            // value of z is 5 . which is at units place of original number

thus int y has the tens digit place and int z has units digit place.


after splitting the two numbers , pass them as parameter to ur function and you will get the desire result

>>firtly you divide the number by 10 . and then myltiply the number by 10.
It is lot better to use the modulus operator.

int N=85;
int Ones,Tens;
Ones=N%10;
Tens=N/10;

well both kinda do the same job . i thought it was better to approach from atomic level .

Oh man, this is really giving me trouble, thanks for the help so far tho.:)

Here's my code thats just not working ( sorry alot of the output is for debugging) My main areas of problem is the power function returning the right sum/answer.

What I am trying to do is,
- break a number (say 85) into 8 & 5
- square each of them (8^2 , 5^2 )
- Add them together to get the SUM ( 8^2 + 5^2 )
- Keep repeating steps 1 - 3 until the SUM = 1 or 4

#include <iostream>
#include <cmath>

using namespace std;

int squared_digit_length(int n1);

int main()
{
	int n1= 0;

	cout << "Enter a number: ";
	cin >> n1;

	cout << n1;
	squared_digit_length(n1);

	return 0;
}

int squared_digit_length(int n1)
{
	double n3= 0;
	int ones,tens;
	int co = 0;

	ones= n1%10;
	tens= n1/10;

	for(int i =0; n3 != 1 || n3 != 4; i++)
	{

		co++;
		n3 = ( pow((double)tens,2) + pow((double)ones,2));   
		cout << "n3 = " << n3 << " count = " << co << endl;
		ones= (int)n3%10;
		tens= (int)n3/10;
		cout << "Ones= " << ones << " Tens= " << tens << endl;
	}
	cout << "n3 =" << n3 << "Count= " << co;

}

You really need to break this down into simple parts and put them together.

This is not the optimized way of doing things but you have stated exactly what you have to do in simple steps:

What I am trying to do is,
- break a number (say 85) into 8 & 5
- square each of them (8^2 , 5^2 )
- Add them together to get the SUM ( 8^2 + 5^2 )
- Keep repeating steps 1 - 3 until the SUM = 1 or 4

Think of the first three steps as independent problems and solve them as such. Once you have those working, a little bit of gluing them together will give you the last step.

The last step is: get the result from the previous step. If it is 1 or 4, you are done. If not, feed the new number back into the previous step.

Getting the sum of the digits of a number was asked in this thread: http://www.daniweb.com/forums/thread186086.html

I adapted the solution I gave (http://www.daniweb.com/forums/post844074-28.html) with two small changes and I had the sum of the square of each digit. About five more lines were needed and I had the solution.

Once you have solved the problem, try to get the first three steps optimized in one function.

I say this all because you have two problems that probably wouldn't have happened if you took this plan of attack. The first is that your solution for getting the digits of a number fails when you have more than 2 digits. Second, your condition n3 != 1 || n3 != 4 has a subtle problem. Think about what happens when n3 equals 1 and then what happens when it equals 4. If you can solve these two issues you can ignore what I said above, but learning how to break a problem down into small sub-tasks is a very important skill to learn.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.