Table of Contents
Examples of Armstrong Number in Java
Example #3
Conclusion – Armstrong Number in Java
Home Java javaTutorial Armstrong Number in Java

Armstrong Number in Java

Aug 30, 2024 pm 04:26 PM
java

Armstrong Number in Java Armstrong is one in which the sum of the cubes of the individual digits of the number is equal to the number itself. Armstrong number is a special kind of number where the digits are first picked up, then they are cubed, and finally, all the cubes of the individual digits are added to get a number. If the number thus found is equal to the original number, then the respective number is known as an Armstrong number. An example of Armstrong’s number is 153. If we break down the digits of 153, they are 1, 5 and 3. Then we find the cube of the respective numbers, and finally, we calculate the cube of the numbers.

153= (1*1*1)+(5*5*5)+(3*3*3)
370= (3*3*3)+(7*7*7)+(0*0*0)
Copy after login

In this way, we can calculate whether a number is an Armstrong number or not.

ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock Tests

Examples of Armstrong Number in Java

We will see the illustration with the help of examples.

Example #1

In the coding example, we use Java programming language to determine whether the number is an Armstrong number or not. If the entered number is an Armstrong number, then the program automatically prints it is as an Armstrong number, and if it is not one, it replies automatically that the number is not an Armstrong number. We can enter three-digit or four-digit values to check whether the number is an Armstrong number or not.

The program’s logic is such that each digit of the respective number is stored in the temp variable. Then, the number is cubed to find out the respective digit’s cube, which is stored in another variable total. Finally, the total number is checked with the corresponding original number. The digits are obtained one by one by getting the number divided by 10 at each step, then getting the remainder of the number, and then cubing the number to get the respective digit’s cube.

Code:

import java.io.*;
public class Armstrong
{
public static void main(String[] args)throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a number");
int num = Integer.parseInt(br.readLine());
int number, digit, sum = 0;
number = num;
while (number != 0)
{
digit = number % 10;
sum = sum + digit*digit*digit;
number /= 10;
}
if(sum == num)
System.out.println(num + " is an Armstrong number");
else
System.out.println(num + " is not an Armstrong number");
}
}
Copy after login

Output:

Armstrong Number in Java

Armstrong Number in Java

Armstrong Number in Java

In the first program, we enter the numbers 370 and 153 as numbers to check whether they are Armstrong or not. Also, we enter 269 as a number to check whether the number is Armstrong. We get the program’s respective output that the numbers 370 and 153 are Armstrong numbers while the number 269 is not an Armstrong number.

Example #2

In the second coding example, we select a range of numbers that are checked whether they are Armstrong numbers or not. The range is from 150 to 160. We select the range, and we check the output whether the number is an Armstrong number or not. Then we see the output. The logic used is similar to that of the logic used for finding an Armstrong number. The respective digits of the number are calculated, and then they are cubed and summed to find the final total number. If the final total number is equal to the original number, they are regarded as Armstrong numbers calculated.

Code:

import java.io.*;
public class ArmstrongRange
{
public static void main(String[] args)throws IOException
{
for(int num= 150; num<160; num++)
{
int number, digit, sum = 0;
number = num;
while (number != 0)
{
digit = number % 10;
sum = sum + digit*digit*digit;
number /= 10;
}
if(sum == num)
System.out.println(num + " is an Armstrong number");
else
System.out.println(num + " is not an Armstrong number");
}
}
}
Copy after login

Output:

Armstrong Number in Java

In the sample output, we see that all the numbers in the range of 150 to 160 have been checked for whether they are Armstrong numbers or not. The program has reported that only 153 is an Armstrong number whose sum of cubes of digits is equal to the original number. All the other numbers have been reported as non-Armstrong numbers.

Example #3

In this coding example, we will see the list of Armstrong numbers present between 365 and 375. We are changing the range of the values to be checked for Armstrong numbers. The sample logic of the coding is exactly the same as the previous ones. The main difference is that the range of numbers to be checked is changed, and they are slightly different from the last line of code.

The individual digits are taken, cubed and summed to get a number. If that number is the same as the original number, then the original number is known as an Armstrong number; otherwise, it is not an Armstrong number.

Code:

import java.io.*;
public class ArmstrongRange
{
public static void main(String[] args)throws IOException
{
for(int num= 365; num<375; num++)
{
int number, digit, sum = 0;
number = num;
while (number != 0)
{
digit = number % 10;
sum = sum + digit*digit*digit;
number /= 10;
}
if(sum == num)
System.out.println(num + " is an Armstrong number");
else
System.out.println(num + " is not an Armstrong number");
}
}
}
Copy after login

Output:

Armstrong Number in Java

In the program’s sample output, we see that only 371 and 370 are Armstrong numbers while the other numbers are not as the sum of the cubes of the individual digits does not add up to the original number.

Conclusion – Armstrong Number in Java

In this article, we have seen the working and definition of an Armstrong number. First, we check whether a number entered is an Armstrong number or not. Secondly, we input a range of values from 150 to 160 and check how many Armstrong numbers are there in between those values. Thirdly, we input a range of numbers from 365 to 375 and find out that 370 and 371 are Armstrong numbers. Armstrong numbers are special numbers used in number theory and can be used to find the nature of digits of some numbers and the summation of their cubes.

The above is the detailed content of Armstrong Number in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Square Root in Java Square Root in Java Aug 30, 2024 pm 04:26 PM

Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Armstrong Number in Java Armstrong Number in Java Aug 30, 2024 pm 04:26 PM

Guide to the Armstrong Number in Java. Here we discuss an introduction to Armstrong's number in java along with some of the code.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

See all articles