Java Armstrong Number

  1. Check Given number is Armstrong number or not in Java
  2. Find Armstrong numbers between two numbers in Java
  3. Generate Armstrong numbers upto N in Java
  4. Calculate total number of Armstrong number between two numbers in Java

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Check Given number is Armstrong number or not in Java


Armstrong_Ex1.java

import java.util.Scanner;
 
public class Armstrong_Ex1 {
	
	int number;
	Scanner scan;
	
	void getVal() {
		
		scan = new Scanner(System.in);
		System.out.println("Check Armstrong Number or not");
		
		System.out.println("\nEnter the number");
		number = Integer.parseInt(scan.nextLine());		
	}
 
	void armstrong() {
		
		int temp = number;
		int sum = 0;
		int mod = 0;
		
		while(temp != 0) {
			
			mod = temp % 10;
			sum = sum + (mod * mod * mod);
			temp = temp / 10;
		}
		
		if(sum == number) 
			System.out.println(number + " is an Armstrong number");
		else
			System.out.println(number + " is not an Armstrong number");
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Armstrong_Ex1 obj = new Armstrong_Ex1();
		
		obj.getVal();
		obj.armstrong();
	}
}

Sample Output

Check Armstrong Number or not

Enter the number
371
371 is an Armstrong number
Check Armstrong Number or not

Enter the number
279
279 is not an Armstrong number




B. Find Armstrong numbers between two numbers in Java


Armstrong_Ex2.java

import java.util.Scanner;
 
public class Armstrong_Ex2 {
	
	int start, end;
	Scanner scan;
	
	void getVal() {
		
		scan = new Scanner(System.in);
		System.out.println("Find Armstrong Numbers..");
		
		System.out.println("\nEnter the Staring value : ");
		start = Integer.parseInt(scan.nextLine());
		
		System.out.println("Enter the End value : ");
		end = Integer.parseInt(scan.nextLine());
	}
 
	void armstrong() {
		
		System.out.println();
		
		for(int i=start; i<end; i++) {
			
			int temp = i;
			int sum = 0;
			int mod = 0;
			
			while(temp != 0) {
				
				mod = temp % 10;
				sum = sum + (mod * mod * mod);
				temp = temp / 10;
			}
			
			if(sum == i) 
				System.out.println(i + " is an Armstrong number");
		}		
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Armstrong_Ex2 obj = new Armstrong_Ex2();
		
		obj.getVal();
		obj.armstrong();
	}
}

Sample Output

Find Armstrong Numbers..

Enter the Staring value : 
10
Enter the End value : 
500

153 is an Armstrong number
370 is an Armstrong number
371 is an Armstrong number
407 is an Armstrong number




C. Generate Armstrong numbers upto N in Java


Armstrong_Ex3.java

import java.util.Scanner;
 
public class Armstrong_Ex3 {
	
	int n;
	Scanner scan;
	
	void getVal() {
		
		scan = new Scanner(System.in);
		System.out.println("Generate Armstrong Numbers..");
		
		System.out.println("\nEnter the 'n' value : ");
		n = Integer.parseInt(scan.nextLine());
 
	}
 
	void armstrong() {
		
		System.out.println();
		
		for(int i=0; i<n; i++) {
			
			int temp = i;
			int sum = 0;
			int mod = 0;
			
			while(temp != 0) {
				
				mod = temp % 10;
				sum = sum + (mod * mod * mod);
				temp = temp / 10;
			}
			
			if(sum == i) 
				System.out.println(i + " is an Armstrong number");
		}
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Armstrong_Ex3 obj = new Armstrong_Ex3();
		
		obj.getVal();
		obj.armstrong();
	}
}

Sample Output

Generate Armstrong Numbers..

Enter the 'n' value : 
500

0 is an Armstrong number
1 is an Armstrong number
153 is an Armstrong number
370 is an Armstrong number
371 is an Armstrong number
407 is an Armstrong number




D. Calculate total number of Armstrong number between two numbers in Java


Armstrong_Ex4.java

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
 
public class Armstrong_Ex4 {
	
	int start, end;
	Scanner scan;
	List<Integer> list;
	
	void getVal() {
		
		scan = new Scanner(System.in);
		System.out.println("Find Number of Armstrong Numbers..");
		
		System.out.println("\nEnter the Staring value : ");
		start = Integer.parseInt(scan.nextLine());
		
		System.out.println("Enter the End value : ");
		end = Integer.parseInt(scan.nextLine());
	}
 
	void armstrong() {
		
		System.out.println();
		list = new ArrayList<Integer> ();
		
		for(int i=start; i<end; i++) {
			
			int temp = i;
			int sum = 0;
			int mod = 0;
			
			while(temp != 0) {
				
				mod = temp % 10;
				sum = sum + (mod * mod * mod);
				temp = temp / 10;
			}
			
			if(sum == i) 
				list.add(i);
		}
		
		System.out.println("The Total number of Armstrong number is : " + list.size());
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Armstrong_Ex4 obj = new Armstrong_Ex4();
		
		obj.getVal();
		obj.armstrong();
	}
}

Sample Output

Find Number of Armstrong Numbers..

Enter the Staring value : 
10
Enter the End value : 
500

The Total number of Armstrong number is : 4






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com