Coding & Programming
Quote from Sunil Singh on October 30, 2023, 11:03 amArmstrong number
import java.util.Scanner;
public class ArmstrongNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();
sc.close();int originalNumber = number;
int numDigits = (int) Math.log10(number) + 1;
int sum = 0;while (number > 0) {
int digit = number % 10;
sum += Math.pow(digit, numDigits);
number /= 10;
}if (sum == originalNumber) {
System.out.println(originalNumber + " is an Armstrong number.");
} else {
System.out.println(originalNumber + " is not an Armstrong number.");
}
}
}
Armstrong number
import java.util.Scanner;
public class ArmstrongNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();
sc.close();
int originalNumber = number;
int numDigits = (int) Math.log10(number) + 1;
int sum = 0;
while (number > 0) {
int digit = number % 10;
sum += Math.pow(digit, numDigits);
number /= 10;
}
if (sum == originalNumber) {
System.out.println(originalNumber + " is an Armstrong number.");
} else {
System.out.println(originalNumber + " is not an Armstrong number.");
}
}
}