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}
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}
1import java.util.Scanner;
2
3/*
4 *@author: Mayank Manoj Raicha
5 * Armstrong Number in Java: A positive number is called armstrong number if it is equal to the sum of cubes of its digits
6 * for example 0, 1, 153, 370, 371, 407 etc.
7 * 153 = (1*1*1)+(5*5*5)+(3*3*3) = 1+125+27 = 153
8 */
9public class ArmstrongNumberExample {
10
11 public static void main(String[] args) {
12
13 int sum = 0;
14
15 Scanner in = new Scanner(System.in);
16 System.out.println("Enter the number: ");
17 int input = in.nextInt(); //1634
18 String val = String.valueOf(input);
19 char[] charArray = val.toCharArray(); //charArray[0] = "1" , charArray[1] = "6", charArray[2] = "3", charArray[3] = "4"
20 int[] numArray = new int[charArray.length]; //Declaring this array to store the result of getPowerOfNumber() method for each digit.
21
22 //for each char element calculate the power of number and store it in the "cubedNumArray" array.
23 for(int i=0; i<charArray.length; i++) {
24 numArray[i] = getPowerOfNumber(Integer.parseInt(String.valueOf(charArray[i])), charArray.length);
25 sum = sum + numArray[i];
26 }
27
28 //Compare if the resulting sum is equal to the original input.
29 if(sum == input) {
30 System.out.println("Entered number is an Armstrong number.");
31 }else {
32 System.out.println("Entered number is NOT an Armstrong number.");
33 }
34
35 in.close();
36 }
37
38 //Calculate & Return the value of the first argument raised to the power of the second argument
39 public static int getPowerOfNumber(int num, int count) {
40 return (int) Math.pow(num, count);
41 }
42}