#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i,n,a=0,b=0,sum=0;
    printf("enter the limit of amstrong number: ");
    scanf("%d",n);
    for(i=1;i<=n;i++)
    {
        a=i;
        while(a>0)
        {
           b=n%10;
           sum=sum+ b*b*b;
           a=a/10;
        }
        if(sum==i)
        {
            printf("%d",sum);
        }
    }
return 0;

}
it prints no errors but doest not work properly

Recommended Answers

All 8 Replies

Your code won't compile with that extra meaningless text at bottom of the program.

i dont understand wat ur trying to say waltp

Line 26. Your program can't compile with that line in it.

And if that's supposed to be a question, after 200+ posts you'd think you'd know better than to simply say "it doesn't work" and let us try to figure out what that means.

oooh u think people after 200 posts are more experienced???but iam new to programming even after 200 posts

But you aren NOT new to asking questions. You KNOW when you ask questions you must give an explanation of the problem, not just tell us to fix it.

no not like that wat i say is im not expert and experienced

Try this

#include <stdio.h>
#include <stdlib.h>

int main()
{   
    int i,n,a=0,b=0,sum=0;
    printf("enter the limit of amstrong number: ");
    scanf("%d",&n); // CHANGE :: scanf expects an address as second argument
    for(i=1;i<=n;i++)
    {
        sum=0; // CHANGE :: reinitialize sum to zero everytime
        a=i;
        while(a>0)
        {
            b=a%10; // CHANGE :: Perform operations on the actual number not 'n'
            sum=sum+ (b*b*b); // CHANGE :: () Brackets make the code readable
            a=a/10;
        }
        if(sum==i)
        {
            printf("\n%d\n",sum); // CHANGE :: Use \n to make prints readable
        }
    }
    return 0;
}

I hope this helps!!
And yes, I agree with @WaltP. Please take care of these things in your future questions.

While this doesn't even deserve an answer as you don't even ask a proper question (You just dump your code and state "it doesn't work!") you could try this.

I'm not going to explain anything about it I guess as you didn't ask me to. It's also a naive implementation that could be optimized a lot but I'm not going to waste time on someone who doesn't even put in an effort to post his thread.

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

int get_digit_count(unsigned int number);

// Obtain the amount of digits a number consists of.
int get_digit_count(unsigned int number)
{
    int digits = 0;

    do
    {
        // Cut off the last digit
        number /= 10;

        // Add this digit to the total number of digits.
        digits++;
    }
    // Do this until there are no more digits left.
    while (number != 0);

    return digits;
}


// Entry point of the application. Naive implementation 
// of an algorithm to find armstrong numbers. 
int main(void) 
{
    int i                 = 0,    // Integer for iterating.
        limit             = 0,    // The limit of the search.
        digits            = 0,    // The amount of digits for the current number.
        powered_digit_sum = 0,    // The sum of the digits pow'd by the number of digits
        current_number    = 0;    // Current number looked at.

    printf("Enter the limit of the search (inclusive): ");
    scanf ("%d", &limit);

    printf("Calculating armstrong numbers from 0-%d (inclusive)..\n", limit);

    // Go through all numbers from 1 till 'limit' (inclusive)
    for(i = 0; i <= limit; i++)
    {
        // Reset the sum of digits that were raised by the power of 'digits'.
        powered_digit_sum   = 0;   

        // Store the number currently being processed. This copy may be modified.
        current_number      = i;     

        // Obtain the amount of digits this number consists of.
        digits = get_digit_count(current_number);

        // Go through every digit
        while (current_number != 0)
        {
            //Get the last digit of the remaining part and add it's cubed value.
            powered_digit_sum += pow((current_number % 10), digits);

            // Remove the last digit
            current_number /= 10;
        }

        // The cube sum is equal to the number => This is an armstrong number.
        if (powered_digit_sum == i)
        {
            printf("Armstrong number found: %d.\n", i);
        }
    }

    return 0;
}
commented: Then why did you bather doing his moework for him in the first place? -3
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.