0% found this document useful (0 votes)
9 views28 pages

Assiment Java Unit2

Uploaded by

gptharshad
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)
9 views28 pages

Assiment Java Unit2

Uploaded by

gptharshad
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
You are on page 1/ 28

INDEX

Decision Making, Loops, Arrays, and Strings

Exercise 1: Decision Making with If-Else and Switch


❖ Write a Java program to input a student's marks and print the grade according to a
defined scale (e.g., A for >90, B for 80-90, etc.).
❖ Implement a program to determine the day of the week based on user input (1 for
Monday, 2 for Tuesday, etc.) using both if-else and switch-case constructs.
Exercise 2: Loop Control Statements
❖ Write a program to print the first N Fibonacci numbers using a for loop.
❖ Write a Java program to display all prime numbers between two given numbers
using nested loops.
❖ Implement a program to calculate the factorial of a number using while and do-
while loops.
❖ Write a program that prints a pattern of stars, such as a pyramid or diamond, using
nested loops.
❖ Create programs to demonstrate the use of break, continue, and labeled
break/continue statements within loops.
Exercise 3: Array Handling
❖ Write a program to input N integers into an array and find the largest and smallest
elements.
❖ Write a Java program to reverse an integer array and print the reversed array.
❖ Implement a program to find the sum and average of elements in a double-type
array.
❖ Create a program to merge two arrays and display the resultant array.
❖ Write a program to count the frequency of each element in an array.

Exercise 4: String Manipulation


❖ Write a program to input a string and check whether it is a palindrome.
❖ Implement programs to demonstrate usage of various String methods such as
length(), charAt(), substring(), indexOf(), replace(), and
toUpperCase()/toLowerCase().
❖ Create a program to compare two strings ignoring case differences.
❖ Write a program to count the number of vowels and consonants in a given string.
❖ Demonstrate the difference between String, StringBuffer, and StringBuilder in
concatenation and reversal operations.
Exercise 5: Two-Dimensional Arrays
❖ Write a program to input elements into a 2D array (matrix) and display it.
❖ Implement matrix addition and multiplication programs.
❖ Write a program to find the transpose of a matrix.
❖ Display the diagonal elements and sum them in a square matrix.

Exercise 1: Decision Making with If-Else and Switch


1. Write a Java program to input a student's marks and print the grade according to a
defined scale (e.g., A for >90, B for 80-90, etc.).
Program
import java.util.Scanner;
public class L01 {//BETN1CS24109
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the student's marks (0-100): ");
int marks = sc.nextInt();
if (marks > 100 || marks < 0) {
System.out.println("Invalid marks! Please enter between 0 and 100.");
} else if (marks > 90) {
System.out.println("Grade: A");
} else if (marks >= 80) {
System.out.println("Grade: B");
} else if (marks >= 70) {
System.out.println("Grade: C");
} else if (marks >= 60) {
System.out.println("Grade: D");
} else if (marks >= 50) {
System.out.println("Grade: E");
} else {
System.out.println("Grade: F (Fail)");
}
}
}

Output
2. Implement a program to determine the day of the week based on user input (1 for
Monday, 2 for Tuesday, etc.) using both if-else and switch-case constructs.
Program
import java.util.Scanner;
public class L02 {//BETN1CS24109
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number (1-7) for the day of the week: ");
int day = sc.nextInt();
System.out.println("\n--- Using if-else ---");
if (day == 1) {
System.out.println("Monday");
} else if (day == 2) {
System.out.println("Tuesday");
} else if (day == 3) {
System.out.println("Wednesday");
} else if (day == 4) {
System.out.println("Thursday");
} else if (day == 5) {
System.out.println("Friday");
} else if (day == 6) {
System.out.println("Saturday");
} else if (day == 7) {
System.out.println("Sunday");
} else {
System.out.println("Invalid input! Please enter 1 to 7.");
}
System.out.println("\n--- Using switch-case ---");
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid input! Please enter 1 to 7.");
}
}
}

Output

Exercise 2: Loop Control Statements


1. Write a program to print the first N Fibonacci numbers using a for loop.
Program
import java.util.Scanner;

public class L03 { //BETN1CS24109


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

int first = 0, second = 1;


System.out.print("Fibonacci Series: ");
for (int i = 1; i <= n; i++) {
System.out.print(first + " ");
int next = first + second;
first = second;
second = next;
}
sc.close();
}
}

Output
2. Write a Java program to display all prime numbers between two given numbers
using nested loops.
Program
import java.util.Scanner;

public class L04 { //BETN1CS24109


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter start number: ");
int start = sc.nextInt();
System.out.print("Enter end number: ");
int end = sc.nextInt();
System.out.println("Prime numbers between " + start + " and " + end + ":");
for (int i = start; i <= end; i++) {
boolean isPrime = true;
if (i <= 1) continue;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.print(i + " ");
}
}
}
}

Output
3. Implement a program to calculate the factorial of a number using while and do-
while loops.
Program
import java.util.Scanner;
public class L05 { //BETN1CS24109
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int fact1 = 1, i = num;
while (i > 0) {
fact1 *= i;
i--;
}
System.out.println("Factorial using while loop: " + fact1);
int fact2 = 1, j = num;
do {
fact2 *= j;
j--;
} while (j > 0);
System.out.println("Factorial using do-while loop: " + fact2);
}
}

Output

4. Write a program that prints a pattern of stars, such as a pyramid or diamond,


using nested loops.
Program
import java.util.Scanner;
public class L06 { //BETN1CS24109
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of rows: ");
int rows = sc.nextInt();
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++) {
System.out.print(" "); // spaces
}
for (int k = 1; k <= (2 * i - 1); k++) {
System.out.print("*"); // stars
}
System.out.println();
}
}
}

Output
5. Create programs to demonstrate the use of break, continue, and labeled
break/continue statements within loops.
Program

public class L07 { //BETN1CS24109


public static void main(String[] args) {
System.out.println("Demonstrating break:");
for (int i = 1; i <= 10; i++) {
if (i == 5) break; // exit loop
System.out.print(i + " ");
}
System.out.println("\n\nDemonstrating continue:");
for (int i = 1; i <= 10; i++) {
if (i == 5) continue; // skip iteration
System.out.print(i + " ");
}
System.out.println("\n\nDemonstrating labeled break:");
outer: for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (i == 2 && j == 2) break outer;
System.out.println(i + " " + j);
}
}
System.out.println("\nDemonstrating labeled continue:");
outer: for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) continue outer;
System.out.println(i + " " + j);
}
}
}
}

Output
Exercise 3: Array Handling

1. Write a program to input N integers into an array and find the largest and smallest
elements
Program

import java.util.Scanner;
public class L08 {//BETN1CS24109
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 " + n + " integers:");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int largest = arr[0], smallest = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > largest) largest = arr[i];
if (arr[i] < smallest) smallest = arr[i];
}
System.out.println("Largest element: " + largest);
System.out.println("Smallest element: " + smallest);
}
}
Output

2. Write a Java program to reverse an integer array and print the reversed array.
Program
import java.util.Scanner;
public class L09 {//BETN1CS24109
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 " + n + " integers:");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
System.out.println("Reversed array:");
for (int i = n - 1; i >= 0; i--) {
System.out.print(arr[i] + " ");
}
}
}

Output
3. Implement a program to find the sum and average of elements in a double-type
array.
Program
import java.util.Scanner;
public class L10 {//BETN1CS24109
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of elements: ");
int n = sc.nextInt();
double[] arr = new double[n];
System.out.println("Enter " + n + " double values:");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextDouble();
}
double sum = 0;
for (double val : arr) {
sum += val;
}
double avg = sum / n;
System.out.println("Sum of elements: " + sum);
System.out.println("Average of elements: " + avg);
}
}

Output

4. Create a program to merge two arrays and display the resultant array.
Program
import java.util.Scanner;
public class L11{//BETN1CS24109
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter size of first array: ");
int n1 = sc.nextInt();
int[] arr1 = new int[n1];
System.out.println("Enter elements of first array:");
for (int i = 0; i < n1; i++) {
arr1[i] = sc.nextInt();
}
System.out.print("Enter size of second array: ");
int n2 = sc.nextInt();
int[] arr2 = new int[n2];
System.out.println("Enter elements of second array:");
for (int i = 0; i < n2; i++) {
arr2[i] = sc.nextInt();
}
int[] merged = new int[n1 + n2];
for (int i = 0; i < n1; i++) merged[i] = arr1[i];
for (int i = 0; i < n2; i++) merged[n1 + i] = arr2[i];
System.out.println("Merged array:");
for (int val : merged) {
System.out.print(val + " ");
}
}
}
Output

5. Write a program to count the frequency of each element in an array.


Program
import java.util.Scanner;
public class L12{//BETN1CS24109
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 " + n + " integers:");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
boolean[] visited = new boolean[n];
System.out.println("Element frequencies:");
for (int i = 0; i < n; i++) {
if (visited[i]) continue;
int count = 1;
for (int j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
count++;
visited[j] = true;
}
}
System.out.println(arr[i] + " occurs " + count + " times");
}
}
}

Output

Exercise 4: String Manipulation


1. Write a program to input a string and check whether it is a palindrome.
Program
import java.util.Scanner;
public class L13 {//BETN1CS24109
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine();
String rev = "";
for (int i = str.length() - 1; i >= 0; i--) {
rev += str.charAt(i);
}
if (str.equalsIgnoreCase(rev)) {
System.out.println("The string is a palindrome.");
} else {
System.out.println("The string is NOT a palindrome.");
}
}
}

Output
2. Implement programs to demonstrate usage of various String methods such as
length(), charAt(), substring(), indexOf(), replace(), and
toUpperCase()/toLowerCase().
Program
public class L14 {//BETN1CS24109
public static void main(String[] args) {
String text = "Hello World";
System.out.println("Original String: " + text);
System.out.println("Length: " + text.length());
System.out.println("Character at index 4: " + text.charAt(4));
System.out.println("Substring (0-5): " + text.substring(0, 5));
System.out.println("Index of 'World': " + text.indexOf("World"));
System.out.println("Replace 'World' with 'Java': " + text.replace("World",
"Java"));
System.out.println("Uppercase: " + text.toUpperCase());
System.out.println("Lowercase: " + text.toLowerCase());
}
}
Output
3. Write a program to count the number of vowels and consonants in a given string.
Program
import java.util.Scanner;
public class L15 {//BETN1CS24109
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine().toLowerCase();
int vowels = 0, consonants = 0;
for (char ch : str.toCharArray()) {
if (Character.isLetter(ch)) {
if ("aeiou".indexOf(ch) != -1) {
vowels++;
} else {
consonants++;
}
}
}
System.out.println("Vowels: " + vowels);
System.out.println("Consonants: " + consonants);
}
}
Output
4. Create a program to compare two strings ignoring case differences.
Program

import java.util.Scanner;
public class L16{//BETN1CS24109
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first string: ");
String str1 = sc.nextLine();
System.out.print("Enter second string: ");
String str2 = sc.nextLine();
if (str1.equalsIgnoreCase(str2)) {
System.out.println("Both strings are equal (ignoring case).");
} else {
System.out.println("Strings are NOT equal.");
}
}
}

Output
5. Demonstrate the difference between String, StringBuffer, and StringBuilder in
concatenation and reversal operations.
Program
public class L17{//BETN1CS24109
public static void main(String[] args) {
String str = "Java";
String concatStr = str + " Programming";
System.out.println("String concatenation: " + concatStr);
StringBuffer sb = new StringBuffer("Java");
sb.append(" Programming");
System.out.println("StringBuffer concatenation: " + sb.toString());
sb.reverse();
System.out.println("StringBuffer reversed: " + sb);
StringBuilder sb2 = new StringBuilder("Java");
sb2.append(" Programming");
System.out.println("StringBuilder concatenation: " + sb2.toString());
sb2.reverse();
System.out.println("StringBuilder reversed: " + sb2);
}
}

Output

Exercise 5: Two-Dimensional Arrays


1. Write a program to input elements into a 2D array (matrix) and display it.
Program
import java.util.Scanner;
public class L18{//BETN1CS24109
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter rows: ");
int rows = sc.nextInt();
System.out.print("Enter columns: ");
int cols = sc.nextInt();
int[][] matrix = new int[rows][cols];
System.out.println("Enter matrix elements:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = sc.nextInt();
}
}
System.out.println("Matrix is:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}

Output

2. Implement matrix addition and multiplication programs.


Program
import java.util.Scanner;
public class L19 {//BETN1CS24109
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter rows: ");
int rows = sc.nextInt();
System.out.print("Enter columns: ");
int cols = sc.nextInt();
int[][] a = new int[rows][cols];
int[][] b = new int[rows][cols];
int[][] sum = new int[rows][cols];
System.out.println("Enter elements of first matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
a[i][j] = sc.nextInt();
}
}
System.out.println("Enter elements of second matrix:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
b[i][j] = sc.nextInt();
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sum[i][j] = a[i][j] + b[i][j];
}
}
System.out.println("Resultant Matrix (Addition):");
for (int[] row : sum) {
for (int val : row) {
System.out.print(val + " ");
}
System.out.println();
}
}
}
Output
Program
import java.util.Scanner;
public class L20 {//BETN1CS24109
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter rows of first matrix: ");
int r1 = sc.nextInt();
System.out.print("Enter cols of first matrix: ");
int c1 = sc.nextInt();
System.out.print("Enter rows of second matrix: ");
int r2 = sc.nextInt();
System.out.print("Enter cols of second matrix: ");
int c2 = sc.nextInt();
if (c1 != r2) {
System.out.println("Matrix multiplication not possible!");
return;
}
int[][] a = new int[r1][c1];
int[][] b = new int[r2][c2];
int[][] product = new int[r1][c2];
System.out.println("Enter elements of first matrix:");
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c1; j++) {
a[i][j] = sc.nextInt();
}
}
System.out.println("Enter elements of second matrix:");
for (int i = 0; i < r2; i++) {
for (int j = 0; j < c2; j++) {
b[i][j] = sc.nextInt();
}
}
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
product[i][j] = 0;
for (int k = 0; k < c1; k++) {
product[i][j] += a[i][k] * b[k][j];
}
}
}
System.out.println("Resultant Matrix (Multiplication):");
for (int[] row : product) {
for (int val : row) {
System.out.print(val + " ");
}
System.out.println();
}
}
}
Output
3. Write a program to find the transpose of a matrix.
Program
import java.util.Scanner;
public class L21 {//BETN1CS24109
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter rows: ");
int rows = sc.nextInt();
System.out.print("Enter columns: ");
int cols = sc.nextInt();
int[][] matrix = new int[rows][cols];
int[][] transpose = new int[cols][rows];
System.out.println("Enter matrix elements:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = sc.nextInt();
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
transpose[j][i] = matrix[i][j];
}
}
System.out.println("Transpose of matrix:");
for (int[] row : transpose) {
for (int val : row) {
System.out.print(val + " ");
}
System.out.println();
}
}
}

Output
4. Display the diagonal elements and sum them in a square matrix.
Program
import java.util.Scanner;
public class L22 {//BETN1CS24109
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter size of square matrix (n): ");
int n = sc.nextInt();
int[][] matrix = new int[n][n];
int sum = 0;
System.out.println("Enter matrix elements:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = sc.nextInt();
}
}
System.out.println("Diagonal elements:");
for (int i = 0; i < n; i++) {
System.out.print(matrix[i][i] + " ");
sum += matrix[i][i];
}
System.out.println("\nSum of diagonal elements: " + sum);
}
}

Output

You might also like