Pages

Saturday, November 14, 2020

Java Example Program to Check Armstrong Number

1. Overview

In this tutorial, you'll learn how to check the given number is armstrong or not. 

This is a commonly asked interview questions for freshers to check the basic logical skill and programming skills.

This is easy to understand if you know the following simple concepts in java.

Java Example Program to Check Armstrong Number

For Loops examples

If Condition examples

A positive integer is called an Armstrong number of order n if

abcd... = an + bn + cn + dn + ...

In case of an Armstrong number of 3 digits, the sum of cubes of each digits is equal to the number itself. For example:

153 = 1*1*1 + 5*5*5 + 3*3*3  // 153 is an Armstrong number.

2. Example - Check Number Armstrong


Let us look the below program that checks the any given number is armstrong or not.

First, Created a number integer variable to store the input number.
Next, created another integer variable to store the original number because we are going to modify the number variable in the next course of steps.
Apart from above two variable, declared another double variable subCubic to store the sum of cubic value for each digit.

Next, need to run the while loop untill the number > 0 and take the each digit to find the cubic value of the digit. Cubic can be calculated using Math.pow() or custom method to calculate the power of a number.

Add the cubic value of each digit to the sumCubic.

Finally, compare the sumCubic and originalNumber. If these two are same then it is a armstrong number else not armstrong.
public class ArmstrongNumberExample {

	public static void main(String[] args) {

		// storing the input number
		int number = 153;
		
		// original number
		int originalNumber = number;

		// cubic sum of each digit
		double sumCubic = 0;
		
		// getting cubic sum of each digit and adding to sumCubic varaible
		while (number > 0) {
			int digit = number % 10;
			sumCubic = sumCubic + Math.pow(digit, 3);
			number = number / 10;

		}
		
		// comparing the original value and result sumCubic
		if(originalNumber == sumCubic) {
			System.out.println(originalNumber+" number is armstrong");
		} else {
			System.out.println(originalNumber+" number is not armstrong");
		}

	}

}
Output:
153 number is armstrong

3. Example - Check Number Armstrong for Order n


If the number is not having the 3 digits then first you need to get the count of digits in the number.

Next, apply the same logic to calculate the power of order n and add all of these power result to the result variable.

Finally, compare the original number and result number. If both are same then input number is armstrong else not.
public class ArmstrongNumberOrderNExample {

	public static void main(String[] args) {

		// storing the input number
		int number = 1634;

		// original number
		int originalNumber = number;

		// cubic sum of each digit
		double sumCubic = 0;

		// finding the length of number
		int length = 0;
		while (number > 0) {

			number = number / 10;
			length++;
		}
		
		// getting cubic sum of each digit and adding to sumCubic varaible
		while (number > 0) {
			int digit = number % 10;
			sumCubic = sumCubic + Math.pow(digit, length);
			number = number / 10;

		}

		// comparing the original value and result sumCubic
		if (originalNumber == sumCubic) {
			System.out.println(originalNumber + " number is armstrong");
		} else {
			System.out.println(originalNumber + " number is not armstrong");
		}

	}

}
Output
1634 number is not armstrong

4. Conclusion


In this article, you've seen armstrong number checking for order 3 and order n with example programs.



No comments:

Post a Comment

Please do not add any spam links in the comments section.