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

Java Basic Programs

The document contains two Java programs. The first program checks if a user-input number is even or odd, printing 1 for even and 0 for odd. The second program prints all odd numbers from 1 to 99, displaying one number per line.

Uploaded by

rahulbadhe630
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)
5 views2 pages

Java Basic Programs

The document contains two Java programs. The first program checks if a user-input number is even or odd, printing 1 for even and 0 for odd. The second program prints all odd numbers from 1 to 99, displaying one number per line.

Uploaded by

rahulbadhe630
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/ 2

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);
}
}
}
}

You might also like