[Go to site: main page, start]

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

Java Lab Programs Bca

The document outlines algorithms and Java programs for various tasks including checking for prime numbers, matrix multiplication, reading a file to count lines, words, and characters, generating random numbers within a range, and manipulating strings. Each section provides a step-by-step algorithm followed by the corresponding Java code implementation. Example inputs and outputs are also included to illustrate the functionality of each program.

Uploaded by

m.tamil948711
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)
5 views8 pages

Java Lab Programs Bca

The document outlines algorithms and Java programs for various tasks including checking for prime numbers, matrix multiplication, reading a file to count lines, words, and characters, generating random numbers within a range, and manipulating strings. Each section provides a step-by-step algorithm followed by the corresponding Java code implementation. Example inputs and outputs are also included to illustrate the functionality of each program.

Uploaded by

m.tamil948711
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

Prime Numbers

Algorithm
1. Start the program.
2. Take a number and store it in the variable n.
3. Initialize a variable count to zero to count the number of factors.
4. Repeat a loop from i = 1 to n.
5. Inside the loop, check whether n is divisible by i.
6. If n is divisible by i, increment the value of count by one using count++.
7. After completing the loop, check the value of count.
8. If count is equal to 2, then print that the number is a prime number.
9. Otherwise, print that the number is not a prime number.
10. Stop the program.

Java Program
class prg1{
public static void main(String[] args) {

int n = 10;
int count = 0;

for (int i = 1; i <= n; i++) {


if (n % i == 0) {
count++;
}
}

if (count == 2)
[Link](n + " is a Prime number");
else
[Link](n + " is NOT a Prime number");
}
}

Output:
10 is NOT a Prime number

[Link] Multiplication
Algorithm
1. Start the program.
2. Read the number of rows and columns of the first matrix.
3. Read the number of rows and columns of the second matrix.
4. Check whether the number of columns of the first matrix is equal to the number of rows of the
second matrix.
5. If the condition is not satisfied, display a message and stop the program.
6. If the condition is satisfied, read all elements of both matrices from the user.
7. Create a result matrix with rows of the first matrix and columns of the second matrix.
8. Use three loops to multiply the matrices:
o First loop for rows of the first matrix.
o Second loop for columns of the second matrix.
o Third loop for multiplying and adding elements.
9. Display the resultant matrix.
10. Stop the program.

Program
import [Link];

class prg2
{
public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

// Input matrix sizes


[Link]("Enter rows of first matrix: ");
int r1 = [Link]();
[Link]("Enter columns of first matrix: ");
int c1 = [Link]();

[Link]("Enter rows of second matrix: ");


int r2 = [Link]();
[Link]("Enter columns of second matrix: ");
int c2 = [Link]();

// Check multiplication condition


if (c1 != r2) {
[Link]("Matrix multiplication not possible");
return;
}

int[][] A = new int[r1][c1];


int[][] B = new int[r2][c2];
int[][] C = new int[r1][c2];

// Input first matrix


[Link]("Enter elements of first matrix:");
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c1; j++) {
A[i][j] = [Link]();
}
}

// Input second matrix


[Link]("Enter elements of second matrix:");
for (int i = 0; i < r2; i++) {
for (int j = 0; j < c2; j++) {
B[i][j] = [Link]();
}
}
// Matrix multiplication
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
for (int k = 0; k < c1; k++) {
C[i][j] = C[i][j] + A[i][k] * B[k][j];
}
}
}

// Display result
[Link]("Resultant Matrix:");
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
[Link](C[i][j] + " ");
}
[Link]();
}

[Link]();
}
}

Input
Enter rows of first matrix: 2
Enter columns of first matrix: 2
Enter rows of second matrix: 2
Enter columns of second matrix: 2

Enter elements of first matrix:


1 2
3 4

Enter elements of second matrix:


5 6
7 8

Output
Resultant Matrix:
19 22
43 50

3. Reads a file from a given path and counts lines, words, and
characters

Algorithm
1. Start the program.
2. Declare a string variable filePath and assign it the full path of the file you want to read.
3. Initialize three integer variables: lines = 0, words = 0, and characters = 0.
4. Use a try block to open the file using FileReader and BufferedReader.
5. Read the file line by line using a while loop:
o For each line, increment lines by 1.
o Add the length of the line to characters.
o Split the line into words using spaces and add the number of words to words.
6. After reading all lines, close the BufferedReader and FileReader.
7. Print the total number of lines, words, and characters.
8. Use a catch block to handle any IOException that occurs (for example, if the file is not
found).
9. Stop the program.

Java Program
import [Link];

import [Link];

import [Link];

class prg3

public static void main(String[] args) {

// Directly give the full file path here

String filePath = "C:\\Users\\YourName\\Documents\\[Link]"; // Windows

// For Mac/Linux: "/Users/YourName/Documents/[Link]"

int lines = 0;

int words = 0;

int characters = 0;

try {

FileReader fr = new FileReader(filePath);

BufferedReader br = new BufferedReader(fr);

String line;

while ((line = [Link]()) != null) {

lines++; // count lines

characters += [Link](); // count characters

String[] wordArray = [Link]("\\s+"); // split by spaces

words += [Link]; // count words

}
[Link]();

[Link]();

[Link]("Number of lines: " + lines);

[Link]("Number of words: " + words);

[Link]("Number of characters: " + characters);

} catch (IOException e) {

[Link]("Error: File not found or cannot be read.");

Example

File [Link] contents:

Output:

Number of lines: 2

Number of words: 7

Number of characters: 33

[Link] Random Numbers Between Two Given Limits

Algorithm
1. Start the program.
2. Define variables: lower = 1 and upper = 100.
3. Import [Link] and create a Random object.
4. Generate a random number between lower and upper.
5. Check which range the number belongs to:
o 1–20, 21–40, 41–60, 61–80, 81–100
6. Print the generated number and its range.
7. Stop the program.

Program
import [Link];

class prg4
{

public static void main(String[] args)

int lower = 1;

int upper = 100;

Random rand = new Random();

int number = [Link](upper - lower + 1) + lower;

[Link]("Generated number: " + number);

if (number >= 1 && number <= 20)

[Link]("1 to 20");

else if (number >= 21 && number <= 40)

[Link]("21 to 40");

else if (number >= 41 && number <= 60)

[Link]("41 to 60");

else if (number >= 61 && number <= 80)

[Link]("61 to 80");

else

[Link]("81 to 100");
}

Output

Generated number: 12

1 to 20

Generated number: 55

41 to 60

Generated number: 88

81 to 100

Algorithm
1. Start the program.
2. Define two strings: str1 = "Hello" and str2 = "World".
3. Convert the strings to character arrays:
o charArray1 = [Link]()
o charArray2 = [Link]()
4. Find the length of the first string:
o length = [Link]
o
Print the length.
5. Find a character at a particular position:
o Define a variable position (zero-based index).
o If position >= 0 and position < [Link]
 Print charArray1[position].
o Else
 Print “Position out of range!”.
6. Concatenate the two strings:
o Use str1 + str2 to create concatenatedString.
o Print concatenatedString.
7. End the program.

Program
class prg5

public static void main(String[] args)


{

String str1 = "Hello";

String str2 = "World";

char[] charArray1 = [Link]();

char[] charArray2 = [Link]();

int length = [Link];

[Link]("Length of the string '" + str1 + "': " + length);

int position = 2; // zero-based index (0 = first character)

if (position >= 0 && position < [Link]) {

[Link]("Character at position " + position + " in '" + str1 + "': " +


charArray1[position]);

else

[Link]("Position out of range!");

String concatenatedString = str1 + str2;

[Link]("Concatenated string: " + concatenatedString);

Output

Length of the string 'Hello': 5

Character at position 2 in 'Hello': l

Concatenated string: HelloWorld

You might also like