[Go to site: main page, start]

0% found this document useful (0 votes)
9 views2 pages

Java Number and Eligibility Checkers

The document contains five Java programs that demonstrate basic programming concepts. These include checking if a number is even or odd, determining if a number is positive, negative, or zero, checking voting eligibility based on age, verifying if a year is a leap year, and finding the largest of three numbers. Each program includes a main method and conditional statements to perform the respective checks.

Uploaded by

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

Java Number and Eligibility Checkers

The document contains five Java programs that demonstrate basic programming concepts. These include checking if a number is even or odd, determining if a number is positive, negative, or zero, checking voting eligibility based on age, verifying if a year is a leap year, and finding the largest of three numbers. Each program includes a main method and conditional statements to perform the respective checks.

Uploaded by

Tesu Rathia
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

​ Even or Odd Number Checker


public class EvenOddChecker {
public static void main(String[] args) {
int number = 10;
if (number % 2 == 0) {
[Link](number + " is even");
} else {
[Link](number + " is odd");
}
}
}

2.​Positive, Negative, or Zero


public class NumberSignChecker {
public static void main(String[] args) {
int number = 0;
if (number > 0) {
[Link]("Number is positive.");
} else if (number < 0) {
[Link]("Number is negative.");
} else {
[Link]("Number is zero.");
}
}
}

3.​Age Eligibility for Voting


public class VotingEligibility {
public static void main(String[] args) {
int age = 17;
if (age >= 18) {
[Link]("Eligible to vote.");
} else {
[Link]("Not eligible to vote.");
}
}
}

4.​ Leap Year Checker


public class LeapYearChecker {
public static void main(String[] args) {
int year = 2000;
boolean isLeapYear = false;
if (year % 4 == 0) {
isLeapYear = true;
if (year % 100 == 0 && year % 400 != 0) {
isLeapYear = false;
}
}
if (isLeapYear) {
[Link](year + " is a leap year");
} else {
[Link](year + " is not a leap year");
}
}
}

5.​ Java Program: Find Largest of Three Numbers


public class LargestNumber {
public static void main(String[] args) {
int num1 = 10, num2 = 20, num3 = 7;

if (num1 >= num2 && num1 >= num3) {


[Link](num1 + " is the largest number.");
} else if (num2 >= num1 && num2 >= num3) {
[Link](num2 + " is the largest number.");
} else {
[Link](num3 + " is the largest number.");
}
}
}

Common questions

Powered by AI

If the number is zero, the NumberSignChecker would produce the output 'Number is zero.' as it directly checks if the number equals zero in its conditional statements .

The LargestNumber program uses logical AND operators (&&) and the greater than or equal to (>=) relational operators to compare the three numbers. It checks each number against the others to determine which one is the largest. For example, num1 is declared the largest if num1 >= num2 and num1 >= num3 .

The VotingEligibility program determines voting eligibility by checking if the age variable is greater than or equal to 18. If the age is at least 18, the person is eligible to vote; otherwise, they are not eligible .

The LeapYearChecker program first checks if the year is divisible by 4, which is the basic requirement for a leap year. It then checks if the year is divisible by 100; if so, it must also be divisible by 400 to be considered a leap year. This accounts for the exception in the Gregorian calendar where years divisible by 100 are not leap years unless they are also divisible by 400 .

The LeapYearChecker handles the exception for centuries by using a nested conditional check. After determining that a year is divisible by 4, it further checks if the year is divisible by 100. If it is, an additional check verifies if the year is divisible by 400. If not divisible by 400, the year is set as not a leap year, overriding the initial condition .

The EvenOddChecker can handle negative numbers because the modulo operation used for determining evenness or oddness works the same way with negative numbers as with positive ones. A negative number modulo 2 will still determine even if the result is zero .

To adapt the VotingEligibility program for different countries, you could replace the hardcoded age threshold with a variable or a constant that can be easily updated without modifying the core logic. This would make the program flexible and applicable to any voting age requirement by changing just one value .

The EvenOddChecker program uses the modulo operator (%) to determine if a number is even or odd. Specifically, it checks if the remainder of the division of the number by 2 is zero. If the remainder is zero, the number is even; otherwise, it is odd .

The NumberSignChecker uses conditional statements to compare the given number to zero. If the number is greater than zero, it categorizes the number as positive. If the number is less than zero, it categorizes the number as negative. Finally, if the number is exactly zero, it states that the number is zero .

The LargestNumber program uses 'else' for the final comparison because after checking the first two conditions (num1 is the largest or num2 is the largest), if neither of those is true, logically num3 must be the largest. Consequently, an additional check is unnecessary, and an 'else' suffices for clean and efficient code .

You might also like