This file contains hidden or 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
/** | |
* @param n >= 1 | |
* @return product of integers from 1 to n (including 1 and n). | |
*/ | |
public static int factorial(int n) { | |
int result = 1; //fixed initial value of result | |
for(int i=1; i <= n; i++) { | |
result = result * i; | |
} | |
} |
This file contains hidden or 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
/** | |
* @param n >= 1 | |
* @return product of integers from 1 to n (including 1 and n). | |
*/ | |
public static int factorial(int n) { | |
int result = 1; //fixed initial value of result | |
for(int i=1; i < n; i++) { | |
result = result * i; | |
} | |
} |
This file contains hidden or 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
/** | |
* @param n >= 1 | |
* @return product of integers from 1 to n (including 1 and n). | |
*/ | |
public static int factorial(int n) { | |
int result = 0; | |
for(int i=1; i < n; i++) { | |
result = result * i; | |
} | |
} |
This file contains hidden or 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
public static boolean isPositive(int n) { | |
if(n > 0) | |
return true; | |
else | |
return false; | |
} |
This file contains hidden or 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
boolean isPositive(int n) { | |
if(n > 0) | |
return true; | |
else | |
return false; | |
} |
This file contains hidden or 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
public static void main(String[] args) { | |
int num = 56; | |
boolean np = isPrime(num); | |
System.out.println(np); | |
} |
This file contains hidden or 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
public static boolean isPrime(int n) { | |
if(n < 2) { | |
return false; | |
} | |
for(int i=2; i < n; i++) { | |
if(n % i == 0) { // violation, CANNOT be prime, no need to check further | |
return false; | |
} | |
} | |
//end of loop, no non-trivial divisor found |
This file contains hidden or 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
public static boolean containsSpace(String s) { | |
for(int i=0; i < s.length(); i++) { | |
if(s.charAt(i) == ' ') { //validation, no need to check further | |
return true; | |
} | |
} | |
//end of loop, no character is a space | |
return false; | |
} |
This file contains hidden or 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
public static int total(int[] a) { | |
int result = 0; | |
for(int i=0; i < a.length; i++) { | |
result+=a[i]; | |
} | |
return result; | |
} | |
public static void main(String[] args) { | |
int[] taxicab = {1, 7, 2, 9}; |
This file contains hidden or 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
public static int totalFirstHalfNegatives(int[] a) { | |
int result = 0; | |
for(int i=0; i < a.length/2; i++) { | |
if(a[i] < 0) { | |
result+=a[i]; | |
} | |
} | |
return result; | |
} |