-4
  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");

      }

   }
shmosel
  • 49,289
  • 6
  • 73
  • 138

1 Answers1

0
  1. Avoid infinite loop. Change

    while(no>=0)
    

    to

    while(no>0)
    

    Since no=no/10; can reduce a positive number least to 0. Not less than that.


  1. Assign rem=0 since this is where you are calculating the sum.
Naman
  • 27,789
  • 26
  • 218
  • 353