Skip to content

Commit 555b715

Browse files
authored
Merge pull request TheAlgorithms#366 from Rachana040/master
Updated Matrix.java, added Transpose of a Matrix
2 parents dcfde17 + b2fc598 commit 555b715

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
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
}

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+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy