-
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.
Merge pull request #179 from Sbiswas001/main
Create ArmstrongNumber.java
- Loading branch information
Showing
1 changed file
with
79 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,79 @@ | ||
package com.sbiswas001.twelveproject; | ||
|
||
import java.util.Scanner; | ||
|
||
/** | ||
* Checks if a number is armstrong or not. | ||
* A number is armstrong if the sum of each digit to the power | ||
* number of digits is equal to the number itself. | ||
* @author Sayan Biswas | ||
* @version 25.04.2022 | ||
*/ | ||
public class ArmstrongNumber { | ||
/** | ||
* Stores the number | ||
*/ | ||
private int number; | ||
|
||
/** | ||
* Initializes instance variables | ||
*/ | ||
private ArmstrongNumber() { | ||
number = 0; | ||
} | ||
|
||
/** | ||
* Inputs a number from user | ||
*/ | ||
private void input() { | ||
Scanner sc = new Scanner(System.in); | ||
System.out.print("Enter number: "); | ||
number = Integer.parseInt(sc.nextLine()); | ||
} | ||
|
||
/** | ||
* Returns number of digits of a number | ||
* @param n Number to be checked | ||
* @return Number of digits | ||
*/ | ||
private int numberOfDigits(int n) { | ||
return (int)Math.floor(Math.log10(n))+1; | ||
} | ||
|
||
/** | ||
* Checks if a number is armstrong or not. | ||
* A number is armstrong if the sum of each digit to the | ||
* power number of digits is equal to the number itself. | ||
* @param a Number to be checked | ||
* @return true or false | ||
*/ | ||
private boolean armstrongCheck(int a) { | ||
int sum = 0; | ||
while(a > 0) { | ||
sum += (int)Math.pow(a % 10, | ||
numberOfDigits(number)); | ||
a /= 10; | ||
} | ||
return sum == number; | ||
} | ||
|
||
/** | ||
* Displays if number is armstrong or not | ||
*/ | ||
private void display() { | ||
|
||
System.out.println(armstrongCheck(number) ? | ||
"Number is armstrong." : | ||
"Number is not armstrong."); | ||
} | ||
|
||
/** | ||
* Calls other methods | ||
* @param args Arguments passed to main method | ||
*/ | ||
public static void main(String[] args) { | ||
ArmstrongNumber ob = new ArmstrongNumber(); | ||
ob.input(); | ||
ob.display(); | ||
} | ||
} |