forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
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 TheAlgorithms#240 from manimanasamylavarapu/add-al…
…ogos-patch2-kadane-algo Added Armstrong number algorithm.
- Loading branch information
Showing
1 changed file
with
47 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,47 @@ | ||
import java.util.Scanner; | ||
|
||
/** | ||
* A utility to check if a given number is armstrong or not. Armstrong number is | ||
* a number that is equal to the sum of cubes of its digits for example 0, 1, | ||
* 153, 370, 371, 407 etc. For example 153 = 1^3 + 5^3 +3^3 | ||
* | ||
* @author mani manasa mylavarapu | ||
* | ||
*/ | ||
public class Armstrong { | ||
public static void main(String[] args) { | ||
Scanner scan = new Scanner(System.in); | ||
System.out.println("please enter the number"); | ||
int n = scan.nextInt(); | ||
boolean isArmstrong = checkIfANumberIsAmstrongOrNot(n); | ||
if (isArmstrong) { | ||
System.out.println("the number is armstrong"); | ||
} else { | ||
System.out.println("the number is not armstrong"); | ||
} | ||
} | ||
|
||
/** | ||
* Checks whether a given number is an armstrong number or not. Armstrong | ||
* number is a number that is equal to the sum of cubes of its digits for | ||
* example 0, 1, 153, 370, 371, 407 etc. | ||
* | ||
* @param number | ||
* @return boolean | ||
*/ | ||
public static boolean checkIfANumberIsAmstrongOrNot(int number) { | ||
int remainder, sum = 0, temp = 0; | ||
temp = number; | ||
while (number > 0) { | ||
remainder = number % 10; | ||
sum = sum + (remainder * remainder * remainder); | ||
number = number / 10; | ||
} | ||
if (sum == temp) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
|
||
} | ||
} |