Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Armstrong number #2217

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions armstrongnumber
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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();
scanner.close();

if (isArmstrongNumber(number)) {
System.out.println(number + " is an Armstrong number.");
} else {
System.out.println(number + " is not an Armstrong number.");
}
}

// Function to check if a number is an Armstrong number
public static boolean isArmstrongNumber(int num) {
int originalNumber, remainder, result = 0;
int n = 0;

originalNumber = num;

// Find the number of digits in the number
for (originalNumber = num; originalNumber != 0; originalNumber /= 10, ++n);

originalNumber = num;

// Calculate the result
while (originalNumber != 0) {
remainder = originalNumber % 10;
result += Math.pow(remainder, n);
originalNumber /= 10;
}

// Check if num is equal to the result
return result == num;
}
}