Skip to content

Commit 47c44aa

Browse files
author
Mani Manasa Mylavarapu
committed
Added Armstrong number algorithm.
fixes TheAlgorithms#96
1 parent 598783d commit 47c44aa

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Others/Armstrong.java

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package Others;
2+
3+
import java.util.Scanner;
4+
/**
5+
* To check if a given number is armstrong or not.
6+
* @author mani manasa mylavarapu
7+
*
8+
*/
9+
public class Armstrong {
10+
public static void main(String[] args) {
11+
Scanner scan = new Scanner(System.in);
12+
System.out.println("please enter the number");
13+
int n = scan.nextInt();
14+
boolean isArmstrong = checkIfANumberIsAmstrongOrNot(n);
15+
if(isArmstrong)
16+
{
17+
System.out.println("the number is armstrong");
18+
}
19+
else
20+
{
21+
System.out.println("the number is not armstrong");
22+
}
23+
}
24+
25+
public static boolean checkIfANumberIsAmstrongOrNot(int number) {
26+
int remainder, sum = 0,temp=0;
27+
temp=number;
28+
while (number > 0) {
29+
remainder = number % 10;
30+
sum = sum + (remainder * remainder * remainder);
31+
number = number / 10;
32+
}
33+
if (sum == temp) {
34+
return true;
35+
} else {
36+
return false;
37+
}
38+
39+
}
40+
}

0 commit comments

Comments
 (0)