Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.
Program To Find Armstrong Number Between Two Number
In this article we will see different ways to find Armstrong number between two numbers.
Armstrong Number:
A n digit number in which cube sum of all It’s digit is equal to the number it self .
For example :
407 is an Armstrong number since (4*4*4) + (0*0*0)+ (7*7*7)= 407.
3 Different approaches to do it.
- Checking Armstrong number using while loop
- Checking Armstrong number using for loop
- Checking Armstrong number using recursion
Method 1: Checking Armstrong number using while loop
Using while loop we can check Armstrong numbers within a range.
Approach:
- Enter first number .
- Enter second number .
- Traverse between 2 number using for loop .
- In that loop do following step,
- Using
while loop
Calculate its cube sum of digits . - Compare with the original number and print according to it .
Program:
import java.util.Scanner; public class Main { public static void main(String[] args) { // CREATING OBJECT Scanner sc = new Scanner(System.in); // TAKING RANGE INPUT FORM USER System.out.print("Enter the 1st number : "); int n1= sc.nextInt(); System.out.print("Enter the 2nd number : "); int n2= sc.nextInt(); System.out.print("Amstrong number are : "); //traversing between two number for( int i=n1; i<=n2;i++) { // storing original value in temp variable int k=i; int temp=0; // calculating the cube sum of digit of that number while(k>0) { int a=k%10; temp=temp+(a*a*a); k=k/10; } if (temp==i) System.out.println(" "+ temp); } } }
Output: Enter the 1st number : 1 Enter the 2nd number : 1000 Armstrong number are : 1 153 370 371 407
Method 2: Checking Armstrong number using for loop
Using for loop we can check Armstrong numbers within a range.
Approach:
- Enter first number .
- Enter second number .
- Traverse between 2 number using for loop .
- In that loop do following step,
- Using
for loop
Calculate its cube sum of digits . - Compare with the original number and print according to it
Program:
import java.util.Scanner; public class Main { public static void main(String[] args) { // CREATING OBJECT Scanner sc = new Scanner(System.in); // TAKING RANGE INPUT FORM USER System.out.print("Enter the 1st number : "); int n1= sc.nextInt(); System.out.print("Enter the 2nd number : "); int n2= sc.nextInt(); System.out.print("Amstrong number are : "); //traversing between two number for( int i=n1; i<=n2;i++) { // storing original value in temp variable int k=i; int temp=0; // calculating the cube sum of digit of that number for( ;k!=0;k/=10 ) { int a=k%10; temp=temp+(a*a*a); } if (temp==i) System.out.println(" "+ temp); } } }
Output: Enter the 1st number : 1 Enter the 2nd number : 1000 Armstrong number are : 1 153 370 371 407
Method 3: Checking Armstrong number using recursion
Using for loop we can check Armstrong numbers within a range.
Approach :
- Create a function that will calculate and return the sum of cube of digit .
- Enter first number .
- Enter second number .
- Traverse between 2 number using for loop .
- In that loop do following step,
- Parse the entered value to that function and store it to a variable .
- Compare with the original number and print according to it .
Program :
import java.util.Scanner; public class Main { int fams(int n,int a) { //calculating cube sum of digits if(n!=0) { int x=n%10; a=a+(x*x*x); n/=10 ; return fams(n,a); } return a; } public static void main(String[] args) { // CREATING OBJECT Scanner sc = new Scanner(System.in); // TAKING RANGE INPUT FORM USER System.out.print("Enter the 1st number : "); int n1= sc.nextInt(); System.out.print("Enter the 2nd number : "); int n2= sc.nextInt(); System.out.print("Amstrong number are : "); // creating the object of main function Main ams= new Main(); // traversing between two number for( int i=n1; i<=n2;i++) { // storing original value in temp variable int k=i; //parsing value to the function int temp=ams.fams(i,0); // comparing with the orginal value and printing it. if (k==temp) System.out.println(" " +k); } } }
Output: Enter the 1st number : 1 Enter the 2nd number : 1000 Armstrong number are : 1 153 370 371 407
Understand the Programming Language Java and learn the language fastly by using our wide range of Java Programming Examples with Output and try to write programs on your own.
Related Java Decision Making and Loop Programs:
- Java Program to Check Armstrong Number
- Java Program to Check Leap Year
- Java Program to Check Whether a Number is Positive or Negative
- Java Program to Check Whether a Character is Alphabet or Not
- Java Program to Calculate the Sum of Natural Numbers
- Java Program to Find Factorial of a Number
- Java Program to Generate Multiplication Table
- Java Program to Find GCD of two Numbers
- Java Program to Find LCM of two Numbers
- Java Program to Display Alphabets (A to Z) using loop
- Java Program to Count Number of Digits in an Integer
- Java Program to Check Palindrome
- Java Program to Check Whether a Number is Prime or Not
- Java Program to Make a Simple Calculator Using switch…case
- Java Program to Sort Elements in Lexicographical Order (Dictionary Order)