#include<stdio.h>
void main()
{int a[100];int i;int getchar;int sum;
 for(i=0;i<=100;i++)
  while 
  {((a[i]=getchar())!=EOF)}
   i++;
printf("your no is =%d",&a[i]);
 i=0;
sum=0;
while(i<=100)
 {sum=sum+((a[i])^3);
 }
 i++;
if(sum==a[100])
 printf("number is armstrong");
else
 printf("number is normal");

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

Recommended Answers

All 6 Replies

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.

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.

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
a/=10;

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:
i=len;
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
            temp/=10;
        }
        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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.