-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: menambahkan contoh angka armstrong (#176)
Signed-off-by: slowy07 <slowy.arfy@proton.me>
- Loading branch information
Showing
3 changed files
with
51 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
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,31 @@ | ||
package maths; | ||
|
||
public class AngkaArmstrong { | ||
/** | ||
* Mengecek angka yang dimana angka tersebut angka itu armstrong atau tidak | ||
* | ||
* @param angka angka yang di cek | ||
* @return {@code true} jika nilai yang diberikan adalah angka amstrong | ||
*/ | ||
public boolean adalahArmstrong(int angka) { | ||
long hasil = 0; | ||
String temp = Integer.toString(angka); | ||
int pangkat = temp.length(); | ||
long angkaOriginal = angka; | ||
|
||
// perulangan akan berjalan selama angkaOriginal lebih besar dari 0 | ||
while (angkaOriginal > 0) { | ||
// mengambil digit terakhir dari angkaOriginal | ||
long digit = angkaOriginal % 10; | ||
// menambahkan hasil pangkat dari digit ke variabel 'hasil' | ||
// Math.pow() digunakan ini untuk menghitung digit pangkat | ||
hasil += (long) Math.pow(digit, pangkat); | ||
// menghapus digit terakhir dari angkaOriginal | ||
angkaOriginal /= 10; | ||
} | ||
|
||
// membandingkan hasil dengan angka asli | ||
// jika sama maka angka adalah bilangan armstrong | ||
return hasil == angka; | ||
} | ||
} |
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,14 @@ | ||
package maths; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class AngkaArmstrongTest { | ||
@Test | ||
void testAdalahAngkaArmstrong() { | ||
AngkaArmstrong armstrong = new AngkaArmstrong(); | ||
assertThat(armstrong.adalahArmstrong(0)).isTrue(); | ||
assertThat(armstrong.adalahArmstrong(200)).isFalse(); | ||
assertThat(armstrong.adalahArmstrong(371)).isTrue(); | ||
} | ||
} |