Write a Java Program to Display Transpose Matrix


Introduction

In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. import java.util.Scanner;
  2. public class Javaexcercise
  3. {
  4. public static void main(String args[])
  5. {
  6. int i, j;
  7. System.out.println("Enter total rows and columns: ");
  8. Scanner s = new Scanner(System.in);
  9. int row = s.nextInt();
  10. int column = s.nextInt();
  11. int array[][] = new int[row][column];
  12. System.out.println("Enter matrix:");
  13. for(i = 0; i < row; i++)
  14. {
  15. for(j = 0; j < column; j++)
  16. {
  17. array[i][j] = s.nextInt();
  18. System.out.print(" ");
  19. }
  20. }
  21. System.out.println("The above matrix before Transpose is ");
  22. for(i = 0; i < row; i++)
  23. {
  24. for(j = 0; j < column; j++)
  25. {
  26. System.out.print(array[i][j]+" ");
  27. }
  28. System.out.println(" ");
  29. }
  30. System.out.println("The above matrix after Transpose is ");
  31. for(i = 0; i < column; i++)
  32. {
  33. for(j = 0; j < row; j++)
  34. {
  35. System.out.print(array[j][i]+" ");
  36. }
  37. System.out.println(" ");
  38. }
  39. }
  40. }

Result

java techstudy.org

     





Related Topics




Popular Posts