0% found this document useful (0 votes)
16 views25 pages

sodapdf-converted

The document contains a series of Java programming tasks related to matrices, dates, and strings. Each task includes a question, an algorithm, and a sample Java code implementation, covering operations like matrix addition, finding diagonal sums, checking leap years, and counting words starting with vowels. The document provides clear input and output examples for each program.
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)
16 views25 pages

sodapdf-converted

The document contains a series of Java programming tasks related to matrices, dates, and strings. Each task includes a question, an algorithm, and a sample Java code implementation, covering operations like matrix addition, finding diagonal sums, checking leap years, and counting words starting with vowels. The document provides clear input and output examples for each program.
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/ 25

---

Topic 1: Double Dimensional Array

---

1. Sum of Diagonal Elements

Question:
Write a Java program to input a 3×3 matrix and find the sum of its diagonal elements.

Algorithm:

1. Start.

2. Input a 3×3 matrix.

3. Initialize a variable sum to 0.

4. For each index i from 0 to 2, add the element at position [i][i] to sum.

5. Display the sum.

6. End.

Java Program Code:

import java.util.Scanner;
class DiagonalSum {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int[][] matrix = new int[3][3];
System.out.println("Enter a 3x3 matrix:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
matrix[i][j] = sc.nextInt();
}
}
int sum = 0;
for (int i = 0; i < 3; i++) {
sum += matrix[i][i];
}
System.out.println("Sum of diagonal elements: " + sum);
}
}

Input and Output Window:


Input:

123
456
789

Output:

Sum of diagonal elements: 15

---

2. Transpose of a Matrix

Question:
Write a Java program to find the transpose of a given 3×3 matrix.

Algorithm:

1. Start.

2. Input a 3×3 matrix.

3. For each element at [i][j], print the element at [j][i] to get the transpose.

4. End.

Java Program Code:

import java.util.Scanner;
class TransposeMatrix {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int[][] matrix = new int[3][3];
System.out.println("Enter a 3x3 matrix:");
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
matrix[i][j] = sc.nextInt();
}
}
System.out.println("Transpose of the matrix:");
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
System.out.print(matrix[j][i] + " ");
}
System.out.println();
}
}
}
Input and Output Window:
Input:

123
456
789

Output:

Transpose of the matrix:


147
258
369

---

3. Addition of Two Matrices

Question:
Write a Java program to add two 3×3 matrices.

Algorithm:

1. Start.

2. Input two 3×3 matrices.

3. Create a result matrix of the same dimensions.

4. For each cell [i][j], compute the sum of corresponding elements from the two matrices.

5. Display the result matrix.

6. End.

Java Program Code:

import java.util.Scanner;
class MatrixAddition {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int[][] matrix1 = new int[3][3];
int[][] matrix2 = new int[3][3];
int[][] result = new int[3][3];

System.out.println("Enter first 3x3 matrix:");


for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
matrix1[i][j] = sc.nextInt();
}
}

System.out.println("Enter second 3x3 matrix:");


for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
matrix2[i][j] = sc.nextInt();
}
}

for (int i = 0; i < 3; i++){


for (int j = 0; j < 3; j++){
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}

System.out.println("Resultant matrix after addition:");


for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
System.out.print(result[i][j] + " ");
}
System.out.println();
}
}
}

Input and Output Window:


Input:

Matrix1:
123
456
789

Matrix2:
987
654
321

Output:

Resultant matrix after addition:


10 10 10
10 10 10
10 10 10

---

4. Finding the Largest Element in a Matrix

Question:
Write a Java program to find the largest element in a 3×3 matrix.

Algorithm:

1. Start.

2. Input a 3×3 matrix.

3. Initialize a variable max with the first element of the matrix.

4. Traverse each element; if an element is greater than max, update max.

5. Display the largest element.

6. End.

Java Program Code:

import java.util.Scanner;
class LargestElement {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int[][] matrix = new int[3][3];
System.out.println("Enter a 3x3 matrix:");
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
matrix[i][j] = sc.nextInt();
}
}
int max = matrix[0][0];
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
if(matrix[i][j] > max)
max = matrix[i][j];
}
}
System.out.println("Largest element in the matrix: " + max);
}
}

Input and Output Window:


Input:

123
4 15 6
789

Output:
Largest element in the matrix: 15

---

5. Multiplication of Two Matrices

Question:
Write a Java program to multiply two 3×3 matrices.

Algorithm:

1. Start.

2. Input two 3×3 matrices.

3. Create a result matrix initialized to 0.

4. For each cell [i][j] in the result matrix, calculate the dot product of the i<sup>th</sup> row of the first
matrix and the j<sup>th</sup> column of the second matrix.

5. Display the product matrix.

6. End.

Java Program Code:

import java.util.Scanner;
class MatrixMultiplication {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int[][] matrix1 = new int[3][3];
int[][] matrix2 = new int[3][3];
int[][] result = new int[3][3];

System.out.println("Enter first 3x3 matrix:");


for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
matrix1[i][j] = sc.nextInt();
}
}

System.out.println("Enter second 3x3 matrix:");


for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
matrix2[i][j] = sc.nextInt();
}
}

for (int i = 0; i < 3; i++){


for (int j = 0; j < 3; j++){
result[i][j] = 0;
for (int k = 0; k < 3; k++){
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}

System.out.println("Product of the matrices:");


for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
System.out.print(result[i][j] + " ");
}
System.out.println();
}
}
}

Input and Output Window:


Input:

Matrix1:
123
456
789

Matrix2:
987
654
321

Output:

Product of the matrices:


30 24 18
84 69 54
138 114 90

---

Topic 2: Dates and Year

---

6. Checking for a Leap Year

Question:
Write a Java program to check if a given year is a leap year.

Algorithm:
1. Start.

2. Input a year.

3. If (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0), then the year is a leap year.

4. Display the result.

5. End.

Java Program Code:

import java.util.Scanner;
class LeapYear {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a year: ");
int year = sc.nextInt();

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))


System.out.println(year + " is a leap year.");
else
System.out.println(year + " is not a leap year.");
}
}

Input and Output Window:


Input:

2024

Output:

2024 is a leap year.

---

7. Finding the Day of the Week for a Given Date

Question:
Write a Java program to find the day of the week for a given date (in the format dd mm yyyy).

Algorithm:

1. Start.
2. Input day, month, and year.

3. Adjust the month and year if month is January or February (as required by Zeller’s Congruence).

4. Compute the day using Zeller’s Congruence formula.

5. Map the computed value to the corresponding day.

6. Display the day of the week.

7. End.

Java Program Code:

import java.util.Scanner;
class DayOfWeek {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter date (dd mm yyyy): ");
int d = sc.nextInt();
int m = sc.nextInt();
int y = sc.nextInt();

// Adjust month and year for Zeller's Congruence


if(m < 3) {
m += 12;
y -= 1;
}

int k = y % 100;
int j = y / 100;
int h = (d + (13 * (m + 1)) / 5 + k + (k / 4) + (j / 4) + 5 * j) % 7;

String day = "";


switch(h) {
case 0: day = "Saturday"; break;
case 1: day = "Sunday"; break;
case 2: day = "Monday"; break;
case 3: day = "Tuesday"; break;
case 4: day = "Wednesday"; break;
case 5: day = "Thursday"; break;
case 6: day = "Friday"; break;
}
System.out.println("Day of the week: " + day);
}
}

Input and Output Window:


Input:

15 8 2025

Output:

Day of the week: Friday

---

8. Extracting and Displaying the Month from a Given Date

Question:
Write a Java program to extract and display the month from a date in the format dd-mm-yyyy.

Algorithm:

1. Start.

2. Input the date as a string.

3. Split the string by "-" to extract the month component.

4. Convert the month number to its corresponding month name using a switch-case.

5. Display the month name.

6. End.

Java Program Code:

import java.util.Scanner;
class ExtractMonth {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter date (dd-mm-yyyy): ");
String date = sc.nextLine();
String[] parts = date.split("-");
int monthNum = Integer.parseInt(parts[1]);
String month = "";
switch(monthNum) {
case 1: month = "January"; break;
case 2: month = "February"; break;
case 3: month = "March"; break;
case 4: month = "April"; break;
case 5: month = "May"; break;
case 6: month = "June"; break;
case 7: month = "July"; break;
case 8: month = "August"; break;
case 9: month = "September"; break;
case 10: month = "October"; break;
case 11: month = "November"; break;
case 12: month = "December"; break;
default: month = "Invalid month"; break;
}
System.out.println("Month: " + month);
}
}

Input and Output Window:


Input:

15-08-2025

Output:

Month: August

---

9. Calculating Age from a Given Birth Date

Question:
Write a Java program to calculate the age of a person given their birth date (dd mm yyyy) and the current
date (dd mm yyyy).

Algorithm:

1. Start.

2. Input birth date (day, month, year).

3. Input current date (day, month, year).

4. Compute the difference in years.

5. If the current month and day are before the birth month and day, subtract 1 from the age.

6. Display the calculated age.

7. End.

Java Program Code:


import java.util.Scanner;
class CalculateAge {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter birth date (dd mm yyyy): ");
int bd = sc.nextInt();
int bm = sc.nextInt();
int by = sc.nextInt();

System.out.print("Enter current date (dd mm yyyy): ");


int cd = sc.nextInt();
int cm = sc.nextInt();
int cy = sc.nextInt();

int age = cy - by;


if(cm < bm || (cm == bm && cd < bd)){
age--;
}
System.out.println("Age: " + age);
}
}

Input and Output Window:


Input:

Birth Date: 15 08 2000


Current Date: 14 08 2025

Output:

Age: 24

---

10. Counting the Number of Days Between Two Dates

Question:
Write a Java program to count the number of days between two dates (format: dd mm yyyy).
Note: For simplicity, assume both dates are in the same year.

Algorithm:

1. Start.

2. Input the first and second dates.

3. Convert each date to its day number in the year (taking leap years into account).

4. Compute the absolute difference between the two day numbers.


5. Display the number of days between the dates.

6. End.

Java Program Code:

import java.util.Scanner;
class DaysBetweenDates {
// Function to calculate day number in the year
public static int dayOfYear(int d, int m, int y) {
int[] daysInMonth = {31,28,31,30,31,30,31,31,30,31,30,31};
// Adjust for leap year
if((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0)) {
daysInMonth[1] = 29;
}
int dayNum = 0;
for(int i = 0; i < m - 1; i++){
dayNum += daysInMonth[i];
}
dayNum += d;
return dayNum;
}

public static void main(String args[]) {


Scanner sc = new Scanner(System.in);
System.out.print("Enter first date (dd mm yyyy): ");
int d1 = sc.nextInt();
int m1 = sc.nextInt();
int y1 = sc.nextInt();

System.out.print("Enter second date (dd mm yyyy): ");


int d2 = sc.nextInt();
int m2 = sc.nextInt();
int y2 = sc.nextInt();

if(y1 != y2) {
System.out.println("This program assumes both dates are in the same year.");
return;
}

int dayNum1 = dayOfYear(d1, m1, y1);


int dayNum2 = dayOfYear(d2, m2, y2);
int diff = Math.abs(dayNum2 - dayNum1);
System.out.println("Number of days between dates: " + diff);
}
}

Input and Output Window:


Input:

First Date: 01 03 2025


Second Date: 15 03 2025

Output:

Number of days between dates: 14

---

Topic 3: String

---

11. Counting Words Starting with a Vowel

Question:
Write a Java program to count the number of words in a sentence that start with a vowel.

Algorithm:

1. Start.

2. Input a sentence.

3. Split the sentence into words.

4. For each word, check if its first character (ignoring case) is a vowel (a, e, i, o, u).

5. Count the words meeting the criterion.

6. Display the count.

7. End.

Java Program Code:

import java.util.Scanner;
class CountVowelWords {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String sentence = sc.nextLine();
String[] words = sentence.split(" ");
int count = 0;
for(String word : words) {
if(word.length() > 0) {
char ch = Character.toLowerCase(word.charAt(0));
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
count++;
}
}
System.out.println("Number of words starting with a vowel: " + count);
}
}

Input and Output Window:


Input:

Apple is a fruit and orange is a color

Output:

Number of words starting with a vowel: 5

---

12. Checking if a String is a Palindrome

Question:
Write a Java program to check if a given string is a palindrome.

Algorithm:

1. Start.

2. Input a string.

3. Reverse the string.

4. Compare the reversed string with the original (ignoring case).

5. Display whether it is a palindrome.

6. End.

Java Program Code:

import java.util.Scanner;
class PalindromeString {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine();
String rev = new StringBuilder(str).reverse().toString();
if(str.equalsIgnoreCase(rev))
System.out.println("The string is a palindrome.");
else
System.out.println("The string is not a palindrome.");
}
}

Input and Output Window:


Input:

Madam

Output:

The string is a palindrome.

---

13. Reversing Each Word in a Sentence

Question:
Write a Java program to reverse each word in a sentence.

Algorithm:

1. Start.

2. Input a sentence.

3. Split the sentence into words.

4. For each word, reverse it and build a new sentence.

5. Display the modified sentence.

6. End.

Java Program Code:

import java.util.Scanner;
class ReverseWords {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String sentence = sc.nextLine();
String[] words = sentence.split(" ");
String result = "";
for(String word : words) {
String rev = new StringBuilder(word).reverse().toString();
result += rev + " ";
}
System.out.println("Reversed words sentence: " + result.trim());
}
}

Input and Output Window:


Input:

Hello World

Output:

Reversed words sentence: olleH dlroW

---

14. Converting a Sentence into Title Case

Question:
Write a Java program to convert a sentence into title case (capitalize the first letter of each word).

Algorithm:

1. Start.

2. Input a sentence.

3. Split the sentence into words.

4. For each word, convert the first character to uppercase and the rest to lowercase.

5. Reconstruct and display the sentence.

6. End.

Java Program Code:

import java.util.Scanner;
class TitleCase {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String sentence = sc.nextLine();
String[] words = sentence.split(" ");
String result = "";
for(String word : words) {
if(word.length() > 0) {
result += Character.toUpperCase(word.charAt(0)) + word.substring(1).toLowerCase() + " ";
}
}
System.out.println("Title Case: " + result.trim());
}
}

Input and Output Window:


Input:

this is a sample sentence.

Output:

Title Case: This Is A Sample Sentence.

---

15. Counting the Frequency of Each Character in a String

Question:
Write a Java program to count the frequency of each character (excluding spaces) in a given string.

Algorithm:

1. Start.

2. Input a string.

3. Use a data structure (like a LinkedHashMap) to store character frequencies.

4. Loop through each character in the string; if it is not a space, update its count.

5. Display the frequency of each character.

6. End.

Java Program Code:

import java.util.Scanner;
import java.util.LinkedHashMap;
import java.util.Map;
class CharacterFrequency {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine();
Map<Character, Integer> freq = new LinkedHashMap<>();
for(char ch : str.toCharArray()){
if(ch != ' '){
freq.put(ch, freq.getOrDefault(ch, 0) + 1);
}
}
for(Map.Entry<Character, Integer> entry : freq.entrySet()){
System.out.println(entry.getKey() + " : " + entry.getValue());
}
}
}

Input and Output Window:


Input:

hello world

Output:

h:1
e:1
l:3
o:2
w:1
r:1
d:1

---

Topic 4: Number Pattern

---

16. Right-Angled Triangle Pattern

Question:
Write a Java program to print a right-angled triangle pattern of numbers.

Algorithm:

1. Start.

2. Input the number of rows.

3. Use a nested loop where the outer loop controls the rows and the inner loop prints increasing numbers.
4. Display the pattern.

5. End.

Java Program Code:

import java.util.Scanner;
class RightAngledTriangle {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of rows: ");
int rows = sc.nextInt();
int num = 1;
for(int i = 1; i <= rows; i++){
for(int j = 1; j <= i; j++){
System.out.print(num + " ");
num++;
}
System.out.println();
}
}
}

Input and Output Window:


Input:

Output:

1
23
456
7 8 9 10
11 12 13 14 15

---

17. Floyd’s Triangle Pattern

Question:
Write a Java program to print Floyd’s triangle pattern.

Algorithm:

1. Start.

2. Input the number of rows.


3. Initialize a counter to 1.

4. For each row, print as many numbers as the row number and increment the counter.

5. Display the pattern.

6. End.

Java Program Code:

import java.util.Scanner;
class FloydTriangle {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of rows: ");
int n = sc.nextInt();
int num = 1;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= i; j++){
System.out.print(num + " ");
num++;
}
System.out.println();
}
}
}

Input and Output Window:


Input:

Output:

1
23
456
7 8 9 10
11 12 13 14 15

---

18. Diamond Pattern of Numbers

Question:
Write a Java program to print a diamond pattern of numbers.

Algorithm:
1. Start.

2. Input the number of rows for half of the diamond.

3. For the upper half, print spaces and then numbers in increasing order.

4. For the lower half, print the pattern in reverse.

5. End.

Java Program Code:

import java.util.Scanner;
class DiamondPattern {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows for half diamond: ");
int n = sc.nextInt();

// Upper half of diamond


for (int i = 1; i <= n; i++){
for (int j = 1; j <= n - i; j++){
System.out.print(" ");
}
for (int k = 1; k <= i; k++){
System.out.print(k + " ");
}
System.out.println();
}
// Lower half of diamond
for (int i = n - 1; i >= 1; i--){
for (int j = 1; j <= n - i; j++){
System.out.print(" ");
}
for (int k = 1; k <= i; k++){
System.out.print(k + " ");
}
System.out.println();
}
}
}

Input and Output Window:


Input:

Output (approximate spacing):


1
12
123
1234
12345
1234
123
12
1

---

19. Pascal’s Triangle Pattern

Question:
Write a Java program to print Pascal's triangle pattern.

Algorithm:

1. Start.

2. Input the number of rows.

3. For each row, print the required spaces.

4. Calculate the binomial coefficient for each position using the formula:
number = number * (row - index) / (index + 1)

5. Display the row.

6. End.

Java Program Code:

import java.util.Scanner;
class PascalsTriangle {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of rows: ");
int n = sc.nextInt();
for (int i = 0; i < n; i++){
int number = 1;
for (int j = 0; j < n - i; j++){
System.out.print(" ");
}
for (int k = 0; k <= i; k++){
System.out.print(number + " ");
number = number * (i - k) / (k + 1);
}
System.out.println();
}
}
}

Input and Output Window:


Input:

Output:

1
11
121
1331
14641

---

20. Hollow Pyramid Pattern

Question:
Write a Java program to print a hollow pyramid pattern.

Algorithm:

1. Start.

2. Input the number of rows.

3. For each row, print appropriate spaces followed by stars so that only the border stars (first and last in a
row) and all stars in the last row are printed.

4. Display the pattern.

5. End.

Java Program Code:

import java.util.Scanner;
class HollowPyramid {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of rows: ");
int n = sc.nextInt();
for (int i = 1; i <= n; i++){
// Print spaces before the pyramid
for (int j = i; j < n; j++){
System.out.print(" ");
}
// Print stars and inner spaces
for (int k = 1; k <= (2 * i - 1); k++){
if(i == n || k == 1 || k == (2 * i - 1))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}

Input and Output Window:


Input:

Output (approximate):

*
**
* *
* *
*********

---

You might also like