Java program to check armstrong number [2 ways] - codeyourslf

How we can write a java program to check a number is armstrong or not ? codeYourslf - a programming blog. Java for beginners

check armstrong number [2 ways] - codeyourslf

Java program to check armstrong number [2 ways] - codeyourslf

Do you know what is armstrong number ? Let me tell you, a number which is equal to the sum of cubes of its individual digits, then that number will be called as armstrong number.

  • Ex :- Let take a number 153.

Here we have to find the cubes of its digits.

  • (1*1*1)=1  
  • (5*5*5)=125  
  • (3*3*3)=27  

The sum of cubes of digits will be

  • Sum = 1 + 125 + 27 = 153

Here you can see both are same, so 153 is an armstrong number.

How we can write a java program to check a number is armstrong or not ? In this article I will discuss 2 different approaches to check whether a given number is armstrong or not.

Hey I write code using my mobile phone, I use java N-IDE to write code for every programs that you see in this blog codeYourslf. Please support me. 

Approach:-1 "check armstrong number in java using if else"


import java.util.Scanner;
public class ArmstrongNumUsingTernary {

  public static void main(String[] args) {

          Scanner sc = new Scanner(System.in);

    System.out.println(" Enter the number ");

    int num = sc.nextInt();

    int temp = num;

    int ArmSum = 0;

    while (num > 0) {

      int lastDigit = num % 10;

      ArmSum = ArmSum + (lastDigit * lastDigit * lastDigit);

      num = num / 10;

    }

    

    if(temp == ArmSum)

    {

      System.out.println("It is armstrong number");

    }

    else

    {

      System.out.println("Not an armstrong number");

    }

  }

}

 
+
Explanation

As I mentioned above armstrong numbers are equal to the sum of cubes of it's digits. 

So first we have to find the cube sum of digits and store it to a variable. 

Now using if else we can check whether that sum is equal to the original number or not. If it matches then the number will be armstrong. 

How to find the sum of cubes fo digits

First we have to access the digits of that number one by one, we can do that using modulus operator (%). num % 10 will return the last digit of num. 

Now we can find cube by multiplying 3 times to that last digit. 

The work with last digit is done, so we have to remove it from num by dividing that num with 10, i.e num / 10. 

Now we have to repeat the same process to find the cube for second last digits. We will continue that process till the first digit of the number. 

Finally we add these cube of digits and store it to a new variable(ArmSum). We have to do all the process with a loop.

Output

Java program to check armstrong number [2 ways] - codeyourslf

Approach:-2 "java program to find armstrong number using ternary operator"

import java.util.Scanner;

public class ArmstrongNum {

  public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    System.out.println(" Enter the number ");

    int num = sc.nextInt();

    int temp = num;

    int ArmSum = 0;

    while (num > 0) {

      int lastDigit = num % 10;

      ArmSum = ArmSum + (lastDigit * lastDigit * lastDigit);

      num = num / 10;

    }

    String output = (temp == ArmSum) ? "Armstrong Number" : "Not Armstrong";

    System.out.println(output);

  }

}

     
+
Explanation

In this program all the process remain same, only the difference is, I use ternary operator in place of if else statement. 

We can use ternary operator to check the condition.

Output

Java program to check armstrong number [2 ways] - codeyourslf

Hey if you have any questions or any doubt, feel free to write in the comment section, I will definitely answer to your questions.

And if you learn something new from the article please share it to your friends.

Also check out

`

Post a Comment

Hey Please 👏, feel free to share your opinion 🌍
© codeYourslf. All rights reserved. Distributed by Pixabin