diff --git a/Data Structures/Matrix/Matrix.java b/Data Structures/Matrix/Matrix.java index edbdcdbf0..a26dfe976 100644 --- a/Data Structures/Matrix/Matrix.java +++ b/Data Structures/Matrix/Matrix.java @@ -221,4 +221,20 @@ public class Matrix { return str; } + + /** + * Returns transposed matrix of this matrix. + * + * @return transposed Matrix. + */ + public Matrix transpose() { + + int[][] newData = new int[this.data[0].length][this.data.length]; + + for (int i = 0; i < this.getColumns(); ++i) + for(int j = 0; j < this.getRows(); ++j) + newData[i][j] = this.data[j][i]; + + return new Matrix(newData); + } } diff --git a/Misc/PalindromicPrime.java b/Misc/PalindromicPrime.java new file mode 100644 index 000000000..866467206 --- /dev/null +++ b/Misc/PalindromicPrime.java @@ -0,0 +1,41 @@ +import java.util.Scanner; +public class PalindromePrime { + + public static void main(String[] args) { // Main funtion + Scanner in = new Scanner(System.in); + System.out.println("Enter the quantity of First Palindromic Primes you want"); + int n = in.nextInt(); // Input of how mant first pallindromic prime we want + funtioning(n); // calling funtion - functioning + } + + public static boolean prime(int num) { // checking if number is prime or not + for (int divisor = 2; divisor <= num / 2; divisor++) { + if (num % divisor == 0) { + return false; // false if not prime + } + } + return true; // True if prime + } + + public static int reverse(int n){ // Returns the reverse of the number + int reverse = 0; + while(n!=0){ + reverse = reverse * 10; + reverse = reverse + n%10; + n = n/10; + } + return reverse; + } + + public static void funtioning(int y){ + int count =0; + int num = 2; + while(count < y){ + if(prime(num) && num == reverse(num)){ // number is prime and it's reverse is same + count++; // counts check when to terminate while loop + System.out.print(num + "\n"); // Print the Palindromic Prime + } + num++; // inrease iterator value by one + } + } +}; diff --git a/Others/CountChar.java b/Others/CountChar.java index 1c8dcfea9..6504aabb4 100644 --- a/Others/CountChar.java +++ b/Others/CountChar.java @@ -14,7 +14,7 @@ public class CountChar { Scanner input = new Scanner(System.in); System.out.print("Enter your text: "); String str = input.nextLine(); - + input.close(); System.out.println("There are " + CountCharacters(str) + " characters."); } @@ -24,7 +24,7 @@ public class CountChar { * @return int: Number of characters in the passed string * */ - public static int CountCharacters(String str) { + private static int CountCharacters(String str) { int count = 0; diff --git a/Others/PowerOfTwoOrNot.java b/Others/PowerOfTwoOrNot.java new file mode 100644 index 000000000..36facfedd --- /dev/null +++ b/Others/PowerOfTwoOrNot.java @@ -0,0 +1,33 @@ +import java.util.Scanner; + +/** +*A utility to check if a given number is power of two or not. +*For example 8,16 etc. +*/ +public class PowerOfTwoOrNot { + + public static void main (String[] args) { + + Scanner sc = new Scanner(System.in); + System.out.println("Enter the number"); + int num = sc.nextInt(); + boolean isPowerOfTwo = checkIfPowerOfTwoOrNot(num); + if (isPowerOfTwo) { + System.out.println("Number is a power of two"); + } else { + System.out.println("Number is not a power of two"); + } + } + + +/** +* Checks whether given number is power of two or not. +* +* @param number +* @return boolean +*/ +public static boolean checkIfPowerOfTwoOrNot(int number) { + return number != 0 && ((number & (number-1)) == 0); + } + +} diff --git a/Others/countwords.java b/Others/countwords.java index 30806e13f..a93aa1a33 100644 --- a/Others/countwords.java +++ b/Others/countwords.java @@ -18,8 +18,8 @@ import java.util.Scanner; input.close(); } - public static int wordCount(String s){ - if(s.isEmpty() || s == null) return -1; + private static int wordCount(String s){ + if(s.isEmpty() || s == null) return 0; return s.trim().split("[\\s]+").length; }