Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5946845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:52:49+00:00 2026-05-22T16:52:49+00:00

I’m trying to check whether or not the number provided by the user is

  • 0

I’m trying to check whether or not the number provided by the user is an armstrong number. 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);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:52:50+00:00Added an answer on May 22, 2026 at 4:52 pm

    The primary problem is that you don’t keep a record of the number you start out with. You divide a by 10 repeatedly (it ends as 0), and then compare 0 with 153. These are not equal.

    Your other problem is that you can’t look for 4-digit or longer Armstrong numbers, nor for 1-digit ones other than 1. Your function fun() would be better named cube(); in my code below, it is renamed power() because it is generalized to handle N-digit numbers.


    I decided that for the range of powers under consideration, there was no need to go with a more complex algorithm for power() – one that divides by two etc. There would be a saving on 6-10 digit numbers, but you couldn’t measure it in this context. If compiled with -DDEBUG, it includes diagnostic printing – which was used to reassure me my code was working right. Also note that the answer echoes the input; this is a basic technique for ensuring that you are getting the right behaviour. And I’ve wrapped the code up into a function to test whether a number is an Armstrong number, which is called iteratively from the main program. This makes it easier to test. I’ve added checks to the scanf() to head off problems, another important basic programming technique.

    I’ve checked for most of the Armstrong numbers up to 146511208 and it seems correct. The pair 370 and 371 are intriguing.

    #include <stdio.h>
    #include <stdbool.h>
    
    #ifndef DEBUG
    #define DEBUG 0
    #endif
    
    static int power(int x, int n)
    {
        int r = 1;
        int c = n;
        while (c-- > 0)
            r *= x;
        if (DEBUG) printf("    %d**%d = %d\n", x, n, r);
        return r;
    }
    
    static bool isArmstrongNumber(int n)
    {
        int y = 0;
        int a = n;
        int p;
        for (p = 0; a != 0; a /= 10, p++)
            ;
        if (DEBUG) printf("    n = %d, p = %d\n", n, p);
        a = n;
        for (int i = 0; i < p; i++)
        {
            y += power(a % 10, p);
            a /= 10;
        }
        return(y == n);
    }
    
    int main(void)
    {
        while (1)
        {
            int a;
            printf("Enter the number you want to identify as an Armstrong number or not: ");
            if (scanf("%d", &a) != 1 || a <= 0)
                break;
            else if (isArmstrongNumber(a))
                printf("%d is an Armstrong number\n", a);
            else
                printf("%d is not an Armstrong number\n", a);
        }
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.