java program to print armstrong number from 1 to 1000

Solutions on MaxInterview for java program to print armstrong number from 1 to 1000 by the best coders in the world

showing results for - "java program to print armstrong number from 1 to 1000"
Theo
04 Aug 2017
1public class ArmstrongBetween1To1000
2{
3   public static void main(String[] args)
4   {
5      int number, n, total = 0;
6      System.out.println("Armstrong number between 1 to 1000: ");
7      for(int a = 1; a <= 1000; a++)
8      {
9         number = a;
10         while(number > 0)
11         {
12            n = number % 10;
13            total = total + (n * n * n);
14            number = number / 10;
15         }
16         if(total == a)
17         {
18            System.out.print(a + " ");
19         }
20         total = 0;
21      }
22   }
23}
Valeria
27 Jul 2018
1// Java program to print all armstrong numbers between given range
2import java.util.Scanner;
3public class ArmstrongNumbersGivenRange 
4{
5   public static void main(String[] args) 
6   {
7      int number, startNumber, endNumber, a, rem, n, count = 0;
8      Scanner sc = new Scanner(System.in);
9      System.out.println("Please enter starting number range: ");
10      startNumber = sc.nextInt();
11      System.out.println("Please enter ending number range: ");
12      endNumber = sc.nextInt();
13      for(a = startNumber + 1; a < endNumber; a++)
14      {
15         n = a;
16         number = 0;
17         while(n != 0)
18         {
19            rem = n % 10;
20            number = number + rem * rem * rem;
21            n = n / 10;
22         }
23         if(a == number)
24         {
25            if(count == 0)
26            {
27               System.out.println("Armstrong numbers between given range " + startNumber + " and " + endNumber + ": ");
28            }
29            System.out.print(a + "  ");
30            count++;
31         }
32      }
33      // if there is no Armstrong number found between range
34      if(count == 0)
35      {
36         System.out.println("Sorry!! There's no armstrong number between given range " + startNumber + " and " + endNumber);
37      }
38      sc.close();
39   }
40}