For Interview

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 26

1.

FACTORIAL

2.PRIME NUMBER

3.INTEGER REVERSE
4.STRING REVERSE

5.INTEGER PALINDROME

6.STRING PALINDROME
7.ARMSTRONG

8.FIBINOCCI

9.LEAP YEAR
10.SWAPPING OF TWO NUMBERS

11.COUNTING NO OF DIGITS IN INTEGER

12. SQUARE ROOT


13.COUNTING ODD EVEN DIGITS IN INTEGER

14.SUM OF DIGITS IN INTEGER


15.LARGEST AMOUNG THREE NUMBERS

ARRAYS RELATED PROGRAMS:


16. SUM OF ARRAY

17.EVEN ODD IN ARRAY

18.ARRAYS EQUAL
19.MISSING ELEMENT IN ARRAY

20.MAX AND MIN IN ARRAY

21.SEARCH ELEMENT IN ARRAY


22.SORT ARRAY

23.UNIQUE ELEMENTS IN ARRAY

24.DUPLICATES IN ARRAY

25.SECOND LARGEST IN ARRAY


26.REVERSING ARRAY

27.REMOVE ELEMENT FROM ARRAY


28. Adding and Subtracting Two Matrices in Java
public class MatrixOperations {
// Function to add two matrices
private static int[][] addMatrices(int[][] A, int[][] B) {
int rows = A.length;
int cols = A[0].length;
int[][] result = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = A[i][j] + B[i][j];
}}
return result;
}
// Function to subtract two matrices
private static int[][] subtractMatrices(int[][] A, int[][] B) {
int rows = A.length;
int cols = A[0].length;
int[][] result = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = A[i][j] - B[i][j];
}}
return result;
}
// Function to print a matrix
private static void printMatrix(int[][] matrix) {
for (int[] row : matrix) {
for (int value : row) {
System.out.print(value + " ");
}
System.out.println();
}}
public static void main(String[] args) {
// Example matrices
int[][] matrixA = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int[][] matrixB = { { 9, 8, 7 }, { 6, 5, 4 }, { 3, 2, 1 } };
System.out.println("Matrix A:");
printMatrix(matrixA);
System.out.println("\nMatrix B:");
printMatrix(matrixB);
// Add matrices
int[][] sumMatrix = addMatrices(matrixA, matrixB);
System.out.println("\nSum of matrices A and B:");
printMatrix(sumMatrix);
// Subtract matrices
int[][] diffMatrix = subtractMatrices(matrixA, matrixB);
System.out.println("\nDifference of matrices A and B:");
printMatrix(diffMatrix);
}
}

29.Multiplication of two matrices


public class MatrixMultiplication {
public static int[][] multiplyMatrices(int[][] matrix1, int[][] matrix2) {
int rows1 = matrix1.length;
int cols1 = matrix1[0].length;
int cols2 = matrix2[0].length;
int[][] result = new int[rows1][cols2];
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
for (int k = 0; k < cols1; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
return result;
}
public static void main(String[] args) {
int[][] matrix1 = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
int[][] matrix2 = {
{ 9, 8, 7 },
{ 6, 5, 4 },
{ 3, 2, 1 }
};
int[][] result = multiplyMatrices(matrix1, matrix2);
// Displaying the result matrix
for (int i = 0; i < result.length; i++) {
for (int j = 0; j < result[0].length; j++) {
System.out.print(result[i][j] + " ");
}
System.out.println();
}
}
}

30.Transposing matrices
public class MatrixTranspose {
public static void main(String[] args) {
int[][] originalMatrix = {
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};

int rows = originalMatrix.length;


int cols = originalMatrix[0].length;

// Transpose the matrix


int[][] transposedMatrix = new int[cols][rows];

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


for (int j = 0; j < cols; j++) {
transposedMatrix[j][i] = originalMatrix[i][j];
}
}

// Print the original matrix


System.out.println("Original Matrix:");
printMatrix(originalMatrix);

// Print the transposed matrix


System.out.println("\nTransposed Matrix:");
printMatrix(transposedMatrix);
}

// Helper method to print a matrix


private static void printMatrix(int[][] matrix) {
for (int[] row : matrix) {
for (int num : row) {
System.out.print(num + " ");
}
System.out.println();
}
}
}

31.DUPLICATES IN STRING ARRAY


32.UNIQUE CHARS IN STRING

33.DUPLICATE CHARS IN STRING

34.COUNT WORDS IN SENTENCE(STRING)


35.ANAGRAM

36.PERMUTATIONS

37.VOWELS IN STRING
38.OCCURENCE OF CHAR

39.REPLACE CHAR IN STRING

40.REMOVE WHITESPACES IN STRING

41.ROTATIONS OF TWO STRINGS


42.BUBBLE SORT

43.INSERTION SORT
Design pattrens:

1. Different Types of Pyramid Patterns


1 Left Half Pyramid Pattern Using *

2.Right Inverted Half Pyramid Pattern Using ”*”


3.Left Half Inverted Pyramid Pattern Using *

4.Right Half Pyramid Pattern Using *


5.Half Pyramid Pattern
6.Inverted Half Pyramid Pattern

7. Left Half Pyramid Pattern Using Numbers

8. Left Half Inverted Pyramid Pattern Using Numbers

9. Inverted Pyramid Pattern Using Numbers


2. Different Types of Diamond Patterns

1.Right Half Diamond Pattern


int rows = 5;
// Upper half of the right half diamond
for (int i = 0; i < rows; i++) {
// Print leading spaces
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
// Print stars
for (int k = 0; k < rows - i; k++) {
System.out.print("*");
}
System.out.println();
}
// Lower half of the right half diamond
for (int i = 1; i < rows; i++) {
// Print leading spaces
for (int j = 0; j < rows - i - 1; j++) {
System.out.print(" ");
}
// Print stars
for (int k = 0; k <= i; k++) {
System.out.print("*");
}
System.out.println();
}

2.Left Half Diamond Pattern


int rows = 5;

// Upper half of the left half diamond


for (int i = 0; i < rows; i++) {
// Print stars
for (int j = 0; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}

// Lower half of the left half diamond


for (int i = rows - 2; i >= 0; i--) {
// Print stars
for (int j = 0; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}

3.Full Diamond Pattern

int rows = 5;
int spaces = rows - 1;
// Upper half of the diamond
for (int i = 0; i < rows; i++) {
// Print leading spaces
for (int j = 0; j < spaces - i; j++) {
System.out.print(" ");
}
// Print stars
for (int k = 0; k < 2 * i + 1; k++) {
System.out.print("*");
}
System.out.println();
}
// Lower half of the diamond
for (int i = rows - 2; i >= 0; i--) {
// Print leading spaces
for (int j = 0; j < spaces - i; j++) {
System.out.print(" ");
}
// Print stars
for (int k = 0; k < 2 * i + 1; k++) {
System.out.print("*");
}
System.out.println();
}

3.Pascal’s Triangle
public class PascalsTriangle {

public static void main(String[] args) {


int numRows = 5;

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


// Print leading spaces
for (int j = 0; j < numRows - i; j++) {
System.out.print(" ");
}

int number = 1;
for (int j = 0; j <= i; j++) {
// Print the current number
System.out.print(number + " ");

// Update the number for the next iteration


number = number * (i - j) / (j + 1);
}

System.out.println();
}
}
}

4.Print Floyd’s Triangle


int rows = 4; // You can adjust the number of rows as needed
int number = 1;

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


for (int j = 1; j <= i; j++) {
System.out.print(number + " ");
number++;
}
System.out.println();
}

5.Different Types of Rhombus Patterns


1. Left inclined Rhombus Pattern
int rows = 5;

// Upper half of the left-inclined rhombus


for (int i = 0; i < rows; i++) {
// Print leading spaces
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
// Print stars
for (int k = 0; k < rows; k++) {
System.out.print("*");
}
System.out.println();
}
// Lower half of the left-inclined rhombus
for (int i = rows - 2; i >= 0; i--) {
// Print leading spaces
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
// Print stars
for (int k = 0; k < rows; k++) {
System.out.print("*");
}
System.out.println();
}

2.Right Inclined Rhombus Pattern

int rows = 5;

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


// Print leading spaces
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
// Print stars
for (int k = 0; k < rows; k++) {
System.out.print("*");
}

System.out.println();
}

6.Different Types of Rectangle Patterns


1.Hollow Rectangle
int rows = 5;
int columns = 8;

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


for (int j = 1; j <= columns; j++) {
// Print '*' for the first and last rows or columns
// or for the rows where j is 1 or columns where i is 1 or rows
// or for the rows where j is equal to columns or columns where i is equal to rows
if (i == 1 || i == rows || j == 1 || j == columns) {
System.out.print("* ");
} else {
System.out.print(" ");
}
}
System.out.println();
}

2.Solid Rectangle
int rows = 4;
int columns = 6;

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


for (int j = 0; j < columns; j++) {
System.out.print("* ");
}
System.out.println();
}

You might also like