Tip

  • Creator
    Topic
  • #3977304

    Checking Armstrong Number in C

    Locked

    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);
    }

All Comments

  • Author
    Replies
    • #3977449
      Avatar photo

      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/

    • #4050712

      Armstrong Number

      by Web123805 ·

      In reply to Checking Armstrong Number in C

      If the result of this calculation is equal to the original number entered by the user, the program prints a message indicating that the number is an Armstrong number. Otherwise, it prints a message indicating that the number is not an Armstrong number.

Viewing 2 reply threads