64.8k views
1 vote
Write the function isOdd()that takes a single parameter n1as input and returns the Boolean value Trueif n is an odd integer and Falseotherwise. Note that the parameter may be any type, but the return value is False whenever it is not an int.

User Claud
by
5.7k points

1 Answer

6 votes

Answer:

public static boolean isOdd(int n1){

if (n1%2==0){

System.out.println("The number is even");

return true;

}

else{

System.out.println("The number is odd");

return false;

}

}

Step-by-step explanation:

Using Java program language;

Declare the method fix the return type to be boolean

use the modulo operator (%) to check if even since (n%evenNo is equal to 0)

return true is even or false if not

See a complete program with user prompt using the scanner class below

import java.util.Scanner;

public class TestClock {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.println("Enter any Integer Number");

int n = in.nextInt();

isOdd(n);

}

public static boolean isOdd(int n1){

if (n1%2==0){

System.out.println("The number is even");

return true;

}

else{

System.out.println("The number is odd");

return false;

}

}

}

User Aviah Laor
by
5.4k points