Q: Write a program to check multiple conditions using if statement along with logical
operators.
public class MultipleConditions {
public static void main(String[] args) {
int a = 10, b = 20, c = 30;
if (a < b && b < c) {
System.out.println("a is less than b AND b is less than c");
} else {
System.out.println("Conditions not satisfied");
}
}
}
Q: Write a program to check no is even or odd.
public class EvenOdd {
public static void main(String[] args) {
int number = 7;
if (number % 2 == 0) {
System.out.println(number + " is even.");
} else {
System.out.println(number + " is odd.");
}
}
}
Q: Write a program to check switch-case using character datatype.
public class SwitchChar {
public static void main(String[] args) {
char grade = 'B';
switch (grade) {
case 'A':
System.out.println("Excellent!");
break;
case 'B':
System.out.println("Well done");
break;
case 'C':
System.out.println("Good");
break;
default:
System.out.println("Invalid grade");
}
}
}
Q: Write a program to display 1 to 20 numbers using for, while and do-while loop.
public class LoopDemo {
public static void main(String[] args) {
System.out.println("For Loop:");
for (int i = 1; i <= 20; i++) {
System.out.print(i + " ");
}
System.out.println("\nWhile Loop:");
int j = 1;
while (j <= 20) {
System.out.print(j + " ");
j++;
}
System.out.println("\nDo-While Loop:");
int k = 1;
do {
System.out.print(k + " ");
k++;
} while (k <= 20);
}
}
Q: Develop a program to use logical operators in do-while loop.
public class DoWhileLogical {
public static void main(String[] args) {
int x = 0;
do {
if (x % 2 == 0 && x < 10) {
System.out.println(x + " is even and less than 10");
}
x++;
} while (x <= 15);
}
}
Q: Write a program to show the use of all methods of String class.
public class StringMethods {
public static void main(String[] args) {
String s = " Hello Java ";
System.out.println("Length: " + s.length());
System.out.println("Trim: " + s.trim());
System.out.println("To Upper: " + s.toUpperCase());
System.out.println("To Lower: " + s.toLowerCase());
System.out.println("CharAt 1: " + s.charAt(1));
System.out.println("Substring: " + s.substring(1, 5));
System.out.println("Replace: " + s.replace("Java", "World"));
System.out.println("Contains 'Java': " + s.contains("Java"));
System.out.println("Starts with H: " + s.trim().startsWith("H"));
System.out.println("Ends with a: " + s.trim().endsWith("a"));
}
}