sodapdf-converted
sodapdf-converted
---
Question:
Write a Java program to input a 3×3 matrix and find the sum of its diagonal elements.
Algorithm:
1. Start.
4. For each index i from 0 to 2, add the element at position [i][i] to sum.
6. End.
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);
}
}
123
456
789
Output:
---
2. Transpose of a Matrix
Question:
Write a Java program to find the transpose of a given 3×3 matrix.
Algorithm:
1. Start.
3. For each element at [i][j], print the element at [j][i] to get the transpose.
4. End.
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:
---
Question:
Write a Java program to add two 3×3 matrices.
Algorithm:
1. Start.
4. For each cell [i][j], compute the sum of corresponding elements from the two matrices.
6. End.
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];
Matrix1:
123
456
789
Matrix2:
987
654
321
Output:
---
Question:
Write a Java program to find the largest element in a 3×3 matrix.
Algorithm:
1. Start.
6. End.
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);
}
}
123
4 15 6
789
Output:
Largest element in the matrix: 15
---
Question:
Write a Java program to multiply two 3×3 matrices.
Algorithm:
1. Start.
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.
6. End.
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];
Matrix1:
123
456
789
Matrix2:
987
654
321
Output:
---
---
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.
5. End.
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();
2024
Output:
---
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).
7. End.
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();
int k = y % 100;
int j = y / 100;
int h = (d + (13 * (m + 1)) / 5 + k + (k / 4) + (j / 4) + 5 * j) % 7;
15 8 2025
Output:
---
Question:
Write a Java program to extract and display the month from a date in the format dd-mm-yyyy.
Algorithm:
1. Start.
4. Convert the month number to its corresponding month name using a switch-case.
6. End.
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);
}
}
15-08-2025
Output:
Month: August
---
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.
5. If the current month and day are before the birth month and day, subtract 1 from the age.
7. End.
Output:
Age: 24
---
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.
3. Convert each date to its day number in the year (taking leap years into account).
6. End.
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;
}
if(y1 != y2) {
System.out.println("This program assumes both dates are in the same year.");
return;
}
Output:
---
Topic 3: String
---
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.
4. For each word, check if its first character (ignoring case) is a vowel (a, e, i, o, u).
7. End.
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);
}
}
Output:
---
Question:
Write a Java program to check if a given string is a palindrome.
Algorithm:
1. Start.
2. Input a string.
6. End.
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.");
}
}
Madam
Output:
---
Question:
Write a Java program to reverse each word in a sentence.
Algorithm:
1. Start.
2. Input a sentence.
6. End.
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());
}
}
Hello World
Output:
---
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.
4. For each word, convert the first character to uppercase and the rest to lowercase.
6. End.
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());
}
}
Output:
---
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.
4. Loop through each character in the string; if it is not a space, update its count.
6. End.
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());
}
}
}
hello world
Output:
h:1
e:1
l:3
o:2
w:1
r:1
d:1
---
---
Question:
Write a Java program to print a right-angled triangle pattern of numbers.
Algorithm:
1. Start.
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.
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();
}
}
}
Output:
1
23
456
7 8 9 10
11 12 13 14 15
---
Question:
Write a Java program to print Floyd’s triangle pattern.
Algorithm:
1. Start.
4. For each row, print as many numbers as the row number and increment the counter.
6. End.
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();
}
}
}
Output:
1
23
456
7 8 9 10
11 12 13 14 15
---
Question:
Write a Java program to print a diamond pattern of numbers.
Algorithm:
1. Start.
3. For the upper half, print spaces and then numbers in increasing order.
5. End.
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();
---
Question:
Write a Java program to print Pascal's triangle pattern.
Algorithm:
1. Start.
4. Calculate the binomial coefficient for each position using the formula:
number = number * (row - index) / (index + 1)
6. End.
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();
}
}
}
Output:
1
11
121
1331
14641
---
Question:
Write a Java program to print a hollow pyramid pattern.
Algorithm:
1. Start.
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.
5. End.
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();
}
}
}
Output (approximate):
*
**
* *
* *
*********
---