Discussion Locked

Checking Armstrong Number in C

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

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

3 replies

by kees_b edited

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.

by thedomofcode

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/

by Web123805 edited

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.

This discussion is closed to new replies.