0% found this document useful (0 votes)
29 views3 pages

Write A Program To Check Whether A Number Is Even or Odd

The document contains two Java programs: the first checks if a number is even or odd, the second finds the maximum of two numbers. Both programs take user input, perform conditional checks, and output the results.

Uploaded by

Castro Osei Wusu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views3 pages

Write A Program To Check Whether A Number Is Even or Odd

The document contains two Java programs: the first checks if a number is even or odd, the second finds the maximum of two numbers. Both programs take user input, perform conditional checks, and output the results.

Uploaded by

Castro Osei Wusu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1. Write a program to check whether a number is even or odd.

Solution
Import java.util.Scanner;

public class Main {


public static void main (String[] args) {

// Declaring Variables
Int num; // variable to store the input number
Boolean isEven; // variable to store whether the number is even or odd

// creating scanner class object


Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number: "); // prompt user to enter a number


num = scanner.nextInt(); // read the user input and store it in num

// check if number is even or odd


if (num % 2 == 0) {
isEven = true; // set isEven to true if the number is even
} else {
isEven = false; // set isEven to false if the number is odd
}

// print the result


if (isEven) {
System.out.println(num + " is even.");
} else {
System.out.println(num + " is odd.");
}
}
}

2. Write a program to find maximum between two numbers.

Import java.util.Scanner;

public class Main {


public static void main (String[] args) {

// Declaring Variables
Int num1; // variable to store the first number
Int num2; // variable to store the second number
Int max; // variable to store the maximum number

// creating scanner class object


Scanner scanner = new Scanner(System.in);

System.out.print("Enter the first number: "); // prompt user to enter the first number
num1 = scanner.nextInt(); // read the user input and store it in num1

System.out.print("Enter the second number: "); // prompt user to enter the second number
num2 = scanner.nextInt(); // read the user input and store it in num2

// find the maximum number


if (num1 > num2) {
max = num1; // set max to num1 if it is greater than num2
} else {
max = num2; // set max to num2 if it is greater than num1
}

// print the result


System.out.println("The maximum number is: " + max);
}
}

You might also like