Armstrong number

I'm currently learning C++ for a class and we have the following assignment. I had my program working perfectly IF the user had to chose a number, but when i realized I had to use a for loop and just display all of them, it outputted 3 weird numbers, but I didnt change anything with the math. Any help would be greatly appreciated. This is the 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.

Here is my program that displays the incorrect numbers;
#include <iostream>
#include <stdio.h>
using namespace std;

bool is_armstrong(int);
int main(){

for(int number=100;number<=999;number++)

if(is_armstrong(number)==true){
cout<<number<<" is an Armstrong number"<<endl;
}
return 0;
}

bool is_armstrong(int number){

while(number!=0){
int temp,sum=0,remainder=0;
temp=number;
remainder=number%10;
number=number/10;
sum=sum+(remainder*remainder*remainder);

bool status;
if(sum==temp){
status=true;
}
else{
status=false;
}
return status;
}
}
But this is the program that I wrote before, and it does work when I input my own number;
#include <iostream>
#include <stdio.h>
using namespace std;

bool is_armstrong(int);

int main(){

int number;
cout<<"Input an integer"<<endl;
cin>>number;

if(is_armstrong(number)){
cout<<"True, this is an Armstrong number"<<endl;
}
else{
cout<<"False, this is not an Armstrong number"<<endl;
}
return 0;
}
bool is_armstrong(int number){
int temp,sum=0,remainder=0;
temp=number;

while(number!=0){
remainder=number%10;
number=number/10;
sum=sum+(remainder*remainder*remainder);
}
bool status;
if(sum==temp){
status=true;
}
else{
status=false;
}
return status;
}
Hi I wrote a program you can see in a previous post in response to the same problem posted by GetMeOuttaHere www.cplusplus.com/forum/beginner/186405/. Hope it helps.
Should work. Just small changes. Compare them. By the way, the "for" loop requires "{" and "}".


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
#include <iostream>
#include <stdio.h>
using namespace std;

bool is_armstrong(int);
     int main(){
    
    for(int number=100;number<=999;number++){
        if(is_armstrong(number)==true){
            cout<<number<<" is an Armstrong number"<<endl;
        }
    }
return 0;
}

bool is_armstrong(int number){
    int temp,sum=0,remainder=0;
    temp=number;
    while(number!=0){
        remainder=number%10;
        number=number/10;
        sum=sum+(remainder*remainder*remainder);
    }

    bool status;
    if(sum==temp)
    status=true;

    else
    status=false;

    return status;

}
Thank you Glyndon, that worked perfectly!
Topic archived. No new replies allowed.