Dudeney Number problem

hello everyone :)
iam new here and i wish to learn from the pro programmers over here

iam having a trouble to solve this problem actually : Write a program that check is the entered integer number is Dudeney Number( A Dudeney number is a positive integer equals to the cube of its digits-total. For example 5231 is not a Dudeney number since (5+2+3+1)3 = 113 = 1331 ≠ 5231, while 4913 is a Dudeney number since (4+9+1+3)3 = 173 = 4913.)

.Sample input / output:

Enter a positive integer : 19683
19683 is a Dudeney Number

Enter a positive integer : 87213
87213 is not a Dudeney Number

thanks in advance
Someone else may know of a better way, but this is what I first thought to do:
Read the number as an integer.
Convert it to a string (keep the int, too).
Get it's length.
Iterate through the string, saving each character as an integer.
Sum the integers.
Cube the sum.
Check if the cubed sum is equal to the original int.
thank you so much for your replay i appreciate it

but actually i just started to learn c++

could u give me hint about what u meant by (Convert it to a string (keep the int, too).
Get it's length.
Iterate through the string, saving each character as an integer.)

thnks
C++ strings are an abstract data type (ADT) that you can use by include the string header with #include <string> at the top of your code. They can be used to hold lines of text.

1
2
3
4
5
6
7
8
9
10
11
12
int main ( )
{
	string str = "Hello World"; // initialize a string to equal some text

	int length = str.size ( ); // 'length' now equals the length of the string, 11

	int sum = 0;

	for ( int i = 0; i < length; ++i ) // iterate through str
		sum += atoi ( &str[i] ); // convert each char in str to an int and add it to sum
	// if the character is not a number, it will be treated as 0
}


I'm sorry if this still isn't that helpful. Try writing something and post it here and I'll be able to help you more.
thanks again for your help

that was kinda helpful but i still didn't get the idea . how to apply strings iny my problem
i will give it my best learn more about strings after that i will post what i have done so far as soon as possible

thank u so much :)

Topic archived. No new replies allowed.