Tip

  • Creator
    Topic
  • #3977304

    Checking Armstrong Number in C

    by Soham1087 ·

    I’m trying to check whether or not the number provided by the user is an Armstrong number in C. Something is wrong though and I can’t figure it out.

    Any help is appreciated.

    Code attached below.

    #include<stdio.h>

    int fun(int);

    int main()
    {
    int x,a,b,y=0;

    printf(“enter the number you want to identify is aN ARMSTRONG OR NOT:”);
    scanf(“%d”,&a);

    for(int i=1 ; i<=3 ; i++)
    {
    b = a % 10;
    x = fun(b);
    y = x+y;
    a = a/10;
    }

    if(y==a)
    printf(“\narmstrong number”);
    else
    printf(“\nnot an armstrong number”);

    return 0;
    }

    int fun(int x)
    {
    int a;
    a=x*x*x;
    return (a);
    }

You are posting a reply to: Checking Armstrong Number in C

The posting of advertisements, profanity, or personal attacks is prohibited. Please refer to our Community FAQs for details. All submitted content is subject to our Terms of Use.

All Comments

  • Author
    Replies
    • #3977449

      Re: armstrong number

      by kees_b ·

      In reply to Checking Armstrong Number in C

      In such a case you print the intermediate results for x,y, a and b (that are 3 lines, since they are calculated three times) and compare them with your manual calculation for any a that gives the wrong result.

      Looks like a homework assignment for a first course in C. Maybe you are a beginning programmer. That’s why I write how to find it yourself.

    • #3977605

      Reply To: Checking Armstrong Number in C

      by thedomofcode ·

      In reply to Checking Armstrong Number in C

      First you should read couple of articles on Armstrong Number in C, you will get lot of results on the web which might help you solve the problem, I used to search on the web for my college assignment whenever I get Stuck.

      Anyway try this code – it might help you


      #include<stdio.h>

      int fun(int);

      int main()
      {
      int x,a,b,y=0;

      printf("enter the number you want to identify is aN ARMSTRONG OR NOT:");
      scanf("%d",&a);

      for(int i=1 ; i<=3 ; i++)
      {
      b = a % 10;
      x = fun(b);
      y = x+y;
      a = a/10;
      }

      if(y==a)
      printf("\narmstrong number");
      else
      printf("\nnot an armstrong number");

      return 0;
      }

      int fun(int x)
      {
      int a;
      a=x*x*x;
      return (a);
      }

      for your easy read, check these websites –

      1. https://stackoverflow.com/questions/71528936/armstrong-number-in-c-programming
      2. https://www.code4example.com/c/check-armstrong-number-of-n-digits-in-c/
      3. https://www.scaler.com/topics/armstrong-number-in-c/

Viewing 1 reply thread