Skip to content

Commit bdc59aa

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents d6a5572 + 7320201 commit bdc59aa

File tree

5 files changed

+94
-4
lines changed

5 files changed

+94
-4
lines changed

Data Structures/Matrix/Matrix.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,20 @@ public String toString() {
221221

222222
return str;
223223
}
224+
225+
/**
226+
* Returns transposed matrix of this matrix.
227+
*
228+
* @return transposed Matrix.
229+
*/
230+
public Matrix transpose() {
231+
232+
int[][] newData = new int[this.data[0].length][this.data.length];
233+
234+
for (int i = 0; i < this.getColumns(); ++i)
235+
for(int j = 0; j < this.getRows(); ++j)
236+
newData[i][j] = this.data[j][i];
237+
238+
return new Matrix(newData);
239+
}
224240
}

Misc/PalindromicPrime.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import java.util.Scanner;
2+
public class PalindromePrime {
3+
4+
public static void main(String[] args) { // Main funtion
5+
Scanner in = new Scanner(System.in);
6+
System.out.println("Enter the quantity of First Palindromic Primes you want");
7+
int n = in.nextInt(); // Input of how mant first pallindromic prime we want
8+
funtioning(n); // calling funtion - functioning
9+
}
10+
11+
public static boolean prime(int num) { // checking if number is prime or not
12+
for (int divisor = 2; divisor <= num / 2; divisor++) {
13+
if (num % divisor == 0) {
14+
return false; // false if not prime
15+
}
16+
}
17+
return true; // True if prime
18+
}
19+
20+
public static int reverse(int n){ // Returns the reverse of the number
21+
int reverse = 0;
22+
while(n!=0){
23+
reverse = reverse * 10;
24+
reverse = reverse + n%10;
25+
n = n/10;
26+
}
27+
return reverse;
28+
}
29+
30+
public static void funtioning(int y){
31+
int count =0;
32+
int num = 2;
33+
while(count < y){
34+
if(prime(num) && num == reverse(num)){ // number is prime and it's reverse is same
35+
count++; // counts check when to terminate while loop
36+
System.out.print(num + "\n"); // Print the Palindromic Prime
37+
}
38+
num++; // inrease iterator value by one
39+
}
40+
}
41+
};

Others/CountChar.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public static void main(String[] args) {
1414
Scanner input = new Scanner(System.in);
1515
System.out.print("Enter your text: ");
1616
String str = input.nextLine();
17-
17+
input.close();
1818
System.out.println("There are " + CountCharacters(str) + " characters.");
1919
}
2020

@@ -24,7 +24,7 @@ public static void main(String[] args) {
2424
* @return int: Number of characters in the passed string
2525
* */
2626

27-
public static int CountCharacters(String str) {
27+
private static int CountCharacters(String str) {
2828

2929
int count = 0;
3030

Others/PowerOfTwoOrNot.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.Scanner;
2+
3+
/**
4+
*A utility to check if a given number is power of two or not.
5+
*For example 8,16 etc.
6+
*/
7+
public class PowerOfTwoOrNot {
8+
9+
public static void main (String[] args) {
10+
11+
Scanner sc = new Scanner(System.in);
12+
System.out.println("Enter the number");
13+
int num = sc.nextInt();
14+
boolean isPowerOfTwo = checkIfPowerOfTwoOrNot(num);
15+
if (isPowerOfTwo) {
16+
System.out.println("Number is a power of two");
17+
} else {
18+
System.out.println("Number is not a power of two");
19+
}
20+
}
21+
22+
23+
/**
24+
* Checks whether given number is power of two or not.
25+
*
26+
* @param number
27+
* @return boolean
28+
*/
29+
public static boolean checkIfPowerOfTwoOrNot(int number) {
30+
return number != 0 && ((number & (number-1)) == 0);
31+
}
32+
33+
}

Others/countwords.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public static void main(String[] args){
1818
input.close();
1919
}
2020

21-
public static int wordCount(String s){
22-
if(s.isEmpty() || s == null) return -1;
21+
private static int wordCount(String s){
22+
if(s.isEmpty() || s == null) return 0;
2323
return s.trim().split("[\\s]+").length;
2424
}
2525

0 commit comments

Comments
 (0)