xmaddiemuffin
xmaddiemuffin

Reputation: 49

Why is only one Armstrong number being printed?

This is my assignment:

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

I have written a program, but it only prints out one number, 999. Can anyone point out my error? Many thanks.

#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;
}

Upvotes: 1

Views: 137

Answers (4)

AOquaish
AOquaish

Reputation: 53

As Kevin stated: putting the Check within the loop would solve your problem

for(int i = 100; i < 1000; i++)
    {
    originalNumber = i;
    answer = getRaiseAndAdd(i);
    if(answer == originalNumber)
            {
            cout<<"found an Armstrong number "<<originalNumber<<endl;
            }
    }

Ran it and got the result

result

Upvotes: 0

johannes
johannes

Reputation: 15989

First you are running this loop:

for(int i = 100; i < 1000; i++)
    {
    originalNumber = i;
    answer = getRaiseAndAdd(i);
    }

after this originalNumber will be 999 and answer will be 999, too.

Only later you are running this check:

    if(answer == originalNumber);
            {
            cout<<"found an Armstrong number "<<originalNumber<<endl;
            }
    }

You should make the check a proper function, you have a forward declaration, followed by a block, and call it from within the loop.

Upvotes: 1

Kevin
Kevin

Reputation: 7324

There are a few things wrong with your code (your armstrong function seems fine though). First, your code to check if the number is an armstrong number is done after you loop through everything. This needs to be done inside the loop so that every number is checked.

Second, you have a semicolon after your if (if (answer == originalNumber); {...}). This causes execution to fall into the next block unconditionally, which is why it prints out 999 (which is not an armstrong number by the way).

So the fixes are to move your check into the loop and remove the semicolon after the if.

Upvotes: 3

sam
sam

Reputation: 48

Starting from beginning of the main method to the end, you are printing one time. If you repeat your printing in a loop you will get multiple prints.

Upvotes: 0

Related Questions