Click here to Skip to main content
15,891,721 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Implement a function in C program to check whether number is Armstrong or not. * A positive integer is called an Armstrong number (of order n) if * abcd... = a^n + b^n + c^n + d^n + ... * where n is the number of digits in the given number # Sample input: 3 123 1000 370 # Explaination: * First line is the number of inputs. * Next line is the integers which must be checked seperated by space # Sample output: NO NO YES

What I have tried:

I have tried, but i cant get the desired outputs and testcases.
Posted
Updated 31-Mar-22 1:23am
Comments
CPallini 31-Mar-22 7:58am    
Then show us your code. We could try to fix it.

Start by knowing that "abcd" is a short form for "a*1000 + b*100 + c*10 + d*1" and n=4 (because you have 4 digits).

You simply have to decompose every input number into its composing digits (3 yields 3, 123 yields 1, 2 and 3, etc.) and compute both the left and right side of the equation.

For the left side, in order to determine the digits and count them, you can perform a loop that computes the remainder of division by 10 repeatedly until the input is 0.
For the right side, to compute the value, you only have to loop the discovered digits and elevate each one to the discovered count, then sum all partial results.
 
Share this answer
 
Comments
CPallini 31-Mar-22 7:58am    
5.
While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900