Program:
Write a program to find the given number is Armstrong number or not?
Description:
Armstrong numbers are the sum of their own digits to the power of
the number of digits. It is also known as narcissistic numbers.
Code:
package com.java2novice.algos;
public class MyArmstrongNumber {
public boolean isArmstrongNumber(int number){
int tmp = number;
int noOfDigits = String.valueOf(number).length();
int sum = 0;
int div = 0;
while(tmp > 0)
{
div = tmp % 10;
int temp = 1;
for(int i=0;i<noOfDigits;i++){
temp *= div;
}
sum += temp;
tmp = tmp/10;
}
if(number == sum) {
return true;
} else {
return false;
}
}
public static void main(String a[]){
MyArmstrongNumber man = new MyArmstrongNumber();
System.out.println("Is 371 Armstrong number? "+man.isArmstrongNumber(371));
System.out.println("Is 523 Armstrong number? "+man.isArmstrongNumber(523));
System.out.println("Is 153 Armstrong number? "+man.isArmstrongNumber(153));
}
}
Output:
Is 371 Armstrong number? true
Is 523 Armstrong number? false
Is 153 Armstrong number? true
Write a program to find the given number is Armstrong number or not?
Description:
Armstrong numbers are the sum of their own digits to the power of
the number of digits. It is also known as narcissistic numbers.
Code:
package com.java2novice.algos;
public class MyArmstrongNumber {
public boolean isArmstrongNumber(int number){
int tmp = number;
int noOfDigits = String.valueOf(number).length();
int sum = 0;
int div = 0;
while(tmp > 0)
{
div = tmp % 10;
int temp = 1;
for(int i=0;i<noOfDigits;i++){
temp *= div;
}
sum += temp;
tmp = tmp/10;
}
if(number == sum) {
return true;
} else {
return false;
}
}
public static void main(String a[]){
MyArmstrongNumber man = new MyArmstrongNumber();
System.out.println("Is 371 Armstrong number? "+man.isArmstrongNumber(371));
System.out.println("Is 523 Armstrong number? "+man.isArmstrongNumber(523));
System.out.println("Is 153 Armstrong number? "+man.isArmstrongNumber(153));
}
}
Output:
Is 371 Armstrong number? true
Is 523 Armstrong number? false
Is 153 Armstrong number? true
Here's a simple Java program to check if a given number is an Armstrong number:
import java.util.Scanner;
public class ArmstrongNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
if (isArmstrong(number)) {
System.out.println(number + " is an Armstrong number.");
} else {
System.out.println(number + " is not an Armstrong number.");
}
scanner.close();
}
public static boolean isArmstrong(int number) {
int sum = 0;
int originalNumber = number;
int digits = String.valueOf(number).length(); // Calculate number of digits
while (number > 0) {
int digit = number % 10; // Get last digit
sum += Math.pow(digit, digits); // Raise to the power of the number of digits
number /= 10; // Remove last digit
}
return sum == originalNumber; // Check if the sum equals the original number
}
}
Explanation:
- Input: The program takes an integer input from the user.
- Armstrong Check:
- It calculates the number of digits in the number.
- It extracts each digit, raises it to the power of the number of digits, and sums these values.
- Output: Finally, it compares the sum to the original number and prints whether it is an Armstrong number.
Example:
- For input
153
, the output will be153 is an Armstrong number.
because . - For input
123
, the output will be123 is not an Armstrong number.
because .