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}
1// 4 digit armstrong number in java
2public class ArmstrongNumberDemo
3{
4 public static void main(String[] args)
5 {
6 int num = 9474, realNumber, remainder, output = 0, a = 0;
7 realNumber = num;
8 for(;realNumber != 0; realNumber /= 10, ++a);
9 realNumber = num;
10 for(;realNumber != 0; realNumber /= 10)
11 {
12 remainder = realNumber % 10;
13 output += Math.pow(remainder, a);
14 }
15 if(output == num)
16 {
17 System.out.println(num + " is an Armstrong number.");
18 }
19 else
20 {
21 System.out.println(num + " is not an Armstrong number.");
22 }
23 }
24}
1 int c=0,a,temp;
2 int n=153;//It is the number to check armstrong
3 temp=n;
4 while(n>0)
5 {
6 a=n%10;
7 n=n/10;
8 c=c+(a*a*a);
9 }
10 if(temp==c)
11 System.out.println("armstrong number");
12 else
13 System.out.println("Not armstrong number");
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}