Java Programming Practical Programs
Control Structures, Loops & Logical Operators
Program 1: Multiple Conditions Using if Statement with Logical Operators
Aim:
To demonstrate the use of logical operators (&&, ||, !) with if-else statements in Java to check multiple
conditions.
Source Code:
import [Link];
public class LogicalOperators {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter age: ");
int age = [Link]();
[Link]("Enter marks (0-100): ");
int marks = [Link]();
[Link]("Enter salary: ");
double salary = [Link]();
// Using && (AND) operator
if (age >= 18 && marks >= 50) {
[Link]("Eligible: Age is 18+ AND marks are 50+");
}
// Using || (OR) operator
if (age >= 18 || marks >= 60) {
[Link]("Condition met: Age >= 18 OR Marks >= 60");
}
// Using ! (NOT) operator
if (!(age < 18)) {
[Link]("Person is an adult (NOT underage)");
}
// Combined logical operators
if (age >= 18 && marks >= 50 && salary > 20000) {
[Link]("Eligible for loan: All conditions satisfied");
} else if (age >= 18 && (marks >= 50 || salary > 15000)) {
[Link]("Partially eligible: Some conditions met");
} else {
[Link]("Not eligible: Conditions not satisfied");
}
[Link]();
}
}
Sample Output:
Enter age: 22
Enter marks (0-100): 75
Enter salary: 25000
Eligible: Age is 18+ AND marks are 50+
Condition met: Age >= 18 OR Marks >= 60
Person is an adult (NOT underage)
Eligible for loan: All conditions satisfied
Program 2: Check Whether a Number is Even or Odd
Aim:
To check whether the entered number is even or odd using the modulus (%) operator with if-else in Java.
Source Code:
import [Link];
public class EvenOdd {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
if (num % 2 == 0) {
[Link](num + " is an EVEN number.");
} else {
[Link](num + " is an ODD number.");
}
[Link]();
}
}
Sample Output:
Enter a number: 14
14 is an EVEN number.
Enter a number: 7
7 is an ODD number.
Program 3: Switch-Case Using Character Datatype
Aim:
To demonstrate the use of switch-case statement with character (char) datatype in Java for an arithmetic
calculator.
Source Code:
import [Link];
public class SwitchChar {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
int a = [Link]();
[Link]("Enter second number: ");
int b = [Link]();
[Link]("Enter operator (+, -, *, /): ");
char ch = [Link]().charAt(0);
switch (ch) {
case '+':
[Link](a + " + " + b + " = " + (a + b));
break;
case '-':
[Link](a + " - " + b + " = " + (a - b));
break;
case '*':
[Link](a + " * " + b + " = " + (a * b));
break;
case '/':
if (b != 0)
[Link](a + " / " + b + " = " + (a / b));
else
[Link]("Error: Division by zero!");
break;
default:
[Link]("Invalid operator entered!");
}
[Link]();
}
}
Sample Output:
Enter first number: 20
Enter second number: 4
Enter operator (+, -, *, /): *
20 * 4 = 80
Program 4: Display 1 to 20 Using for, while, and do-while Loop
Aim:
To display numbers from 1 to 20 using all three loop constructs available in Java: for, while, and do-while.
Source Code:
public class DisplayNumbers {
public static void main(String[] args) {
// Using for loop
[Link]("Using for loop:");
for (int i = 1; i <= 20; i++) {
[Link](i + " ");
}
[Link]();
// Using while loop
[Link]("\nUsing while loop:");
int i = 1;
while (i <= 20) {
[Link](i + " ");
i++;
}
[Link]();
// Using do-while loop
[Link]("\nUsing do-while loop:");
i = 1;
do {
[Link](i + " ");
i++;
} while (i <= 20);
[Link]();
}
}
Sample Output:
Using for loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Using while loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Using do-while loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Program 5: Logical Operators in do-while Loop
Aim:
To demonstrate the use of logical operators (&&, ||) inside a do-while loop to validate input and control loop
execution in Java.
Source Code:
import [Link];
public class LogicalDoWhile {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int num, sum = 0, count = 0;
// Example 1: do-while with && to validate range
[Link]("Enter numbers between 1-100 (enter 0 to stop):");
do {
[Link]("Enter number: ");
num = [Link]();
if (num >= 1 && num <= 100) {
sum += num;
count++;
[Link](num + " added. Sum = " + sum);
} else if (num != 0) {
[Link]("Out of range! Enter between 1 and 100.");
}
} while (num != 0); // Loop continues until 0 is entered
if (count > 0) {
[Link]("Count: " + count + ", Sum: " + sum
+ ", Average: " + (float) sum / count);
}
// Example 2: do-while with || for menu-driven program
char choice;
[Link]("\nMenu-driven do-while with || operator:");
do {
[Link]("\nA - Hello | B - World | Q - Quit");
[Link]("Enter choice: ");
choice = [Link]().charAt(0);
if (choice == 'A' || choice == 'a')
[Link]("Hello!");
else if (choice == 'B' || choice == 'b')
[Link]("World!");
else if (choice != 'Q' && choice != 'q')
[Link]("Invalid choice!");
} while (choice != 'Q' && choice != 'q');
[Link]("Program exited.");
[Link]();
}
}
Sample Output:
Enter numbers between 1-100 (enter 0 to stop):
Enter number: 45
45 added. Sum = 45
Enter number: 80
80 added. Sum = 125
Enter number: 0
Count: 2, Sum: 125, Average: 62.5
A - Hello | B - World | Q - Quit
Enter choice: A
Hello!
Enter choice: Q
Program exited.