How to display Armstrong numbers between 1 to n
Is this for a Programming Exercise?
Plenty of Articles on this one with lots of pseudo code around!
My approach would be
Start with 1, see if it's an Armstrong number, if not, move to the next number and keep going until you reach n.
Not sure how performant this is
If you just need to display the list, and not want to calculate each time, put in a static entity
@Praveen Tumati ,
judging by the type of questions you have asked so far, you are learning to become a developer.
Just blankly copying the exercises from your course, will teach you very little.
The whole point is that you learn to use whatever tools any software language or platform has to offer, and put them to good use to solve your problem.
Just try and solve things yourself, and if you try something, and get stuck, come and ask how to move forward.
Dorine
Hi Praveen
Armstrong number is a number that is equal to the sum of cubes of its digits. For example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers.
Let's try to understand why 153 is an Armstrong number.
153 = (1*1*1)+(5*5*5)+(3*3*3)
where:
(1*1*1)=1
(5*5*5)=125
(3*3*3)=27
So: 1+125+27=153
This Logic you must try !!
Kind regards
Sourabh Sharma
@Praveen Tumati,
As you asked how to display Armstrong numbers from 1 to n, at first you need to know what an Armstrong number is.
So an Armstrong number is a number whose each digit is multipled as many times as the number of digits of that number and then added to get the number.
For example :-
《1》
The number is n = 153
Number of digits is d = 3
So each digit will be multiplied as the number of digits...
Like, 153 = (1*1*1) + (5*5*5) + (3*3*3)
where, (1*1*1) = 1
(5*5*5) = 125
(3*3*3) = 27
So, 125 + 27 + 1 = 153
《2》
The number is n = 1634
Number of digits is d = 4
Like, 1634 = (1*1*1*1) + (6*6*6*6) + (3*3*3*3) + (4*4*4*4)
where, (1*1*1*1) = 1
(6*6*6*6) = 1296
(4*4*4*4) = 256
(3*3*3*3) = 81
So, 1296 + 256 + 81 + 1 = 1634
Now, when there are numbers upto 'n' for that this might help you.
First, run an iteration from 1 to n. Then check whether each number is an Armstrong number or not and then display that number.
Like,
int n, i = 0, n1 = i, c = 0, s = 0, d = 0;
for (i = 1; i < = n; i ++)
{
while ( n1 > 0)
c++;
n1 = n1 / 10;
}
n1 = i;
while (n1 > 0)
d = n % 10;
s = s + (int) Math.pow (d,c);
if ( s == i)
System.out.println (i + " is an Armstrong number");
Hope this helps...