Program for Armstrong Number in Java

Here you will get program for armstrong number in java.

A number which is equal to the sum of its digits raised to the power n is called armstrong number. Here n is the total digits in the given number.

Example: 9 is an armstrong number as n = 1 and 9= 9.

153 is an armstrong number as n = 3 and 13 + 53 + 3= 1 + 125 + 27 = 153.

Program for Armstrong Number in Java

package com;

import java.util.Scanner;

public class ArmstrongNumberJava {
	public static void main(String args[]) {
		int n, m, p = 0, len;
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter a number:");
		n = sc.nextInt();
		m = n;
		
		//finding total digits in number
		len = String.valueOf(n).length();
		
		while(m!=0) {
			p = p + (int)Math.pow(m%10, len);
			m = m/10;
		}
		
		if(p == n){
			System.out.println("Armstrong Number");
		}
		else{
			System.out.println("Not Armstrong Number");
		}
	}
}

 

Output

Enter a number:
8208
Armstrong Number

Comment below if you are facing any difficulty to understand concept of armstrong number in java.

Leave a Comment

Your email address will not be published. Required fields are marked *