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

Java Branching Statements Example

Uploaded by

kushaldubey121
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Java Branching Statements Example

Uploaded by

kushaldubey121
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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:

You might also like