logo
  • Programming
  • Testing
  • AI
  • Devops
  • Data Science
  • Design
  • Blogs
  • Crypto Tools
  • Dev Feed
  • Login
Story
Follow @devglan

Java Program to test if a given number is Armstrong Number or not

author-image   By Dhiraj Ray,   31 December, 2017 8K

Description

Armstrong Number is a number that is the sum of its own digits each raised to the power of the number of digits.For example 371 is an Armstrong number as 3^3+7^3+1^3 = 371.It is also sometimes called as narcissistic number or pluperfect digital invariant (PPDI). Following is the java program to test if a given number is an armstrong number or not.

ArmstrongNumber.java
package com.devglan;

public class ArmstrongNumber {

    public boolean isArmstrongNumber(int number){

        int tmp = number;
        int noOfDigits = String.valueOf(number).length();
        int sum = 0;
        int div = 0;
        while(tmp > 0){
            Line 1 div = tmp % 10;
            int temp = 1;
            for(int i=0; i < noOfDigits; i++){
                temp *= div;
            }
            sum += temp;
            Line 7 tmp = tmp/10;
        }
        if(number == sum) {
            return true;
        } else {
            return false;
        }
    }

    public static void main(String a[]){
        ArmstrongNumber man = new ArmstrongNumber();
        System.out.println("Is 371 Armstrong number? " + man.isArmstrongNumber(371));
        System.out.println("Is 523 Armstrong number? " + man.isArmstrongNumber(523));
    }
}

Explanation

Click to Suggest Your Own Explanation

From line no. 1 to 7 - extract each digit and find it's nth square where n is the length of number and accumulate the sum of each result.Compare this result with the given number to identify if given nmber is an armstrong or not.

Other Similar Java Programs:

1 Java Program to test if a given number is Fibonacci or not
2 java program to find distinct word list from a file
3 Java program to find duplicate character from a string
4 Java Program to find middle index of array where both ends sum is equal
5 Java Program to find line with max character length in descending order in Java
6 Java Program to find max two numbers in an array
7 Java program to find max repeated words from a file
8 Java program to find sum of prime numbers
9 Java program to reverse a given number
10 Java program to find permutations of a given string
11 Java program to reverse a given string
12 Java program to find factorial of a given number
13 Java Program for Binary Search
14 Java Program to Add Two 2D Matrix
15 3 Ways to Check if Given Words are Anagram or not
16 Java Program to Find LCM of a Two Given Number
17 Check Given String is Rotation of Another String
18 Java Program To Check If A Given Number is A Perfect Number
19 Remove Common Characters From Given Strings
20 Find Second Largest Number in Array
21 Java Program To Find the Longest Palindrome Present in a String
22 Java Program to Reverse an Array in Place Without Using Any Second Array
23 Java Program to Print 1 To 10 Without Using Loop
24 Write a Java Program to Compare Files in Java
25 Java Program to Find missing Number in an Array
26 Java Program to Find First non Repeated Character in a String
27 Write a Java Program to Find Union and Intersection of Arrays in Java
28 Writing a Java program to rotate an array by d elements.
29 Write a Java program to rotate a matrix
30 Write a Java program to find the largest sum of the contiguous subarray in a given Array

If You Appreciate This, You Can Consider:

  • Share this article on social media or with your teammates.

Suggest Explanation

The suggestion has been saved for review.Thanks for your effort.

{{errorMessage}}

Vertex

Devglan is one stop platform for all
programming tutorials and courses.

About Us

  • About Us
  • Contact Us
  • Submission Criteria
  • Privacy Policy

Quick Links

  • Home
  • Login / Join
  • Submit Your Story
  • Donate

Contact Us

Dhiraj
dhiraj@devglan.com

© 2021 Devglan. All rights reserved.