0% found this document useful (0 votes)
4 views

Java Lab Journal Full

The document outlines three Java programs: the first implements a dynamic array to find the smallest and largest elements, the second calculates the GCD of two numbers using recursion, and the third computes the binomial coefficient recursively. Each program includes objectives, theoretical explanations, source code, and conclusions on their successful implementation. References to Java documentation and lab manuals are also provided.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Java Lab Journal Full

The document outlines three Java programs: the first implements a dynamic array to find the smallest and largest elements, the second calculates the GCD of two numbers using recursion, and the third computes the binomial coefficient recursively. Each program includes objectives, theoretical explanations, source code, and conclusions on their successful implementation. References to Java documentation and lab manuals are also provided.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Java Lab Journal

Program 1: Program to implement dynamic array and find smallest & largest
element of the array
 Objective: The goal of implementing a dynamic array to find the smallest and largest
elements is to create a data structure that can adjust its size based on the number of
elements stored in it. The flexibility allows it to efficiently determine the smallest and
largest elements within the array.
 Theory: The smallest and largest elements in an array using a dynamic array means that
we create a resizable array structure that allows fast and efficient storage and retrieval
of data. By dynamically adjusting the size of the array as needed, we can ensure that the
smallest and largest elements are effectively managed. The usual way is to:
1) Create a dynamically resizable array structure with fields like size and capacity.
2) Implement function to add elements to the array and resize it when necessary.
 Source Code and Output Print:

import java.util.*;

public class DynamicArray {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of elements: ");
int n = sc.nextInt();
int[] arr = new int[n];

System.out.println("Enter elements:");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}

int min = arr[0], max = arr[0];


for (int i = 1; i < n; i++) {
if (arr[i] < min) min = arr[i];
if (arr[i] > max) max = arr[i];
}

System.out.println("Smallest: " + min);


System.out.println("Largest: " + max);
}
}

 Conclusion: Successfully implemented a dynamic array and found the smallest and
largest values.
 References: Java Docs, BCA 2nd Semester Lab Manual
Program 2: Program to find GCD using recursive function
 Objective: To find the Greatest Common Divisor (GCD) of two numbers using a recursive
function.
 Theory: Recursion is a process in which a function calls itself. GCD of two numbers can
be found using Euclid's algorithm, which is efficiently implemented using recursion.
 Source Code and Output Print:

public class GCDRecursive {


static int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}

public static void main(String[] args) {


int a = 56, b = 98;
System.out.println("GCD: " + gcd(a, b));
}
}

 Conclusion: This program successfully demonstrates the concept of recursion by


calculating the GCD of two numbers using Euclid’s algorithm. It highlights how recursive
functions can simplify complex logic in programming.
 References: Java Docs, Data Structures Lab Manual

Program 3: Program to generate binomial coefficient using recursive function


 Objective: To calculate the binomial coefficient using recursion.
 Theory: The binomial coefficient C(n, k) can be calculated using the formula: C(n, k) =
C(n-1, k-1) + C(n-1, k). Recursion helps implement this formula directly.
 Source Code and Output Print:

public class BinomialCoefficient {


static int binomial(int n, int k) {
if (k == 0 || k == n) return 1;
return binomial(n - 1, k - 1) + binomial(n - 1, k);
}

public static void main(String[] args) {


int n = 5, k = 2;
System.out.println("Binomial Coefficient: " + binomial(n, k));
}
}

 Conclusion: Through this program, the recursive approach to calculate binomial


coefficients illustrates how recursion maps directly onto mathematical definitions,
making the code intuitive and elegant.
 References: Java Docs, Mathematical Computation Notes

You might also like