BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to Check Armstrong Number

By Chaitanya Singh | Filed Under: Java Examples

Here we will write a java program that checks whether the given number is Armstrong number or not. We will see the two variation of the same program. In the first program we will assign the number in the program itself and in second program user would input the number and the program will check whether the input number is Armstrong or not.

Before we go through the program, lets see what is an Armstrong number. A number is called Armstrong number if the following equation holds true for that number:

xy..z = xn + yn+.....+ zn

where n denotes the number of digits in the number

For example this is a 3 digit Armstrong number

370 = 33 + 73 + o3
         = 27 + 343 + 0
         = 370

Let’s write this in a program:
To understand this Program you should have the knowledge of following Java Programming topics:

  1. Java while loop
  2. Java if..else-if

Example 1: Program to check whether the given number is Armstrong number

public class JavaExample {

    public static void main(String[] args) {

        int num = 370, number, temp, total = 0;

        number = num;
        while (number != 0)
        {
            temp = number % 10;
            total = total + temp*temp*temp;
            number /= 10;
        }

        if(total == num)
            System.out.println(num + " is an Armstrong number");
        else
            System.out.println(num + " is not an Armstrong number");
    }
}

Output:

370 is an Armstrong number

In the above program we have used while loop, However you can also use for loop. To use for loop replace the while loop part of the program with this code:

for( ;number!=0;number /= 10){
    temp = number % 10;
    total = total + temp*temp*temp;
}

Example 2: Program to check whether the input number is Armstrong or not

import java.util.Scanner;
public class JavaExample {

    public static void main(String[] args) {

        int num, number, temp, total = 0;
        System.out.println("Ënter 3 Digit Number");
        Scanner scanner = new Scanner(System.in);
        num = scanner.nextInt();
        scanner.close();
        number = num;

        for( ;number!=0;number /= 10)
        {
            temp = number % 10;
            total = total + temp*temp*temp;
        }

        if(total == num)
            System.out.println(num + " is an Armstrong number");
        else
            System.out.println(num + " is not an Armstrong number");
    }
}

Output:

Ënter 3 Digit Number
371
371 is an Armstrong number

Check out the related programs:

  1. Java Program to check Prime Number
  2. Java Program to check Leap Year
  3. Java Program to check Even or Odd number

Leave a Reply Cancel reply

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

Programs

  • C Programs
  • Java Programs
  • C++ Programs

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap