Armstrong Number

 

Write a Program to determine if the given number is Armstrong number or not. Print true if number is armstrong, otherwise print false.

An Armstrong number is a number (with digits n) such that the sum of its digits raised to nth power is equal to the number itself.

For example,

371, as 3^3 + 7^3 + 1^3 = 371
1634, as 1^4 + 6^4 + 3^4 + 4^4 = 1634
Sample Input 1 :
103
Sample Output 1 :
false
import java.util.Scanner;
import java.math.*;

public class Armstrong {

	public static void main(String[] args) {
		
		Scanner s = new Scanner(System.in);
		int n=s.nextInt();
		
		int t=n;
		int num=n;
		int c=0;
		// d=0;
		int sum=0;
		while(num>0)
		{
			c++;
			num=num/10;
		}
		
		while(n>0)
		{
			sum=(int)(sum+(Math.pow(n%10, c)));
			n=n/10;
			
		}
		if(t==sum)
		{
			System.out.println("true");
		}
		else
		{
		System.out.println("false");
	}
	}
}

Comments

Popular posts from this blog

Minimum Length Word

Check Number Sequence

Star Pattern