import java.util.*;
/* For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.
* Write a program to find all Armstrong number in the range of 0 and 999.
*/
public class Armstrong {
public static void main(String[] args) {
int no=1,rem=1,mod=1,armstrong;
//check for armstrong
System.out.println("Enter armstrong no to be checked: ");
Scanner sc=new Scanner(System.in);
no=sc.nextInt();
armstrong=no;
while(no>=0)
{
System.out.println(no);
mod=no%10;
System.out.println(no);
rem=rem+(mod*mod*mod);
System.out.println(no);
no=no/10;
System.out.println(no);
}
if(no==armstrong)
System.out.println("it is armstrong no");
}
}
Asked
Active
Viewed 45 times
-4
-
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 this is the output – Shashank Mar 01 '17 at 05:21
-
Please dont flag it duplicate as I know armstrong no code I want to know where I went wrong with my code. Thanks. – Shashank Mar 01 '17 at 05:22
-
What's your input? – shmosel Mar 01 '17 at 05:23
-
I'm guessing http://stackoverflow.com/questions/4685450/why-is-the-result-of-1-3-0 – Guy Mar 01 '17 at 05:23
-
1`while(no>=0)` will loop until `no` is negative. When do you expect that to happen? – shmosel Mar 01 '17 at 05:25
1 Answers
0
Avoid infinite loop. Change
while(no>=0)
to
while(no>0)
Since
no=no/10;
can reduce a positive number least to0
. Not less than that.
- Assign
rem=0
since this is where you are calculating the sum.

Naman
- 27,789
- 26
- 218
- 353
-
-
@Shashank cool, if that solves the problem, be kind enough to mark it as an answer. – Naman Mar 04 '17 at 05:54
-