An Armstrong number (also called a plus perfect number or narcissistic number) is a number equal to the sum of the nth power of the digits, where n is the quantity of digits in the number.
This Java program checks whether a given number is an Armstrong number or not. An Armstrong number is a number that is equal to the sum of the cubes of its digits. The program uses the checkArmstrong() method to check whether a given number is an Armstrong number or not. This method calculates the sum of the cubes of the digits of the given number and compares it with the original number. If both are equal, the method returns true, otherwise it returns false. The main method of the program takes a number as input from the user and checks whether it is an Armstrong number or not by calling the checkArmstrong() method. The program uses the Scanner class to take input from the user.
import java.util.Scanner;
public class ArmstrongNumber {
private static boolean checkArmstrong(int number) {
int temp, remainder, i, counter = 0, result = 0, power;
temp = number;
while (temp != 0) {
temp /= 10;
counter++;
}
temp = number;
while (temp != 0) {
remainder = temp % 10;
power = 1;
i = counter;
while (i != 0) {
power *= remainder;
i--;
}
result += power;
temp /= 10;
}
if (number == result) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int number;
System.out.print("Enter a number to check for armstrong: ");
number = reader.nextInt();
reader.close();
if (checkArmstrong(number)) {
System.out.print(number + " is an armstrong number");
} else {
System.out.print(number + " is not an armstrong number");
}
}
}
Enter a number to check if armstrong number: 8208
8208 is an armstrong number
The code generates the armstrong number series for a given upper bound. An armstrong number is a number that is equal to the sum of cubes of its digits.
The code starts with the definition of a function named checkArmstrong(int number)
. This function takes an integer number
as an argument and returns a boolean value indicating whether number
is an armstrong number or not.
The function first initializes some variables such as temp
, remainder
, i
, counter
, result
, and power
with initial values. The temp
variable is initialized with the value of number
. The counter
is initialized with 0, which will be used to count the number of digits in number
.
The first while loop is used to count the number of digits in number
by dividing temp
by 10 and incrementing the value of counter
in each iteration until temp
becomes 0. The temp
is then re-initialized with the value of number
.
The second while loop is used to calculate the sum of cubes of digits of number
. The remainder
is calculated as the remainder of temp
divided by 10, which is the last digit of temp
. The power
is then calculated as the cube of remainder
by initializing i
with the value of counter
and decrementing the value of i
in each iteration of inner while loop. The inner while loop continues until i
becomes 0. The result
is then updated with the value of result
plus power
. The temp
is then updated by dividing it by 10.
The final step of the function is to check whether number
is equal to result
. If number
is equal to result
, then the function returns true
, otherwise it returns false
.
The main function starts with the creation of a Scanner
object named reader
that is used to take input from the user. A variable max
is then declared to store the upper bound for the armstrong number series.
The user is then prompted to enter the upper bound to find armstrong numbers. The value entered by the user is stored in max
.
The main function then uses a for loop to generate the armstrong number series. The loop starts from 1
and continues until i
becomes greater than max
. In each iteration, the function checkArmstrong(i)
is called, which returns a boolean value indicating whether i
is an armstrong number or not. If i
is an armstrong number, it is printed on the console.
Finally, the Scanner
object is closed to release any resources it is holding.
import java.util.Scanner;
public class ArmstrongNumberSeries {
private static boolean checkArmstrong(int number) {
int temp, remainder, i, counter = 0, result = 0, power;
temp = number;
while (temp != 0) {
temp /= 10;
counter++;
}
temp = number;
while (temp != 0) {
remainder = temp % 10;
power = 1;
i = counter;
while (i != 0) {
power *= remainder;
i--;
}
result += power;
temp /= 10;
}
if (number == result) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int max;
System.out.print("Enter the upper bound to find armstrong numbers: ");
max = reader.nextInt();
reader.close();
System.out.println("Armstrong Number Series:");
for (int i = 1; i <= max; i++) {
if (checkArmstrong(i)) {
System.out.print(i + " ");
}
}
}
}
Enter the upper bound to find armstrong numbers: 1000
Armstrong Number Series:
1 2 3 4 5 6 7 8 9 153 370 371 407