Armstrong number in Java. Here we have written the code in four different ways standard, using for loop, recursion, while loop and also with different examples as like: between 100 and 999, between 1 to 1000 and between 1 to 500 with sample outputs and online execution tool embedded.
What is Armstrong Number?
A: An Armstrong number of three digits is an integer, where the sum of the cubes of its digits is equal to the number itself.
[table id=16 /]
Here is the example for you :
Consider the example: 371
A: 3^3 + 7^3 + 1^3 = 371 ( If you add those all numbers, the final digit should be same as given number ).
Here is the list of Java programs with different methods in different ways. Do check out the table of contents so that you will get an idea.
1. Using static method Between 100 and 999
[wp_ad_camp_3]
Here is the first sample program using the static method with sample output as well. Here we used the method ‘ Static ‘ and taken numbers between 100 to 999. Once you are done with the execution, it automatically displays the Armstrong numbers between 100 and 999. Check out the sample output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import java.util.Scanner; class ArmstrongWhile { public static void main(String[] arg) { int i=100,arm; System.out.println("Armstrong numbers between 100 to 999"); while(i<1000) { arm=armstrongOrNot(i); if(arm==i) System.out.println(i); i++; } } static int armstrongOrNot(int num) { int x,a=0; while(num!=0) { x=num%10; a=a+(x*x*x); num/=10 ; } return a; } } |
1 2 3 4 5 |
Armstrong numbers between 100 to 999 153 370 371 407 |
2. Using while Loop ( 1 to 500 )
[wp_ad_camp_2]
There you go another method using while loop. Java, while loop is nothing but executing a set of statements repeatedly as long as condition, is true – here is the complete guide on while loop in Java with examples. In this case, we have taken the example of numbers from 1 to 500. Upon execution, you will get to know what are Armstrong numbers. Check out the output below so that you will get an idea.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
class ArmstrongWhile { public static void main(String[] arg) { int i=1,a,arm,n,temp; System.out.println("Armstrong numbers between 1 to 500 are"); while(i<500) { n=i; arm=0; while(n>0) { a=n%10; arm=arm+(a*a*a); n=n/10; } if(arm==i) System.out.println(i); i++; } } } |
1 2 3 4 5 6 |
Armstrong numbers between 1 to 500 are 1 153 370 371 407 |
3. Using Recursion ( Between 1 to 1000 )
Another method using recursion: A function that calls itself is called recursion. Here is the complete code with the sample program and the output of the program. Here we have taken the example of numbers from 1 to 1000. Check it out.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import java.util.Scanner; class ArmstrongRecursion { int x; int findArmstrong(int n,int a) { if(n!=0) { x=n%10; a=a+(x*x*x); n/=10 ; return findArmstrong(n,a); } return a; } public static void main(String[] arg) { ArmstrongRecursion A=new ArmstrongRecursion(); int arm; System.out.println("Armstrong numbers between 1 to 1000"); for(int num=1;num<500;num++) { arm=A.findArmstrong(num,0); if(arm==num) System.out.println(num); } } } |
1 2 3 4 5 6 |
Armstrong numbers between 1 to 1000 1 153 370 371 407 |
4. Using BufferedReader
There you another method using bufferedreader. Do check it out.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.io.*; class ArmstrongBuf { public static void main(String[] arg) throws IOException { int a,arm=0,n,temp; BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a number"); n = Integer.parseInt(in.readLine()); temp=n; while(n!=0) { a=n%10; arm=arm+(a*a*a); n=n/10; } if(arm==temp) System.out.println(temp+" is a armstrong number "); else System.out.println(temp+" is not a armstrong number "); } } |
Output:
1 2 3 |
Enter a number 153 153 is a armstrong number |
5. Using for loop ( 100 to 500 )
Another sample program to print Armstrong number using for loop. Check out the program along with the sample output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Scanner; class Armstrong { public static void main(String[] arg) { int a,arm=0,n,temp; Scanner sc=new Scanner(System.in); System.out.println("Enter a number"); n=sc.nextInt(); temp=n; for( ;n!=0;n/=10 ) { a=n%10; arm=arm+(a*a*a); } if(arm==temp) System.out.println(temp+" is a armstrong number "); else System.out.println(temp+" is not a armstrong number "); } } |
That’s it. If you have any doubts related to the above code. Just leave a comment here at the end of the post. We do try to help you out related to ant query asap.
More programs for you: