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

Java Lab Manual Part1

The document is a Java lab manual containing five programming exercises. These exercises include finding prime numbers, performing matrix multiplication, analyzing text for character, line, and word counts, generating random numbers within specified limits, and manipulating strings using character arrays. Each section provides code examples and explanations for the respective tasks.

Uploaded by

ENLIGHTNING PATH
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)
7 views3 pages

Java Lab Manual Part1

The document is a Java lab manual containing five programming exercises. These exercises include finding prime numbers, performing matrix multiplication, analyzing text for character, line, and word counts, generating random numbers within specified limits, and manipulating strings using character arrays. Each section provides code examples and explanations for the respective tasks.

Uploaded by

ENLIGHTNING PATH
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/ 3

PROGRAMMING IN JAVA LAB MANUAL

1. Prime Numbers Up To an Integer


import java.util.Scanner;
public class PrimeNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
System.out.println("Prime numbers up to " + n + " are:");
for (int i = 2; i <= n; i++) {
boolean isPrime = true;
for (int j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.print(i + " ");
}
}
sc.close();
}
}

2. Matrix Multiplication
import java.util.Scanner;
public class MatrixMultiplication {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter rows and columns of matrices: ");
int r = sc.nextInt(), c = sc.nextInt();
int[][] a = new int[r][c];
int[][] b = new int[r][c];
int[][] result = new int[r][c];
System.out.println("Enter elements of first matrix:");
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
a[i][j] = sc.nextInt();
System.out.println("Enter elements of second matrix:");
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
b[i][j] = sc.nextInt();
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
for (int k = 0; k < c; k++)
result[i][j] += a[i][k] * b[k][j];
System.out.println("Product of matrices:");
for (int[] row : result) {
for (int val : row)
System.out.print(val + " ");
System.out.println();
}
sc.close();
}
}

3. Count Characters, Lines, and Words


import java.util.Scanner;
public class TextAnalysis {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter text (type 'END' to finish):");
int lines = 0, words = 0, chars = 0;
while (true) {
String line = sc.nextLine();
if (line.equals("END")) break;
lines++;
chars += line.length();
words += line.split("\s+").length;
}
System.out.println("Lines: " + lines);
System.out.println("Words: " + words);
System.out.println("Characters: " + chars);
sc.close();
}
}

4. Generate Random Numbers Between Limits


import java.util.Random;
import java.util.Scanner;
public class RandomRange {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random rand = new Random();
System.out.print("Enter lower limit: ");
int low = sc.nextInt();
System.out.print("Enter upper limit: ");
int high = sc.nextInt();
int num = rand.nextInt(high - low + 1) + low;
System.out.println("Generated number: " + num);
if (num < (low + high) / 2)
System.out.println("Low range value");
else
System.out.println("High range value");
sc.close();
}
}

5. String Manipulation Using Character Array


public class CharArrayStringOps {
public static void main(String[] args) {
char[] str1 = {'H','e','l','l','o'};
char[] str2 = {' ','W','o','r','l','d'};
int length = str1.length;
System.out.println("Length: " + length);
System.out.println("Character at position 2: " + str1[2]);
char[] result = new char[str1.length + str2.length];
System.arraycopy(str1, 0, result, 0, str1.length);
System.arraycopy(str2, 0, result, str1.length, str2.length);
System.out.println("Concatenated String: " + new
String(result));
}
}

You might also like