1// Check whether a given number is armstrong number or not
2import java.util.Scanner;
3public class ArmstrongNumber
4{
5 public static void main(String[] args)
6 {
7 int x, y, z = 0, temp;
8 Scanner sc = new Scanner(System.in);
9 System.out.println("Please enter a number: ");
10 x = sc.nextInt();
11 temp = x;
12 while(x > 0)
13 {
14 y = x % 10;
15 x = x / 10;
16 z = z + (y * y * y);
17 }
18 if(temp == z)
19 {
20 System.out.println(temp + " is an Armstrong Number.");
21 }
22 else
23 {
24 System.out.println(temp + " is not an Armstrong Number.");
25 }
26 sc.close();
27 }
28}
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
3public class Armstrong
4{
5 public static void main(String args[])
6 {
7 int num,temp,c=0;
8 Scanner in=new Scanner(System.in);
9 num=in.nextInt();
10 temp=num;
11 while(num!=0)
12 {
13 int d=num%10; //extracting last digit
14 c+=d*d*d;
15 num/=10; // removing last digit
16 }
17 if(temp==c)
18 {
19 System.out.println("Number is Armstrong");
20 }
21 else
22 System.out.println("Number is not Armstrong");
23 }
24}
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}