Click here to Skip to main content
15,905,785 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
An Armstrong number is a n digit number, which is equal to the sum of the nth powers of its digits.
So, i have to print Armstrong numbers between 1 and 500. there are 12 of them in total between the range.

the program i have written, it actually was not printing Armstrong numbers over 9. so i decided to test it by printing the numbers that i get from the 'sum' mentioned above.
this is how i found out that the loop was working above 149. Whenever it reaches 150, the program somehow subtracts 1 from the 'sum'. PLEASE HELP AS I HAVE TO SUBMIT IT IN A COUPLE OF DAYS.

What I have tried:

C++
#include <iostream>
#include <math.h>

using namespace std;

int main()
{
    int di;
    for(int a = 1, b = a, c = a, sum = 0, digi = 0;a <= 150;a++, b = a, digi = 0, c = a, sum = 0)
    {
        for(b;b != 0;b /= 10, digi++);
        do
        {
            di = c % 10;
            sum += pow(di, digi);
            c /= 10;
        }while(c != 0);
        cout << sum << " ";
        // if(sum == a) cout << sc << " ";
    }
}
Posted
Updated 19-Aug-17 1:23am
v2
Comments
Richard MacCutchan 19-Aug-17 5:23am    
Suggestions:
- Start by giving your variables meaningful names, so we can understand the purpose of each one.
- Next take out all those extra expressions from your for statements so we can understand what they are supposed to be doing.
- Then add some comments so the code is easier to understand.

Quote:
So, i have to print Armstrong numbers between 1 and 500.

There is no way an integer variable can hold so much digits. An int can't hold much more than 10 digits.
You should study the different C data types.

Oops, made a mistake between the values and number of digits

C++
for(int a = 1, b = a, c = a, sum = 0, digi = 0;a <= 150;a++, b = a, digi = 0, c = a, sum = 0)

There is no point to type such code, there is no gain, it only make it more difficult to read and to understand.
Any gain in number of lines of code is at the expense of readability.

[Update]
Advice: make your code easier to read.
Then use the debugger to see your code perform, it will help you to understand why it go wrong.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
v3
Comments
Graeme_Grant 19-Aug-17 6:32am    
Well ... at least get it working first, then you can have fun making as difficult to read as possible! ;)

But as ppolymorphe very rightfully said, readability is very important for others to read and not go WTF when you go back to it in 6+ month's time!
Patrice T 19-Aug-17 6:40am    
Hi,
Are you sure your comment is at the right place ?
Graeme_Grant 19-Aug-17 6:42am    
Yes ... just adding to yours! ;)
Patrice T 19-Aug-17 6:45am    
ok
Graeme_Grant 19-Aug-17 6:58am    
:)
You can use sprintf() to convert each value (from 1 to 500) to a displayable string. The string length will be the number of digits (i.e. exponent) and subtracting '0' from each char in the array (using a loop) will give you the digit value and you already have the exponent.
Coding this way should be easier and you should be able to 'submit in a couple of days hours'.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900