If sum of cubes of each digit of the number is equal to the number itself,then the number is called an Armstrong Numbers. For example
153= (1*1*1) + (5*5*5) + (3*3*3) is an Armstrong Number.
this is my code. I know its wrong. somehow I'm just managed to do it here. A million of gratitude if anyone could help me to point out my mistakes and give advices :)
A couple of suggestions:
1. Take Thomas's advice ( integers avoid rounding errors causing possible problems with == test )
2. Maybe have 3 prompts instead of one. eg "Please enter first number" then for second and third numbers. (Always say please)
3. Maybe have an else section with a message when the two numbers aren't equal.
Are you restricted to 3-digit numbers? The original problem statement didn't actually say that - it just used a 3-digit example.
Suggestion:
- prompt for input of a single integer (which you will need to store);
- loop round, picking off the digits one by one and adding their cubes to a total;
- compare your sum of cubes with the original stored number.
Edit: OK, an internet search refers to them as n-th powers of n-digit numbers. So, if you are after cubes then they will be 3 digits.
@Thomas1965 Okay. Thank for the advice.
@Kemort okay. Got it.
@lastchance yeah. at first I was thinking about single integer. by that case do I have to use array ?
or any way to teach me how to do by using a single integer ?
If you are restricting yourself to sum-of-cubes and 3-digit numbers then you don't need an array.
You can fill in the blanks below. All variables are of type int.
1 2 3 4 5 6 7 8 9 10 11 12
// other code to input variable 'number'
nwork = number; // nwork will change; number is the original
int sumCubes = 0; // initialise sum of cubes
while ( nwork > 0 )
{
digit = nwork % 10; // picks off the last digit (modulo operation)
nwork /= 10; // kicks off the last digit (integer divide by 10)
sumCubes += digit * digit * digit; // add to sum of cubes
}
// code to compare sumCubes with original number
@lastchance hey I have a doubt in your code.
when I replaced the line 9 in your code sumCubes += digit * digit * digit;
with sumCubes+= pow(digit,3);
it won't work.
@DatDankMeme
I'm not sure that using the pow() function for a low-order integer exponent is necessary. If you use pow() then the C++ header is <cmath> - I presume you could also use <math.h>, but I wasn't aware that there was one called <maths.h> with an 's'. Using pow() works fine (once you put the correct header in) but I didn't use it because (a) it isn't necessary; (b) it returns a double.
For cubes (and 3-digit numbers) you can use (with @Kemort's suggestion of 153 as a test):
#include <iostream>
#include <vector>
#include <iomanip>
usingnamespace std;
//====== Dealing with bigger numbers
// typedef int INT;
typedefunsignedlonglong INT;
// typedef size_t INT;
template <class T> T myPower( T base, int exponent ) // number (type T) raised to positive integer power
{
T value = 1;
while ( exponent-- > 0 ) value *= base;
return value;
}
//==================================
int main()
{
INT number, nwork;
vector<INT> digits;
cout << "Input a positive number: ";
cin >> number;
nwork = number;
while ( nwork > 0 )
{
digits.push_back( nwork % 10 );
nwork /= 10;
}
INT numDigits = digits.size();
INT sumPowers = 0;
for ( int i = 0; i < numDigits; i++ ) sumPowers += myPower( digits[i], numDigits );
cout << endl;
cout << "Original number: " << number << endl;
cout << "Number of digits (n): " << numDigits << endl;
cout << "Sum of n-th powers: " << sumPowers << endl;
cout << "Is an Armstrong (narcissistic) number? " << boolalpha << ( sumPowers == number ) << endl;
}
Input a positive number: 4338281769391371
Original number: 4338281769391371
Number of digits (n): 16
Sum of n-th powers: 4338281769391371
Is an Armstrong (narcissistic) number? true
@lastchance
I know that it is easier to use normal multiplication for such small problems and the function pow for bigger ones.
But isn't pow(digit,3);
the same thing as digit*digit*digit;
But isn't
pow(digit,3);
the same thing as
digit*digit*digit;
so why won't it work
It DOES work: try cpp-shell (the little gear-wheel icon at the upper right of your own posted code).
They are very nearly the same thing:
digit*digit*digit
will still be an int.
pow(digit,3) will be a double.
Unless there are major rounding errors (and there aren't) they will give the same answer. Your code works fine (on my system, and in cpp-shell).
No, it isn't the same.
There may be implementation differences between compilers, but it's possible to think of the internal working as something like: exp(log(digit) * 3)
I'm not saying that's how it actually works, but it is possible that some implementations may use floating-point calculations which can lead to approximations such as 7.9999 instead of 8 and so on, which could lead to discrepancies.
it does work in cpp.sh
but it won't work in my codeblocks IDE.
when I use pow it gives the sum as 152 for the original number 153.
I think @Chervil has given the correct explanation for this. If you use pow() then it will return a floating-point number (double, or possibly float). If, as in his example, the floating-point number happens (with floating-point rounding) to come to 7.9999 then, when assigned or added to an int, it will ROUND TOWARD ZERO (i.e. become 7, not 8)
If I have to use pow() when I'm EXPECTING AN INTEGER then I sometimes pre-empt any errors from rounding by doing something like sumCubes += ( pow(digit,3) + 0.5 );
to ensure that any whole-number rounding toward zero produces the correct whole number. Please, do NOT do this if the answer was expected to come back as a floating-point number as your answer would definitely be WRONG! (This is messy, and C++11 gives better and less opaque rounding functions.)
In general, be very wary of pow(). If it's a positive integer exponent then repeated multiplication is more reliable (and probably faster) where everything involved is an integer.
My code fragment below would ensure that powers are worked out by repeated multiplies rather than exp( log (..) ) for positive integer exponent.
1 2 3 4 5 6
template <class T> T myPower( T base, int exponent ) // number (type T) raised to positive integer power
{
T value = 1;
while ( exponent-- > 0 ) value *= base;
return value;
}