Odd/Even Exercise
Additional Exercises
// Warmup
// create a java program to check a number
// to see if it is even or odd
// hint: use parts of last class to help
// 8-10 mins.; don't worry if there are errors
// remember to import java scanner
// hint 2: use parseInt to change string to int
import java.util.Scanner;
class Main {
public static void main(String[] args) {
System.out.println("Enter a number here: ");
Scanner sc = new Scanner(System.in);
int number = Integer.parseInt(sc.nextLine());
int x = number % 2;
if(x==0){
System.out.println("The number is even");
}
else {
System.out.println("The number is odd");
}
}
}
// Arrays
// What are arrays?
// store multiple values in a single variable
// put same type data into one part
// Arrays in Java are also objects. Need to be
// declared and then created. In order to declare a variable
// that will hold an array of integers, we use the following syntax:
int[] arr;
arr = new int[34];
// this creates a new array with the size of 34
// Exercise: create three new arrays with three dif. sizes
// check an array's size by printing it:
// System.out.println(arr.length);
arr[0] = 4;
arr[1] = arr[0] + 5;
int[] arr = {1, 2, 3, 4, 5}
//Loops
for (int i = 0; i < 3; i++) {}
//write out what the code will output,
//what would be running
int i = 0;
i < 3 //0 < 3 T
i++ // adds 1 to 0
i < 3 // 1 < 3 T
i++ //adds 1 to 1
i < 3//2 < 3 T
i++ //ads 1 to 2
i < 3// 3 < 3 F
//Break out of the loop
//for loop exercise:
//create a java program that loops through
//a list and prints out only the even numbers
//create an array with numbers
//i.e. int[] numbers = {numbers in here};
// do the public class main and public static void
public class Main {
public static void main(String[] args) {
int[] numbers = {
2, 3, 5, 8, 19, 45, 88, 60, 7
};
for (int i = 0; i < numbers.length; i++) {
int a = numbers[i];
if (a % 2 == 0) {
System.out.println(a);
}
}
}
}
//While
//while(condition) {}