Armstrong Number In A Particular Range Example Java Program


Definition

An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 3

Armstrong Number In A Particular Range Example Program

import java.util.Scanner; 

public class FindArmstrongNumberInRange{ 
	public static void main(String args[]){ 
		Scanner in=new Scanner(System.in); 
		System.out.print("Enter the Range :"); 
		int range=in.nextInt(); 
		int num1,num2,num3,sum,count=0; 
		num2=1; 
		while(num2<=range){ 
			sum=0; 
			num3=num2; 
			while(num3>0){ 
				num1=num3%10; 
				sum=sum+(num1*num1*num1); 
				num3=num3/10; 
				} 
			if(sum==num2){ 
				System.out.println(num2+" is a Armstrong Number"); 
				count=count+1; 
			} 
		num2++; 
		} 
	System.out.println("Total Armstrong Number Present With in that Range is "+count); 
	} 
} 

Sample Output

Output is:
Enter the Range :1067
1 is a Armstrong Number
153 is a Armstrong Number
370 is a Armstrong Number
371 is a Armstrong Number
407 is a Armstrong Number
Total Armstrong Number Present With in that Range is 5