-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6ad3692
commit 5dbd13b
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 (isArmstrong(number)) { | ||
System.out.println(number + " is an Armstrong number."); | ||
} else { | ||
System.out.println(number + " is not an Armstrong number."); | ||
} | ||
} | ||
|
||
//Check if a number is an Armstrong number | ||
public static boolean isArmstrong(int num) { | ||
int originalNumber, remainder, result = 0, n = 0; | ||
|
||
originalNumber = num; | ||
|
||
//Number of digits | ||
for (originalNumber = num; originalNumber != 0; originalNumber /= 10) { | ||
n++; | ||
} | ||
|
||
originalNumber = num; | ||
|
||
// Sum of digits raised to a certain power of number of digits | ||
while (originalNumber != 0) { | ||
remainder = originalNumber % 10; | ||
result += Math.pow(remainder, n); | ||
originalNumber /= 10; | ||
} | ||
|
||
// Check if Result = Original number entered | ||
return num == result; | ||
} | ||
} |