0% found this document useful (0 votes)
24 views8 pages

Java Practical 1,2

The document contains practical Java programming exercises demonstrating various concepts such as Boolean values, loops, arithmetic operations, conditional statements, and switch cases. It includes sample code for finding the biggest number among three numbers using both if-else and ternary operator, as well as checking grades based on marks input. Each program is followed by its output to illustrate the expected results.
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)
24 views8 pages

Java Practical 1,2

The document contains practical Java programming exercises demonstrating various concepts such as Boolean values, loops, arithmetic operations, conditional statements, and switch cases. It includes sample code for finding the biggest number among three numbers using both if-else and ternary operator, as well as checking grades based on marks input. Each program is followed by its output to illustrate the expected results.
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

Practical No.

a. Write a program in Java to demonstrate Boolean value.


Program:-
import [Link].*;
class FindBoolean
{
public static void main(String[] args){
int x=10;
int y=9;
[Link](x<y);
}
}

Output:
b. Print a string 10 times using a for loop.
Program:-
import [Link].*;
class Looping
{
public static void main(String[] args)
{
for(int i=1;i<10;i++)
[Link]("Hello World");
}
}
Output:
c. Write a program in Java to evaluate a + b * c % d.
Program:
import [Link].*;
class Calculation {
public static void main(String [] args ){
int a=1;
int b=2;
int c=3;
int d=4;
int result=a + b * c % d;
[Link](result);
}
}
Output:
Practical No.2

a. Write a program in Java to find the biggest element among three numbers
using if else.
Program:
public class FindBiggestElement {
public static void main(String[] args) {
int num1 = 10;
int num2 = 25;
int num3 = 15;
int max;

if (num1 >= num2) {


// If num1 is greater than or equal to num2, compare num1 with
num3
if (num1 >= num3) {
max = num1;
} else {
max = num3;
}
} else {
// If num2 is greater than num1, compare num2 with num3
if (num2 >= num3) {
max = num2;
} else {
max = num3;
}
}

[Link]("The three numbers are: " + num1 + ", " + num2


+ ", " + num3);
[Link]("The biggest number is: " + max);
}
}

Output:
b. Write a program in Java to find the biggest element among three numbers
using the ternary operator.
Program: public class BiggestNumber {
public static void main(String[] args) {
int a = 66;
int b = 69;
int c = 79;
int biggest = ( a >= b ) ? (( a >= c ) ? a : c ) : (( b >= c ) ? b : c) ;
[Link]("The numbers are: a = " + a + ", b = " + b + ", c = " + c);
[Link]("The biggest number is: " + biggest);
}
}
Output:
c. Write a program in Java to check the grade of marks using a switch case.
Program:
import [Link];
public class GradeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter marks (0–100): ");
int marks = [Link]();
int grade = marks / 10; // Convert marks into grade range
switch (grade) {
case 10:
case 9:
[Link]("Grade: A");
break;
case 8:
[Link]("Grade: B");
break;
case 7:
[Link]("Grade: C");
break;
case 6:
[Link]("Grade: D");
break;
case 5:
[Link]("Grade: E");
break;
default:
if (marks < 0 || marks > 100)
[Link]("Invalid marks");
else
[Link]("Grade: F");
}
[Link]();
}
}
Output:

You might also like