Posts

possible palindrome or not

Write a java program to check that string is possible to make palindrome or not......... import java.util.Scanner; public class PossiblePalindrome { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter a String : "); String str=sc.nextLine(); char[] arr=str.toCharArray(); int[] index=indexNumber(arr); boolean res=possPalin(index); if(res) { System.out.println("yes it is possible to make palindrome"); } else { System.out.println("no it is not possible to make palindrome "); } } static int[] indexNumber(char[] arr) { int index[]=new int[26]; for(int i=0;i<arr.length;i++) { char ch=arr[i]; if(ch>64 && ch<91) { index[ch-65]++; } else if(ch>=97 && ch<=122) { index[ch-97]++; } } return index; } static boolean possPalin(int arr[]) { int count=0; fo

java program to Replace a Word in String

Write a java program to Replace a word in String : package String; import java.util.Scanner; public class ReplaceString { public static void main(String[] args) { Scanner in=new Scanner(System.in); String str="Hello <<UserName>>, How are you?"; String uname=null; do { System.out.println("Enter user name :"); uname=in.nextLine(); if(uname.length()>3 && uname.length()<11) break; else { System.out.println("Invalid user please ReEnter : "); continue; } }while(uname.length()<3 || uname.length()>10); System.out.println(uname); str=str.replaceAll("<<UserName>>", uname); System.out.println(str); } }

program to calculate product of each column in matrix

Write a java program to calculate product of each column in matrix :  import java.util.Random; import java.util.Scanner; public class ColumnWiseProduct { public static void main(String[] args) { ColumnWiseProduct cwp=new ColumnWiseProduct(); int arr[][]=cwp.readmatrix(); cwp.dispMat(arr); int prod[]=cwp.prodColWise(arr); System.out.println("Entered matrix Column wise product is :"); cwp.dispSingleArray(prod); } int[] prodColWise(int mat[][]){ int prod[]=new int[mat[0].length]; for(int i=0;i<mat[0].length;i++) { prod[i]=1; for(int j=0;j<mat.length;j++) { prod[i]=prod[i]*mat[j][i]; } } return prod; } int[][] readmatrix(){ Scanner sc=new Scanner(System.in); System.out.println("Enter Row size and column size :"); int row=sc.nextInt(); int col=sc.nextInt(); int mat[][]=new int[row][col]; System.out.println("Enter Elements in Table : "); for(int i=0;i<row;i++) { for(int

calculate sum column wise in a matrix:

Write a java program to calculate sum column wise in a matrix: import java.util.Random; import java.util.Scanner; public class ColumnWiseLeast { public static void main(String[] args) { int mat[][]=readmatrix(); dispMat(mat); System.out.println("Sum of Diagonal element is : "); int arr[]=leastColWise(mat); dispSingleArray(arr); } static int[] leastColWise(int mat[][]) { int least[]=new int[mat[0].length]; for(int i=0;i<mat[0].length;i++) { least[i]=mat[0][i]; for(int j=0;j<mat.length;j++) { if(least[i]>mat[j][i]) { least[i]=mat[j][i]; } } }return least; } static int[][] readmatrix(){ Random rx=new Random(); Scanner sc=new Scanner(System.in); System.out.println("Enter Row size and column size :"); int row=sc.nextInt(); int col=sc.nextInt(); int mat[][]=new int[row][col]; System.out.println("Enter Elements in Table : "); for(int i=0;i<row;i++) { fo

Write a java program to sum of Diagonal elements in matrix:

Write a java program to sum of Diagonal elements in matrix: import java.util.Random; import java.util.Scanner; public class SumOfDiagonalElements { public static void main(String[] args) { int arr[][]=readmatrix(); dispMat(arr); int sum[]=sumOfDiagonal(arr); System.out.println("primary Diagonal element sum is : "+sum[0]); System.out.println("Secondary Diagonal element sum is :"+sum[1]); } static int[] sumOfDiagonal(int mat[][]) { int []sum=new int[2]; int n=mat.length; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if(i==j) { sum[0]=sum[0]+mat[i][j]; } if(i+j==n-1) { sum[1]=sum[1]+mat[i][j]; } } } return sum; } static int[][] readmatrix(){ Random rx=new Random(); Scanner sc=new Scanner(System.in); System.out.println("Enter Row size and column size :"); int row=sc.nextInt(); int col=sc.nextInt(); int mat[][]=new int[row]

Rotate matrix /2-D array 90 degrre left and Right

Write a java program to Rotate matrix 90 degree left and rotate 90 degree right :  import java.util.Random; import java.util.Scanner; public class RotateMatrix { public static void main(String[] args) { System.out.println("Enter matrix : "); int arr[][]=readmatrix(); System.out.println("Matrix is : "); dispMat(arr); int arr2[][]=rotate90Left(arr); System.out.println("90 Degree left rotate matrix is :"); dispMat(arr2); System.out.println("90 degree right rotate Matrix is :"); arr=rotate90Right(arr); dispMat(arr); } static int[][] rotate90Left(int mat[][]){ ColumnWiseReverse cw=new ColumnWiseReverse(); mat=transposeMat(mat); cw.colWiseReverse(mat); return mat; } static int[][] rotate90Right(int mat[][]){ RowWiseReverse rw=new RowWiseReverse(); mat=transposeMat(mat); rw.rowWiseReverse(mat); return mat; } static int[][] transposeMat(int m[][])

Count negative number in a matrix

Write a java program to count negative number in a matrix : import java.util.Scanner; public class CountNegValue { public static void main(String[] args) { int a[][]=readmatrix(); dispMat(a); int count=countNeg(a); System.out.println("Entered negative number is : "+count); } static int[][] readmatrix(){ Scanner sc=new Scanner(System.in); System.out.println("Enter Row size and column size :"); int row=sc.nextInt(); int col=sc.nextInt(); int mat[][]=new int[row][col]; System.out.println("Enter Elements in Table : "); for(int i=0;i<row;i++) { for(int j=0;j<col;j++) { mat[i][j]=sc.nextInt(); } }return mat; } static void dispMat(int mat[][]) {   for(int i=0;i<mat.length;i++) { for(int j=0;j<mat[i].length;j++) { System.out.print(mat[i][j]+" "); }System.out.println(); }   } static int countNeg(int mat[][]) { int count=0;