Merge remote-tracking branch 'upstream/master'

This commit is contained in:
sahilb2 2018-02-06 01:03:22 -06:00
commit bdc59aa4ea
5 changed files with 94 additions and 4 deletions

View File

@ -221,4 +221,20 @@ public class Matrix {
return str; 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);
}
} }

View 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
}
}
};

View File

@ -14,7 +14,7 @@ public class CountChar {
Scanner input = new Scanner(System.in); Scanner input = new Scanner(System.in);
System.out.print("Enter your text: "); System.out.print("Enter your text: ");
String str = input.nextLine(); String str = input.nextLine();
input.close();
System.out.println("There are " + CountCharacters(str) + " characters."); System.out.println("There are " + CountCharacters(str) + " characters.");
} }
@ -24,7 +24,7 @@ public class CountChar {
* @return int: Number of characters in the passed string * @return int: Number of characters in the passed string
* */ * */
public static int CountCharacters(String str) { private static int CountCharacters(String str) {
int count = 0; int count = 0;

View 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);
}
}

View File

@ -18,8 +18,8 @@ import java.util.Scanner;
input.close(); input.close();
} }
public static int wordCount(String s){ private static int wordCount(String s){
if(s.isEmpty() || s == null) return -1; if(s.isEmpty() || s == null) return 0;
return s.trim().split("[\\s]+").length; return s.trim().split("[\\s]+").length;
} }