Armstong Numbers

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 #include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	double a,b,c;
	double calculation=0;

	cout<<"Enter Number : ";
	cin>>a>>b>>c;


	calculation = (a*a*a) + (b*b*b) + (c*c*c);

	if(calculation==//something***)
	{
		cout<<calculation<<"is an Amstrong number"<<endl;
	}
	system("Pause");
	return 0;
}


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 :)
Last edited on
closed account (48T7M4Gy)
Consider this: if the 3 numbers are a, b and c then a^3 + b^3 + c^3 = 100a + 10b + c
@kemort Thanks for the reply once again.

is it something look like this ?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	double a,b,c;
	double calculation1=0;
	double calculation2=0;

	cout<<"Enter Number : ";
	cin>>a>>b>>c;

	calculation1 =  (100*a) + (10*b) + c ;
	calculation2 = (a*a*a) + (b*b*b) + (c*c*c);

	if(calculation2==calculation1)
	{
		cout<<calculation1<<" is an Armstrong number"<<endl;
	}
	system("Pause");
	return 0;
}
Last edited on
You need to use integers not doubles.
closed account (48T7M4Gy)
@DesmondLee

It worked with 1, 5 and 3

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.
@DesmondLee

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.
Last edited on
@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 ?
Hello @DesmondLee

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 



Last edited on
closed account (48T7M4Gy)
The brute force way:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

int main()
{
    int lhs = 0;
    int rhs = 0;
    
    for(int a= 0; a < 10; a++)
    {
        for(int b = 0; b < 10; b++)
        {
            for( int c= 0; c < 10; c++)
            {
                lhs = a*a*a + b*b*b + c*c*c;
                rhs = 100*a + 10*b + c;
                
                if (rhs > 99 && lhs == rhs)
                    std::cout << a << b << c << " is an Armstrong number\n";
            }
        }
    }
    
    return 0;
}
@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.

I did include the <maths.h> header file
@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):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <iomanip>
using namespace std;


int main()
{
   int number, nwork;
   int digit;

   cout << "Input a positive number: ";   cin >> number;

   int sumCubes = 0;
   nwork = number;
   while ( nwork > 0 )
   {
      digit = nwork % 10;
      nwork /= 10;
      sumCubes += digit * digit * digit;
   }

   cout << "\nOriginal number:  " << number;
   cout << "\nSum of cubes:     " << sumCubes;
   cout << "\nIs an Armstrong (narcissistic) number? " << boolalpha << ( sumCubes == number );
}



If you want higher-digit numbers (and, hence, higher powers) then it is more complex in a lot of ways, but you can use something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;


//====== Dealing with bigger numbers

// typedef int INT;
   typedef unsigned long long 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


See: http://mathworld.wolfram.com/NarcissisticNumber.html
Last edited on
@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;

so why won't it work if we use the function in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <iomanip>
#include<cmath>
using namespace std;


int main()
{
   int number, nwork;
   int digit;

   cout << "Input a positive number: ";   cin >> number;

   int sumCubes = 0;
   nwork = number;
   while ( nwork > 0 )
   {
      digit = nwork % 10;
      nwork /= 10;
      sumCubes += pow(digit,3);
   }

   cout << "\nOriginal number:  " << number;
   cout << "\nSum of cubes:     " << sumCubes;
   cout << "\nIs an Armstrong (narcissistic) number? " << boolalpha << ( sumCubes == number );
}


Sorry if this is dumb but I am new to C++ and trying to experiment as much as possible.
Last edited on
1
2
3
4
5
6
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).

But isn't
 
pow(digit,3);

the same thing as
 
digit*digit*digit;

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.

Last edited on
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;
}

Last edited on
@lastchance and @Chervil
thanks a lot!
Topic archived. No new replies allowed.