AIM: Java Demonstrating branching statements (if, if-else, if-else-if, switch).
PROGRAM:
import [Link];
public class BranchingEx {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]):
[Link]("Enter a number: ");
int num = [Link]();
if (num < 0) {
[Link]("Number is negative.");
} else if (num == 0) {
[Link]("Number is zero.");
} else {
[Link]("Number is positive.");
if (num % 2 == 0) {
[Link]("Number is even.");
} else {
[Link]("Number is odd.");
[Link]("\nChoose an option:");
[Link]("1. Square");
[Link]("2. Cube");
[Link]("3. Exit");
int choice = [Link]();
switch (choice) {
case 1:
[Link]("Square of " + num + " is " + (num * num));
break;
case 2:
[Link]("Cube of " + num + " is " + (num * num * num));
break;
case 3:
[Link]("Exiting program...");
break;
default:
[Link]("Invalid choice.");
[Link]();
OUTPUT: