Skip to content

Instantly share code, notes, and snippets.

@Spotlightsrule
Created October 24, 2018 19:23
Show Gist options
  • Save Spotlightsrule/766081f4ccaeebf6d5c5f3a03da3f062 to your computer and use it in GitHub Desktop.
Save Spotlightsrule/766081f4ccaeebf6d5c5f3a03da3f062 to your computer and use it in GitHub Desktop.
Evens and Odds From Two Numbers
import java.util.ArrayList;
public class EveryOther {
public static ArrayList<Integer> everyOdd(int sNum, int eNum){
//Make the arraylist to hold the numbers
ArrayList<Integer> oddArr = new ArrayList<>();
//Start looping through the provided range of numbers
for(int i=sNum; i<=eNum; i++){
//Check if the number isn't divisible by two
if(i%2 != 0){
oddArr.add(i);
}
}
//Return the resulting arraylist as a primitive array of ints via stream ( >= JAVA SE 1.8 is required)
//return eooArr.stream().mapToInt(i -> i).toArray();
return oddArr;
}
public static ArrayList<Integer> everyEven(int sNum, int eNum){
//Make the arraylist to hold the numbers
ArrayList<Integer> evenArr = new ArrayList<>();
//Start looping through the provided range of numbers
for(int i=sNum; i<=eNum; i++){
//Check if the number is divisible by two
if(i%2 == 0){
evenArr.add(i);
}
}
//Return the resulting arraylist as a primitive array of ints via stream ( >= JAVA SE 1.8 is required)
//return eooArr.stream().mapToInt(i -> i).toArray();
return evenArr;
}
public static ArrayList<Integer> everyOtherOdd(int sNum, int eNum){
//Make the arraylist to hold the numbers
ArrayList<Integer> eooArr = new ArrayList<>();
//Start looping through the provided range of numbers
for(int i=(sNum-2); i<(eNum-1); i++){
//Check if the number isn't divisible by two
if(i%2 != 0){
//Check if the number two places ahead isn't divisible by 0
if(((i+=2)%2) != 0){
//Add the number to the arraylist
eooArr.add(i);
}
}
}
//Return the resulting arraylist as a primitive array of ints via stream ( >= JAVA SE 1.8 is required)
//return eooArr.stream().mapToInt(i -> i).toArray();
return eooArr;
}
public static ArrayList<Integer> everyOtherEven(int sNum, int eNum){
//Make the arraylist to hold the numbers
ArrayList<Integer> eoeArr = new ArrayList<>();
//Start looping through the provided range of numbers
for(int i=(sNum-2); i<(eNum-1); i++){
//Check if the number is divisible by two
if(i%2 == 0){
//Check if the number two places ahead is divisible by 0
if(((i+=2)%2) == 0){
//Add the number to the arraylist
eoeArr.add(i);
}
}
}
//Return the resulting arraylist as a primitive array of ints via stream ( >= JAVA SE 1.8 is required)
//return eooArr.stream().mapToInt(i -> i).toArray();
return eoeArr;
}
public static void main(String[] args){
int x = 0;
int y = 101;
System.out.println("Original Numbers: " + x + " and " + y + "\n");
System.out.println("Every Odd: " + everyOdd(x,y));
System.out.println("Every Even: " + everyEven(x,y));
System.out.println("Every Other Odd: " + everyOtherOdd(x,y));
System.out.println("Every Other Even: " + everyOtherEven(x,y));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment