Armstrong Number Finder in java

Armstrong Number Finder in java

Given a number N, you need to check whether the given number is Armstrong number or not. Now what is Armstrong number let us see below:

A number is said to be Armstrong if it is equal to sum of the cube of its digits.

Input

A single integer N.

Output

You need to return 1 if the given number N is an Armstrong number otherwise 0.

Sample Input:

1

Sample Output:

1

Sample input

150

Sample Output:

0

Solution of Armstrong Number Finder in java:–

import java.util.*;
import java.lang.*;
import java.io.*;

public class Main
{

  static int demo(int x)
  {
    int cube=0,r,temp=x;
    while(x!=0)
      {
        r=x%10;
        cube=(r*r*r)+cube;
        x=x/10;
      }

    if(temp==cube)
    {
      return 1;
    }
    else{
      return 0;
    }
    
  }
	public static void main (String[] args) throws java.lang.Exception
	{
		//your code here
      Scanner scanner = new Scanner(System.in);
      int x= scanner.nextInt();
      int y=  demo(x);
      System.out.println(y);
      
	}
}

Add a Comment

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