Armstrong Number Program

I was given this assignment. I'm not sure how to do it, much less how to start, other than using namespace std. Any direction will be greatly appreciated.

An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 153 is an Armstrong number
since 1^3 + 5^3 + 3^3 = 153. Write a function named is_armstrong that takes one positive integer number as argument and returns a boolean variable which is true if the number is an Armstrong number and return false otherwise. Use this function is_armstrong in your main( ) function to print all Armstrong number in the range of 100 and 999.

You will need to first accept numbers as strings, then separate each digit, convert each individual digit to an integer, raise them to the power, add, etc they meet the condition, you got it.

You will have to use the define <string> and maybe define <cmath> this last one may not be necessary.

That is the basic outline.

Good luck.
Do what you know, bit by bit.

You do know that you need function main().
You do know that main() calls is_armstong().
You do know the name, return type, and arguments of is_armstong().
You should know that you have to separate the digits of the input number.
You should know operators +, *, /, % and ==.
Hi created a program for you. It will produce tbe Armstrong numbers between 100 and 999. However you will have to create a boolean function. Most of the hard work is done. By the way, I managed to separate an integer into its components by dividing. With strings it becomes unnecessarily complicated. It is self-explanatory (I hope) any questions do not hesitate to ask.

#include <iostream>


using namespace std;



// function to calculate each digit cubed and the the sum of the results
// we are sending in the current number from the loop starting at 100 and
// counting one by one to 999

int getRaiseAndAdd(int &numberInput)
{
int firstNumber, secondNumber, thirdNumber;
int remainder;
int sum;
int firstPower, secondPower, thirdPower;


firstNumber = numberInput / 100; // this first division gets the hundreds
remainder = numberInput % 100; // this gets the remainder of previous division
secondNumber = remainder / 10; // divides remainder by 10 to get second digit
thirdNumber = remainder % 10; // this gives the remainder of previous division
// to get the units

// code to add up the three separate components of original number

firstPower = firstNumber * firstNumber * firstNumber;
secondPower = secondNumber *secondNumber * secondNumber;
thirdPower = thirdNumber * thirdNumber * thirdNumber;


// returns result of adding the three numbers

return sum = firstPower + secondPower + thirdPower;
}

int main()
{
int answer;
int originalNumber;

for(int i = 100; i < 1000; i++)
{
originalNumber = i; // you will need to store the original number as the loop will
// change the numbers it adds as it goes from 100 to 999

// calls function to cube the individual digits of the original number and
// add them up. Returns result to a variable called answer.

answer = getRaiseAndAdd(i);

// just make this code into a boolean function called is_Armstrong
// this code will give you the Armstrong numbers you seek



// tests the answer to see if it matches the original number, if it does,
// prints out the message it found an Armstrong number

if(answer == originalNumber) // compares result of sum with original number
{
cout << "found an Armstrong number " << originalNumber << endl;
}
}

return 0;
}




Last edited on
@Glyndon:
Please use code tags when posting code. See http://www.cplusplus.com/articles/jEywvCM9/
Make sure that indentation is intuitive/systematic.
Thanks very much!

I have almost got the code working, but I have run into an error:

prog4a.cpp: In function ‘int main()’:
prog4a.cpp:42:25: error: name lookup of ‘i’ changed for ISO ‘for’ scoping [-fpermissive]
answer = getRaiseAndAdd(i);
^
prog4a.cpp:42:25: note: (if you use ‘-fpermissive’ G++ will accept your code)


I tried Googling the -fpermissive flag, but could not find any help on how to prevent it from showing as an error. Any suggestions?

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
49
50
51
52
#include <iostream>

using namespace std;

//Function name: getRaiseAndAdd
//Purpose: Calculate each digit cubed and the sum of the results.
//We are sending in the current number from the loop starting at 100 and
//counting one by one to 999.
//Parameters: &numberInput
//Return value: Result of adding 3 numbers
int getRaiseAndAdd(int &numberInput)
{
int firstNumber, secondNumber, thirdNumber;
int remainder;
int sum;
int firstPower, secondPower, thirdPower;

firstNumber = numberInput / 100;
remainder = numberInput % 100;
secondNumber = remainder / 10;
thirdNumber = remainder % 10;

firstPower = firstNumber * firstNumber * firstNumber;
secondPower = secondNumber *secondNumber * secondNumber;
thirdPower = thirdNumber * thirdNumber * thirdNumber;

return sum = firstPower + secondPower + thirdPower;
}

int main()
{
int answer;
int originalNumber;

for(int i = 100; i < 1000; i++)
originalNumber = i;

answer = getRaiseAndAdd(i);
        {
        //Function name: is_Armstrong
        //Purpose: finding the Armstrong numbers
        //Parameters: answer
        //Return value: 0
        bool is_Armstrong (int answer);
        if(answer == originalNumber);
                {
                cout << "found an Armstrong number " << originalNumber << endl;
                }
        }

return 0;
}
Topic archived. No new replies allowed.