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)
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 TestsWe will see the illustration with the help of examples.
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"); } }
Output:
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.
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"); } } }
Output:
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.
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"); } } }
Output:
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.
この記事では、アームストロング数の仕組みと定義について見てきました。まず、入力された数値がアームストロング数値であるかどうかを確認します。次に、150 から 160 までの範囲の値を入力し、それらの値の間にアームストロング数がいくつあるかを確認します。 3 番目に、365 から 375 までの範囲の数値を入力し、370 と 371 がアームストロング数であることがわかります。アームストロング数は数論で使用される特別な数であり、いくつかの数値の桁の性質とその 3 乗の合計を見つけるために使用できます。
The above is the detailed content of Armstrong Number in Java. For more information, please follow other related articles on the PHP Chinese website!