0% found this document useful (0 votes)
7 views9 pages

Omopss 2

The document outlines an experiment aimed at teaching students about branching, looping, and the use of labelled break and continue in Java programming. It includes various programming tasks such as calculating the roots of a quadratic equation, checking for prime numbers, demonstrating operator types, and printing Armstrong numbers. The objective is to enhance understanding of decision-making constructs and looping mechanisms in Java, along with providing syntax and examples for each concept.
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)
7 views9 pages

Omopss 2

The document outlines an experiment aimed at teaching students about branching, looping, and the use of labelled break and continue in Java programming. It includes various programming tasks such as calculating the roots of a quadratic equation, checking for prime numbers, demonstrating operator types, and printing Armstrong numbers. The objective is to enhance understanding of decision-making constructs and looping mechanisms in Java, along with providing syntax and examples for each concept.
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
You are on page 1/ 9

Experiment No:2-

AIM : Program On Branching ,Looping, Labelled break and continue


Branching
WAP to print the roots of quadractic equation.
WAP to check if the entered no. is a prime no. or not.
WAJP to demonstrate the working of types of operators(Bitwise, Logical and relational) using
switch case. Looping

WAP to print the patterns.

WAP to print all the three digit armstrong nos. Labelled break
& continue

WAP to demonstrate the working of labelled break and continue.

OBJECTIVE : To make Students understand the working of different decision Making and

Looping Constructs in Java. Also introduce them to the concept of Labelled break and continue.

Description :

Give syntax and explaination for if-else, switch,for while and do_while loop along with example.
Give description for Labelled break and continue with example.

CONCLUSION: Students are expected to List the error they are facing along with their solution

REFERENCES :
1.WAPJ to print the roots of quadratic equation :

import java.util.Scanner; public class


QuadraticEquationSolve { public
static void main(String[] args) {
System.out.printn(“Om Gaikwad”);
Scanner scanner = new Scanner(System.in);
System.out.print("Enter coefficient a: "); double a =
scanner.nextDouble(); System.out.print("Enter
coefficient b: "); double b = scanner.nextDouble();
System.out.print("Enter coefficient c: "); double c =
scanner.nextDouble(); double discriminant = b * b - 4
* a * c; if (discriminant > 0) { double root1 = (-b +
Math.sqrt(discriminant)) / (2 * a); double root2 = (-b -
Math.sqrt(discriminant)) / (2 * a);
System.out.println("The roots are real and distinct.");
System.out.println("Root 1: " + root1);
System.out.println("Root 2: " + root2);
} else if (discriminant == 0) { double
root = -b / (2 * a);
System.out.println("The roots are real and the same.");
System.out.println("Root: " + root);
} else { double realPart = -b
/ (2 * a);
double imaginaryPart = Math.sqrt(-discriminant) / (2 * a); System.out.println("The
roots are complex and distinct.");
System.out.println("Root 1: " + realPart + " + " + imaginaryPart + "i");
System.out.println("Root 2: " + realPart + " - " + imaginaryPart + "i");
}
scanner.close();
}
}
OUTPUT:
2.WAP to check if the entered no. is a prime no. or not

import java.util.Scanner; public class


PrimeChecker { public static void
main(String[] args) {
System.out.printn(“Om Gaikwad”);
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number to check: ");
int number = scanner.nextInt(); if
(isPrime(number)) {
System.out.println(number + " is a prime number.");
} else {
System.out.println(number + " is not a prime number.");
}
scanner.close();
}
public static boolean isPrime(int num) { if
(num <= 1) return false; if (num <= 3) return
true; if (num % 2 == 0 || num % 3 == 0) return
false; for (int i = 5; i * i <= num; i += 6) { if
(num % i == 0 || num % (i + 2) == 0) return
false;
}
return true;
}
}
Output:

o demonstrate the working of types of operators(Bitwise, Logical and relational)using switch case. .
import java.util.Scanner; public
class OperatorDemo { public static
void main(String[] args) {
System.out.printn(“Om Gaikwad”);
Scanner scanner = new Scanner(System.in);
System.out.println("Select the type of operator to demonstrate:");
System.out.println("1. Bitwise Operators");
System.out.println("2. Logical Operators");
System.out.println("3. Relational Operators");
System.out.print("Enter your choice (1/2/3): ");
int choice = scanner.nextInt(); switch (choice)
{ case 1: demonstrateBitwiseOperators();
break; case 2:
demonstrateLogicalOperators(); break; case
3:
demonstrateRelationalOperators();
break; default:
System.out.println("Invalid choice."); break;
}
scanner.close();
}
private static void demonstrateBitwiseOperators() {
int a = 5; // 0101 in binary int b = 3; // 0011 in binary
System.out.println("Bitwise Operators Demonstration:");
System.out.println("a = " + a + " (in binary: 0101)");
System.out.println("b = " + b + " (in binary: 0011)");
System.out.println("a & b = " + (a & b) + " (in binary: " + Integer.toBinaryString(a & b) + ")");
System.out.println("a | b = " + (a | b) + " (in binary: " + Integer.toBinaryString(a | b) + ")");
System.out.println("a ^ b = " + (a ^ b) + " (in binary: " + Integer.toBinaryString(a ^ b) + ")");
System.out.println("~a = " + (~a) + " (in binary: " + Integer.toBinaryString(~a) + ")");
System.out.println("a << 1 = " + (a << 1) + " (in binary: " + Integer.toBinaryString(a << 1) +
")");
System.out.println("a >> 1 = " + (a >> 1) + " (in binary: " + Integer.toBinaryString(a >> 1) +
")");
}
private static void demonstrateLogicalOperators() {
boolean x = true; boolean y = false;
System.out.println("\nLogical Operators Demonstration:");
System.out.println("x = " + x);
System.out.println("y = " + y);
System.out.println("x && y = " + (x && y));
System.out.println("x || y = " + (x || y));
System.out.println("!x = " + !x);
System.out.println("!y = " + !y);
}
private static void demonstrateRelationalOperators() {
int a = 50; int b = 40;
System.out.println("\nRelational Operators Demonstration:");
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("a == b: " + (a == b));
System.out.println("a != b: " + (a != b));
System.out.println("a > b: " + (a > b));
System.out.println("a < b: " + (a < b));
System.out.println("a >= b: " + (a >= b));
System.out.println("a <= b: " + (a <= b));
}
Output:

4.WAP to print all the three digit armstrong nos


public class ArmstrongNumbers { public static
void main(String[] args) {
System.out.printn(“Om Gaikwad”);

System.out.println("Three-digit Armstrong numbers are:"); for (int num =


100; num < 1000; num++) { int originalNum = num; int sum = 0; int digit1 =
originalNum / 100; int digit2 = (originalNum / 10) % 10; int digit3 =
originalNum % 10; sum = (int) (Math.pow(digit1, 3) + Math.pow(digit2, 3) +
Math.pow(digit3, 3)); if (sum == num) {

System.out.println(num);
}
}
}
}
Output:
5.WAP to demonstrate the working of labelled break and continue.

public class LabeledBreakContinue { public

static void main(String[] args) {

System.out.printn(“Om Gaikwad”);

outerLoop:
for (int i = 2; i <= 5; i++) {
System.out.println("Outer loop iteration: " + i); for
(int j = 1; j <= 5; j++) {
System.out.println(" Inner loop iteration: " + j);
if (j == 3) {
System.out.println(" Breaking out of the outer loop"); break
outerLoop;
}
}
}
System.out.println(); outerLoop:
for (int i = 2; i <= 5; i++) { if
(i == 3) {
System.out.println("Skipping iteration of the outer loop when i is 3"); continue
outerLoop;
}
System.out.println("Outer loop iteration: " + i); for
(int j = 1; j <= 5; j++) {
System.out.println(" Inner loop iteration: " + j);
}
}
}
}
Output:

You might also like