// Java program to Convert String into reverse String
public static void main(String[] args) {
String a = "This is my country , And I love my Country";
String b="";
for (int i = a.length()-1; i > 0; i--) {
b+=a.charAt(i);
}
System.out.println(b +"\n" +a);
}
*********************************************************************************
// Java program to reverse integer array
public static void main(String[] args) {
int[] inputArray = { 2, 4, 6, 3, 8, 9, 44, 3 ,22};
int [] OutputArray = new int [inputArray .length]; // define lenght of int array
int j =0; // define counter for OutputArray
// take element from end of inputArray and add in front of outputArray
for (int i =a.length-1; i>0;i-- ){
b[j]= a[i];
System.out.println(a[i]+"**"+b[j]);
j++; // Increase a counter
}
}
*********************************************************************************
// Java program to reverse integer array with binary
*********************************************************************************
//// Java program to reverse words of String
// input =>"This is my country And I love my Country";
//output => "sihT si ym yrtnuoc dnA I evol ym yrtnuoC"
public static void main(String[] args) {
String inputString = "This is my country And I love my Country"; //input string
String[] words = inputString.split(" "); //split words, now -->This , is , my , country,..etc
String reverseString = ""; // get output in this.
// first loop to get get each words one by one from string
for (int i = 0; i < words.length; i++) {
String word = words[i]; //initi, word contain -"This" in next iteration it's "is " & so on
String reverseWord = ""; // get a output (reverse word)
//second loop is for to reverse a word
for (int j = word.length() - 1; j >= 0; j--) {
reverseWord = reverseWord + word.charAt(j); //append chars in reverse manner
}
reverseString = reverseString + reverseWord + " "; //appends words as process from inner loop
}
System.out.println(inputString);
System.out.println(reverseString);
}
*********************************************************************************
// Prefix and post fix
public static void main(String[] args){
int x;
int a = 100;
System.out.println(a-- +"==="+--a);
//100 99 98
// values postfix ,prefix 99 is became 98
x=a-- - --a;
System.out.println(x); //output is -->2
}
}
*********************************************************************************
//To Find Duplicate Elements In An Array
*********************************************************************************
// Find Second Largest Number In An Integer Array :
public static void main(String[] args) {
public static void main(String[] args) {
String inputString = "Hi this is good tested Nagard";
*********************************************************************************
//Find The Percentage Of Uppercase Letters, Lowercase Letters, Digits And Other Special Characters In A String
public static void main(String[] args) {
String inputString = "Hi My email id is Test@gmail.com 112 time appear";
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); // Make ready compiler to ready from data
System.out.println("Enter The Number Of Rows");
int row = sc.nextInt();
System.out.println("Enter The Number Of Columns");
int cols = sc.nextInt();
int[][] matrix1 = new int[row][cols]; // Define matrix
int[][] matrix2 = new int[row][cols];
int[][] sum = new int[row][cols];
System.out.println("Enter The Data For First Matrix :");
for (int i = 0; i < row; i++)
{
for (int j = 0; j < cols; j++)
{
matrix1[i][j] = sc.nextInt();
}
}
System.out.println("Enter The Data For Second Matrix :");
for (int i = 0; i < row; i++)
{
for (int j = 0; j < cols; j++)
{
matrix2[i][j] = sc.nextInt();
}
}
System.out.println("Sum = ");
for (int i = 0; i < row; i++)
{
for (int j = 0; j < cols; j++)
{
sum[i][j] = matrix1[i][j] + matrix2[i][j];
System.out.print(sum[i][j]+"\t");
}
System.out.println();
}
*********************************************************************************
// Multiplication of Matrix
*********************************************************************************
// Transpose of Matrix (Rows into col and col into rows)
********************************************************************************
//To Reverse And Add A Number Until You Get A Palindrome (number whose reverse == original number . ie 123321 ):
public static void main(String[] args) {
String a = "This is my country , And I love my Country";
String b="";
for (int i = a.length()-1; i > 0; i--) {
b+=a.charAt(i);
}
System.out.println(b +"\n" +a);
}
*********************************************************************************
// Java program to reverse integer array
public static void main(String[] args) {
int[] inputArray = { 2, 4, 6, 3, 8, 9, 44, 3 ,22};
int [] OutputArray = new int [inputArray .length]; // define lenght of int array
int j =0; // define counter for OutputArray
// take element from end of inputArray and add in front of outputArray
for (int i =a.length-1; i>0;i-- ){
b[j]= a[i];
System.out.println(a[i]+"**"+b[j]);
j++; // Increase a counter
}
}
*********************************************************************************
// Java program to reverse integer array with binary
*********************************************************************************
//// Java program to reverse words of String
// input =>"This is my country And I love my Country";
//output => "sihT si ym yrtnuoc dnA I evol ym yrtnuoC"
public static void main(String[] args) {
String inputString = "This is my country And I love my Country"; //input string
String[] words = inputString.split(" "); //split words, now -->This , is , my , country,..etc
String reverseString = ""; // get output in this.
// first loop to get get each words one by one from string
for (int i = 0; i < words.length; i++) {
String word = words[i]; //initi, word contain -"This" in next iteration it's "is " & so on
String reverseWord = ""; // get a output (reverse word)
//second loop is for to reverse a word
for (int j = word.length() - 1; j >= 0; j--) {
reverseWord = reverseWord + word.charAt(j); //append chars in reverse manner
}
reverseString = reverseString + reverseWord + " "; //appends words as process from inner loop
}
System.out.println(inputString);
System.out.println(reverseString);
}
*********************************************************************************
// Prefix and post fix
public static void main(String[] args){
int x;
int a = 100;
System.out.println(a-- +"==="+--a);
//100 99 98
// values postfix ,prefix 99 is became 98
x=a-- - --a;
System.out.println(x); //output is -->2
}
}
*********************************************************************************
//To Find Duplicate Elements In An Array
public
static
void
main(String[] args)
{
String[] strArray = {
"abc"
,
"def"
,
"mno"
,
"xyz"
,
"pqr"
,
"xyz"
,
"def"
};
for
(
int
i =
0
; i < strArray.length-
1
; i++) //to take 1 element from array
{
for
(
int
j = i+
1
; j < strArray.length; j++)//compare all the element in array one by one
{
if
( (strArray[i].equals(strArray[j])) && (i != j) )
//to avoid outOfIndex Error
{
System.out.println(
"Duplicate Element is : "
+strArray[j])
;
}
}
}
}
// Find Second Largest Number In An Integer Array :
int firstLargest, secondLargest;
if(input[0] > input[1]) //Checking first two elements of input array
{
//If first element is greater than second element
firstLargest = input[0];
secondLargest = input[1];
}
else
{
//If second element is greater than first element
firstLargest = input[1];
secondLargest = input[0];
}
//Checking remaining elements of input array
for (int i = 2; i < input.length; i++)
{
if(input[i] > firstLargest)
{
//If element at 'i' is greater than 'firstLargest'
secondLargest = firstLargest;
firstLargest = input[i];
}
else if (input[i] < firstLargest && input[i] > secondLargest)
{
//If element at 'i' is smaller than 'firstLargest' and greater than 'secondLargest'
secondLargest = input[i];
}
}
System.out.println(secondLargest );
}
*********************************************************************************
// Sorting the integer array
public static void main(String[] args) {
int[] inputArray = { 1, 2, 4, 6, 3, 8, 9, 44, 3, 22 };
int temp, pos;
for (int i = 0; i < inputArray.length - 1; i++) //loop for taking first element
{
pos = i; // initializing pos variable within for loop so every-time it is incremented
//finding the position of smallest element between (i+1)th element and last element
for (int j = i+1; j < inputArray.length; j++)
{
if(inputArray[j] < inputArray[pos] ) // condition to find smallest element
{
pos = j;
}
}
//Swapping inputArray[i] and inputArray[pos]
temp = inputArray[i];
inputArray[i] = inputArray[pos];
inputArray[pos] = temp;
}
for (int i = 0; i < inputArray.length; i++)
{
System.out.print(inputArray[i] + " ");
}
System.out.println();
}
*********************************************************************************
// Find a continues sub array whose sum is equal to inputNumber
public static void main(String[] args) {
int[] inputArray = { 1, 2, 4, 6, 3, 8, 9, 44, 3, 22 };
int inputNumber = 15;
int sum = 0; // Initializing 'sum' to 0
// Iterating through 'inputArray'
for (int i = 0; i < inputArray.length; i++) {
sum = inputArray[i]; // Assigning inputArray[i] to 'sum'
for (int j = i + 1; j < inputArray.length; j++) {
// Adding inputArray[j] to 'sum'
sum = sum + inputArray[j];
// If 'sum' is equal to 'inputNumber' then printing the sub array
if (sum == inputNumber) {
System.out.println("Continuous sub array of "
+ Arrays.toString(inputArray) + " whose sum is "
+ inputNumber + " is ");
//print sub array from i element to j element which satisfy a condition
for (int k = i; k <= j; k++) {
System.out.print(inputArray[k] + " ");
}
System.out.println();
}
// if 'sum' is smaller than 'inputNumber', continue the loop
else if (sum < inputNumber) {
continue;
}
// if 'sum' is greater than 'inputNumber', then break the loop
else if (sum > inputNumber) {
break;
}
}
}
*********************************************************************************
// Reverse The String With Preserving The Position Of Spaces :public static void main(String[] args) {
String inputString = "Hi this is good tested Nagard";
char
[] inputStringArray = inputString.toCharArray();
//Converting inputString to char array 'inputStringArray'
//Defining a new char array 'resultArray' with same size as inputStringArray
char
[] resultArray =
new
char
[inputStringArray.length];
//First for loop :
//For every space in the 'inputStringArray',
//we insert spaces in the 'resultArray' at the corresponding positions
for
(
int
i =
0
; i < inputStringArray.length; i++)
{
if
(inputStringArray[i] ==
' '
)
{
resultArray[i] =
' '
;
}
}
//Initializing 'j' with length of resultArray
int
j = 0
;
//Second for loop :
//we copy every non-space character of inputStringArray
//from first to last at 'j' position of resultArray
for
(
int
i =
inputStringArray.length-1; i >0; i--)
{
if
(inputStringArray[i] !=
' '
)
{
//If resultArray already has space at index j then decrementing 'j'
if
(resultArray[j] !=
' '
)
{
resultArray[j] = inputStringArray[i];
}
j++;
}
}
//Find The Percentage Of Uppercase Letters, Lowercase Letters, Digits And Other Special Characters In A String
public static void main(String[] args) {
String inputString = "Hi My email id is Test@gmail.com 112 time appear";
//Getting total no of characters in the given string
int
totalChars = inputString.length();
//Initializing upperCaseLetters, lowerCaseLetters, digits and others with 0
int
upperCaseLetters =
0
;
int
lowerCaseLetters =
0
;
int
digits =
0
;
int
others =
0
;
//Iterating through each character of inputString
for
(
int
i =
0
; i < inputString.length(); i++)
{
char
ch = inputString.charAt(i);
//If ch is in uppercase, then incrementing upperCaseLetters
if
(Character.isUpperCase(ch))
{
upperCaseLetters++;
}
//If ch is in lowercase, then incrementing lowerCaseLetters
else
if
(Character.isLowerCase(ch))
{
lowerCaseLetters++;
}
//If ch is a digit, then incrementing digits
else
if
(Character.isDigit(ch))
{
digits++;
}
//If ch is a special character then incrementing others
else
{
others++;
}
}
//Calculating percentage of uppercase letters, lowercase letters, digits and other characters
double
upperCaseLetterPercentage = (upperCaseLetters *
100.0
) / totalChars ;
double
lowerCaseLetterPercentage = (lowerCaseLetters *
100.0
) / totalChars;
double
digitsPercentage = (digits *
100.0
) / totalChars;
double
otherCharPercentage = (others *
100.0
) / totalChars;
DecimalFormat formatter =
new
DecimalFormat(
"##.##"
);
//Printing percentage of uppercase letters, lowercase letters, digits and other characters
System.out.println(
"In '"
+inputString+
"' : "
);
System.out.println(
"Uppercase letters are "
+formatter.format(upperCaseLetterPercentage)+
"% "
);
System.out.println(
"Lowercase letters are "
+formatter.format(lowerCaseLetterPercentage)+
"%"
);
System.out.println(
"Digits Are "
+formatter.format(digitsPercentage)+
"%"
);
System.out.println(
"Other Characters Are "
+formatter.format(otherCharPercentage)+
"%"
);
}
}
***********************************************************************************
// Creation Of Matrix In Java
Scanner sc =
new
Scanner(System.in);
System.out.println(
"Enter The Number Of Rows"
);
int
row = sc.nextInt();
System.out.println(
"Enter The Number Of Columns"
);
int
cols = sc.nextInt();
//defining 2D array to hold matrix data
int
[][] matrix =
new
int
[row][cols];
System.out.println(
"Enter Matrix Data"
);
for
(
int
i =
0
; i < row; i++)
{
for
(
int
j =
0
; j < cols; j++)
{
matrix[i][j] = sc.nextInt();
}
}
System.out.println(
"Your Matrix is : "
);
for
(
int
i =
0
; i < row; i++)
{
for
(
int
j =
0
; j < cols; j++)
{
System.out.print(matrix[i][j]+
"\t"
);
}
System.out.println();
}
*********************************************************************************
//Matrix Addition In Java
Scanner sc = new Scanner(System.in); // Make ready compiler to ready from data
System.out.println("Enter The Number Of Rows");
int row = sc.nextInt();
System.out.println("Enter The Number Of Columns");
int cols = sc.nextInt();
int[][] matrix1 = new int[row][cols]; // Define matrix
int[][] matrix2 = new int[row][cols];
int[][] sum = new int[row][cols];
System.out.println("Enter The Data For First Matrix :");
for (int i = 0; i < row; i++)
{
for (int j = 0; j < cols; j++)
{
matrix1[i][j] = sc.nextInt();
}
}
System.out.println("Enter The Data For Second Matrix :");
for (int i = 0; i < row; i++)
{
for (int j = 0; j < cols; j++)
{
matrix2[i][j] = sc.nextInt();
}
}
System.out.println("Sum = ");
for (int i = 0; i < row; i++)
{
for (int j = 0; j < cols; j++)
{
sum[i][j] = matrix1[i][j] + matrix2[i][j];
System.out.print(sum[i][j]+"\t");
}
System.out.println();
}
*********************************************************************************
// Multiplication of Matrix
public
static
void
main(String[] args)
{
Scanner sc =
new
Scanner(System.in);
System.out.println(
"Enter The Number Of Rows In First Matrix"
);
int
rowsInFirst = sc.nextInt();
System.out.println(
"Enter The Number Of Columns In First Matrix / Rows In Second Matrix"
);
int
colsInFirstRowsInSecond = sc.nextInt();
System.out.println(
"Enter The Number Of Columns In Second Matrix"
);
int
colsInSecond = sc.nextInt();
int
[][] matrix1 =
new
int
[rowsInFirst][colsInFirstRowsInSecond];
int
[][] matrix2 =
new
int
[colsInFirstRowsInSecond][colsInSecond];
int
[][] product =
new
int
[rowsInFirst][colsInSecond];
System.out.println(
"Enter The Data For First Matrix :"
);
for
(
int
i =
0
; i < rowsInFirst; i++)
{
for
(
int
j =
0
; j < colsInFirstRowsInSecond; j++)
{
matrix1[i][j] = sc.nextInt();
}
}
System.out.println(
"Enter The Data For Second Matrix :"
);
for
(
int
i =
0
; i < colsInFirstRowsInSecond; i++)
{
for
(
int
j =
0
; j < colsInSecond; j++)
{
matrix2[i][j] = sc.nextInt();
}
}
System.out.println(
"Product = "
);
for
(
int
i =
0
; i < rowsInFirst; i++)
{
for
(
int
j =
0
; j < colsInSecond; j++)
{
for
(
int
k =
0
; k < colsInFirstRowsInSecond; k++)
{
product[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
for
(
int
i =
0
; i < rowsInFirst; i++)
{
for
(
int
j =
0
; j < colsInSecond; j++)
{
System.out.print(product[i][j]+
"\t"
);
}
System.out.println();
}
}
// Transpose of Matrix (Rows into col and col into rows)
public
static
void
main(String[] args)
{
Scanner sc =
new
Scanner(System.in);
System.out.println(
"Enter The Number Of Rows"
);
int
rows = sc.nextInt();
System.out.println(
"Enter The Number Of Columns"
);
int
cols = sc.nextInt();
int
[][] matrix =
new
int
[rows][cols];
int
[][] transpose =
new
int
[cols][rows];
System.out.println(
"Enter The Data For Matrix :"
);
for
(
int
i =
0
; i < rows; i++)
{
for
(
int
j =
0
; j < cols; j++)
{
matrix[i][j] = sc.nextInt();
}
}
System.out.println(
"Transpose of Matrix is :"
);
for
(
int
i =
0
; i < rows; i++)
{
for
(
int
j =
0
; j < cols; j++)
{
transpose[j][i] = matrix[i][j]; // Just need to change position of i and j
}
}
for
(
int
i =
0
; i < cols; i++)
{
for
(
int
j =
0
; j < rows; j++)
{
System.out.print(transpose[i][j]+
"\t"
);
}
System.out.println();
}
********************************************************************************
//To Reverse And Add A Number Until You Get A Palindrome (number whose reverse == original number . ie 123321 ):