Write a Java program to accept a number and check whether the number is even
or not. Prints 1 if the number is even or 0 if odd.
import java.util.*;
public class Exercise49 {
public static void main(String[] args) {
// Create a scanner for user input
Scanner in = new Scanner(System.in);
// Prompt the user to input a number
System.out.print("Input a number: ");
int n = in.nextInt();
// Check if the number is even and print the result
if (n % 2 == 0) {
System.out.println(1); // If the number is even, print 1
} else {
System.out.println(0); // If the number is odd, print 0
}
}
}
Write a Java program to print odd numbers from 1 to 99. Prints one number per
line.
import java.util.*;
public class Exercise48 {
public static void main(String[] args) {
// Iterate through numbers from 1 to 99
for (int i = 1; i < 100; i++) {
// Check if the number is odd
if (i % 2 != 0) {
//in if condition if have to print even numbers then just write this condition (I % 2 == 0) it will print even
numbers
// Print the odd number
System.out.println(i);
}
}
}
}