armstrong number in java w3schools functional approch code example

Example 1: Armstrong number in java

// Check whether a given number is armstrong number or not
import java.util.Scanner;
public class ArmstrongNumber
{
   public static void main(String[] args)
   {
      int x, y, z = 0, temp;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter a number: ");
      x = sc.nextInt();
      temp = x;
      while(x > 0)
      {
         y = x % 10;
         x = x / 10;
         z = z + (y * y * y);
      }
      if(temp == z)
      {
         System.out.println(temp + " is an Armstrong Number.");
      }
      else
      {
         System.out.println(temp + " is not an Armstrong Number.");
      }
      sc.close();
   }
}

Example 2: armstrong number in java

import java.util.Scanner;

public class Armstrong
{
  public static void main(String args[])
  {
    int num,temp,c=0;
  	Scanner in=new Scanner(System.in);
  	num=in.nextInt();
  	temp=num;
  	while(num!=0)
  	{
    	int d=num%10; //extracting last digit
    	c+=d*d*d;
    	num/=10; // removing last digit
  	}
  	if(temp==c)
  	{
    	System.out.println("Number is Armstrong");
  	}
  	else
  		System.out.println("Number is not Armstrong");
  }
}

Tags:

Java Example