Skip to content

Commit 2fb6202

Browse files
authored
added total of factors
1 parent 5ae4b25 commit 2fb6202

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Diff for: NumOfFactors/NumOfFactors.java

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
public class NumOfFactors {
3+
4+
5+
public static void main(String[] args) {
6+
7+
long number=10;
8+
int factors=getNumOfFactors(number);
9+
10+
System.out.println(String.format("Total factors of %d are: %d",number,factors));
11+
}
12+
13+
private static int getNumOfFactors(long num) {
14+
15+
int totalFactors=1;//accounts for the number itself
16+
int increment=1;
17+
if(num%2==1) {//if number is odd, it can never have an even factor
18+
increment=2;
19+
}
20+
21+
for(long numFac=1; numFac<num ; numFac+=increment) {
22+
if(num%numFac==0) {
23+
totalFactors+=1;
24+
}
25+
}
26+
return totalFactors;
27+
}
28+
}

0 commit comments

Comments
 (0)