Even or Odd Solution

If you got stuck and haven't looked at 'Mathy Mathematicals' article in this book, go do that first and try again with this problem. This is a very standard AP Computer Science A short-answer, and being able to use basic math to solve for certain things, like checking whether a number is even or odd, is crucial for computationally harder math problems.

import java.util.*;
import java.io.*;
import static java.lang.System.*;

public class Main {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);

        int T = sc.nextInt(); //counter for number of data sets
        for (int i = 0; i < T; i++) { //loop through T times
            int N = sc.nextInt(); //take in N as an int

            if (N % 2 == 0) //if you divide by two and there is no remainder
                out.printf("%d is even.\n", N); //N is even
            else //if N is not even then it must be odd
                out.printf("%d is odd.\n", N);
        }
    }
}

Good luck, may Nisreen be with you!

results matching ""

    No results matching ""