Merge remote-tracking branch 'upstream/master'
This commit is contained in:
commit
bdc59aa4ea
@ -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);
|
||||
}
|
||||
}
|
||||
|
41
Misc/PalindromicPrime.java
Normal file
41
Misc/PalindromicPrime.java
Normal file
@ -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
|
||||
}
|
||||
}
|
||||
};
|
@ -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;
|
||||
|
||||
|
33
Others/PowerOfTwoOrNot.java
Normal file
33
Others/PowerOfTwoOrNot.java
Normal file
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user