void main()
{int a[100];int i;int getchar;int sum;
printf("your no is =%d",&a[i]);
 printf("number is armstrong");
 printf("number is normal");

not working,can someone give me an alternative usiing arrays,i got the for loop,want to execute using arrays!

5 Years
Discussion Span
Last Post by elrond

If it's not working, have you considered fixing it instead of asking for something completely different? Or is it that you stole that code from somewhere and aren't capable of fixing it?

I can tell you right now that (a[i])^3 is meaningless because ^ is a bitwise XOR operator, not a power operator.

Edited by deceptikon


Im sorry, i wrote it in a book and typed it into the pc should have used the pow finction.and by alternatives i meant fix to this as im quite inexperienced in the area!


by alternatives i meant fix to this as im quite inexperienced in the area!

Clearly. You seem to understand what Armstrong numbers are, that's not a problem. But your C is atrocious to the point where I'd question if you know it at all. Because enumerating all of the problems in your code and explaining how to fix them would take herculean efforts, I'll recommend that you start over from the beginning of your textbook, and work through it more slowly to make sure that you're absorbing all of the information. Fundamentals are important because if you don't have a strong foundation, everything will be exponentially more difficult.

Edited by deceptikon


Here's a hint of how you can compute this number:
8^4 + 2^4 + 0^4 + 8^4 = 8208.
Split the number into its digits. Sum the digits acordingly than compare to the initial number.
How to split a number to get its digits, well simple:

int a = 1234;
int b = a%10; //b has the value of 4

Put that into a for, from 0 < len(number) and you'll have all your digits (make sure you store them in an array);
How to find the length of a number? Well, you could either, see if the number if smaller than a given number, and than return its length, like

if (a<10) return 1;
else if (a<100) return 2;
// and so on

or you could apply log10 on that number:

int len = (int)log10(number)+1;

Another thing: elements are stored in the array in reverse order, so, make sure you apply the pow function in reverse order. Like this:

//1234 will be stored in the array like this:
// 4 3 2 1 so when applying the sumation make sure you don't forget:
result = 0;
for (;i--;) result += pow(a[i], len);

And here's my general aproach of this program:

#include <stdio.h>
#include <math.h>

int main(){
    int i;
    for (i=1;i<99999;i++){
        int temp = i, len = (int)log10(i)+1, rez=0, j=0; //set the variables
        int a[len]; //set the array
        for (;j<len;j++){ 
            a[j] = temp%10; //get the digits
        for (;j--;) rez+=pow(a[j], len); //make the sumation
        if (rez==i) printf("%d\n", rez); //check the result
    return 0;

Thanks lucavi andrew and deceptikon thanks to you too maybe inwas getting ahead of myself!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.