Skip to content

Commit a0fd638

Browse files
authored
Merge pull request TheAlgorithms#240 from manimanasamylavarapu/add-alogos-patch2-kadane-algo
Added Armstrong number algorithm.
2 parents b0155e3 + 6dd5a9c commit a0fd638

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

Others/Armstrong.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import java.util.Scanner;
2+
3+
/**
4+
* A utility to check if a given number is armstrong or not. Armstrong number is
5+
* a number that is equal to the sum of cubes of its digits for example 0, 1,
6+
* 153, 370, 371, 407 etc. For example 153 = 1^3 + 5^3 +3^3
7+
*
8+
* @author mani manasa mylavarapu
9+
*
10+
*/
11+
public class Armstrong {
12+
public static void main(String[] args) {
13+
Scanner scan = new Scanner(System.in);
14+
System.out.println("please enter the number");
15+
int n = scan.nextInt();
16+
boolean isArmstrong = checkIfANumberIsAmstrongOrNot(n);
17+
if (isArmstrong) {
18+
System.out.println("the number is armstrong");
19+
} else {
20+
System.out.println("the number is not armstrong");
21+
}
22+
}
23+
24+
/**
25+
* Checks whether a given number is an armstrong number or not. Armstrong
26+
* number is a number that is equal to the sum of cubes of its digits for
27+
* example 0, 1, 153, 370, 371, 407 etc.
28+
*
29+
* @param number
30+
* @return boolean
31+
*/
32+
public static boolean checkIfANumberIsAmstrongOrNot(int number) {
33+
int remainder, sum = 0, temp = 0;
34+
temp = number;
35+
while (number > 0) {
36+
remainder = number % 10;
37+
sum = sum + (remainder * remainder * remainder);
38+
number = number / 10;
39+
}
40+
if (sum == temp) {
41+
return true;
42+
} else {
43+
return false;
44+
}
45+
46+
}
47+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy