Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

is there any problem in my code to identify armstrong number?

I am finding Armstrong number but it’s showing "not a Armstrong number" Everytime and I can’t find the solution. as my observation I think there is some problem the sum section. the sum is not giving the correct value.

#include<stdio.h>
#include<math.h>
int main()
{
    int num, original_num, sum=0, lastdigit, digit, count=1;

    printf("Enter the number: ");
    scanf("%d", &num);

    original_num = num;

    while (num >= 10)
    {
        count++;
        num /= 10;
    }
    digit = count;

    while (num > 0)
    {
        lastdigit = num % 10;
        sum += pow(lastdigit, digit);      /** here is the problem lying as my 
                                              observation, the sum is giving the 
                                              incorrect value.**/
        num/=10;
    }
    if (original_num == sum)
    {
        printf("The number is an ARMSTRONG number");
    }
    else
    {
        printf("The number is not a ARMSTRONG number");
    }
    return 0;
    }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

num has already became single digit after first while loop(which counts the number of digits). In which case your second loop which takes every digit in the number and raise it to the n power is running only once.

You need to restore the number before second loop.

  num = original_num; 

   while (num > 0)
    {
        lastdigit = num % 10;
        sum += pow(lastdigit, digit);      
        num/=10;
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading