Practical 1
Aim : A java program to print "Hello World."
Code:
public class HelloWorld{
public static void main(String args []){
[Link]("Hello World" + " : Sumit Verma 24SCSE2140042");
Output :
Practical 2
Aim : A java program to find sum of odd number and even number.
Code:
import [Link];
public class SumOfOddEven {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter Start of Range : ");
int start = [Link]();
[Link]("Enter End of Range : ");
int end = [Link]();
int sumOfOdd = 0 ; int sumOfEven = 0;
for (int i = start; i <= end; i++) {
if (i % 2 == 0) {
sumOfEven += i; // Even
else
sumOfOdd += i; // Odd
[Link]();
[Link]("Sum of Even Numbers is : " + sumOfEven);
[Link]("Sum of Odd Numbers is : " + sumOfOdd);
[Link]("Sumit Verma 24SCSE2140042");
Output :
Practical 3
Aim : A java program to find factorial of any given number using recursion.
Code:
import [Link];
public class factorial {
static int factorial(int n) {
if (n == 1) {
return 1;
return n * factorial(n - 1);
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter Number: ");
int n = [Link]();
[Link]("Factorial is : " + factorial(n));
[Link]();
[Link]("Sumit Verma 24SCSE2140042");
Output :
Practical 4
Aim : A java program to read an integer value through Scanner class and find
prime number within given range.
Code:
import [Link];
public class prime{
public static boolean checkIsPrime(int n) {
for (int i = 2; i <= [Link](n); i++) {
if (n == 2)
return true;
else if (n % i == 0) {
return false;
return true;
public static void primenorange(int end, int start) {
for (int i = start; i <= end; i++) {
if (checkIsPrime(i) == true) {
[Link](i + " ");
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter Starting Range : ");
int start = [Link]();
[Link]("Enter Ending Range : ");
int end = [Link]();
primenorange(end, start);
[Link]();
[Link]("Sumit Verma 24SCSE2140042");
Output :
Practical 5
Aim: A java program that uses length property for displaying
any number of command line arguments.
class commandline {
public static void main(String[] args) {
[Link]("Number of command-line arguments: " +
[Link]);
for (int i = 0; i < [Link]; i++) {
[Link]("Argument " + (i+1) + ": " + args[i]);
[Link]("Sumit Verma 24SCSE2140042");
Output :
Practical 6
Aim: A java program to sort n numbers using bubble sort.
import [Link];
public class bubbleSort {
public static void bubbleSort(int[] arr) {
int n = [Link];
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
public static void printArray(int[] arr) {
for (int num : arr) {
[Link](num + " ");
}
[Link]();
}
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter the number of elements: ");
int n = [Link]();
int[] arr = new int[n];
[Link]("Enter " + n + " integers:");
for (int i = 0; i < n; i++) {
arr[i] = [Link]();
}
[Link]("Original array:");
printArray(arr);
bubbleSort(arr);
[Link]("Sorted array:");
printArray(arr);
[Link]("sumit verma 24scse2140042");
[Link]();
}
}
Output :
Practical 7
Aim: A java program to find addition and multiplication of two
2D Matrices. Program:
import [Link];
public class MatrixOperations {
public static int[][] addMatrices(int[][] matrix1, int[][] matrix2, int rows, int
cols) {
int[][] sum = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
return sum;
}
public static int[][] multiplyMatrices(int[][] matrix1, int[][] matrix2, int rows1,
int cols1, int cols2) {
int[][] product = new int[rows1][cols2];
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
for (int k = 0; k < cols1; k++) {
product[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
return product;
}
// Method to print a matrix
public static void printMatrix(int[][] matrix, int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
[Link](matrix[i][j] + " ");
}
[Link]();
}
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number of rows for the matrices: ");
int rows = [Link]();
[Link]("Enter number of columns for the matrices: ");
int cols = [Link]();
int[][] matrix1 = new int[rows][cols];
int[][] matrix2 = new int[rows][cols];
[Link]("Enter elements of first matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix1[i][j] = [Link]();
}
}
[Link]("Enter elements of second matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix2[i][j] = [Link]();
}
}
[Link]("Matrix Addition Result:");
int[][] sum = addMatrices(matrix1, matrix2, rows, cols);
printMatrix(sum, rows, cols);
[Link]("Matrix Multiplication Result:");
int[][] product = multiplyMatrices(matrix1, matrix2, rows, cols, cols);
printMatrix(product, rows, cols);
[Link]();
}
}
Output :
Practical 8
Aim: A java code to implement the concept of simple
inheritance, multilevel inheritance, and hierarchical inheritance.
class Animal {
public void eat() {
[Link]("This animal is eating.");
}
}
class Mammal extends Animal {
public void walk() {
[Link]("This mammal is walking.");
}
}
class Dog extends Mammal {
public void bark() {
[Link]("The dog is barking.");
}
}
class Cat extends Mammal {
public void meow() {
[Link]("The cat is meowing.");
}
}
public class InheritanceExample {
public static void main(String[] args) {
Mammal mammal = new Mammal();
[Link]("Mammal Example:");
[Link]();
[Link]();
Dog dog = new Dog();
[Link]("\nDog Example (Multilevel Inheritance):");
[Link]();
[Link]();
[Link]();
Cat cat = new Cat();
[Link]("\nCat Example (Hierarchical Inheritance):");
[Link]();
[Link]();
[Link]();
[Link]("sumit verma 24scse2140042");
}
}
Output :
Practical 9
Aim: A java programs for Exception handling using try, catch,
throw, throws and finally.
import [Link];
public class ExceptionHandlingExample {
public static int divide(int a, int b) throws ArithmeticException {
if (b == 0) {
throw new ArithmeticException("Division by zero is not allowed.");
}
return a / b;
}
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
try {
[Link]("Enter first number: ");
int num1 = [Link]();
[Link]("Enter second number: ");
int num2 = [Link]();
int result = divide(num1, num2);
[Link]("Result of division: " + result);
} catch (ArithmeticException e) {
[Link]("Exception caught: " + [Link]());
} catch (Exception e) {
[Link]("Some other exception occurred: " + [Link]());
} finally {
[Link]("Finally block executed. Resources can be cleaned up
here.");
}
[Link]("sumit verma 24scse2140042");
[Link]();
}
}
Output :
Practical 10
Aim: A java program to implement the usage of customized
exceptions.
class CustomException extends Exception {
public CustomException(String message) {
super(message);
}
}
public class ExceptionHandlingExample2 {
public static void validateNumber(int number) throws CustomException {
if (number < 0) {
throw new CustomException("Number cannot be negative!");
} else {
[Link]("Valid number: " + number);
}
}
public static void main(String[] args) {
int number1 = 5;
int number2 = -3;
try {
[Link]("Checking number: " + number1);
validateNumber(number1);
[Link]("\nChecking number: " + number2);
validateNumber(number2);
} catch (CustomException e) {
[Link]("Caught Exception: " + [Link]());
} finally {
[Link]("Finally block executed.");
}
[Link]("sumit verma 24scse2140042");
}
}
Output :