You are on page 1of 4

NAME – SACHIN ROLL NUMBER – BTECH/60055/19 BRANCH - CSE

1. WAP where given number of lines (n<10) to be read from keyboard. For example, for n=5, the
pyramid of 5 lines should be printed as: using link list::

       1

           2 3 2

    34543

     4 5 6 7 6 5 4

 5 6 7 8 9 8 7 6 5

Code:

1. import java.util.*;
2. public class DS_Assignment3
3. {
4. public static void main(String[] args)
5. {
6. Scanner in=new Scanner(System.in);
7. System.out.println("SACHIN \t BTECH/60055/19 ");
8. System.out.println("Enter the dimention");
9. LinkedList<String> ll = new LinkedList<String>();
10. int n = in.nextInt();
11. int rows = n, k = 0, counter = 0, counter1 = 0;
12. for (int i = 1; i <= rows; ++i)
13. {
14. for (int blank = 1; blank <= rows - i; ++blank)
15. {
16. ll.add(" ");
17. ++counter;
18. }
19. while (k != 2 * i - 1)
20. {
21. if (counter <= rows - 1)
22. {
23. ll.add((i + k) + " ");
24. ++counter;
25. } else
26. {
27. ++counter1;
28. ll.add((i + k - 2 * counter1) + " ");
29. }
30. ++k;
31. }
32. counter1 = counter = k = 0;
33. for (String str : ll)
34. System.out.print(str + " ");
35. System.out.println();
36. ll.clear();
37. }
38. }
39. }

 
NAME – SACHIN ROLL NUMBER – BTECH/60055/19 BRANCH - CSE

  Input/Output:

2. WAP that Compute sum of left diagonal elements (a “left diagonal” of a square matrix A[n][n] is
the set of elements lying on the diagonal line connecting the elements A[0][0] and A[n−1][n−1]).

Compute sum of right diagonal elements (a “right diagonal” of a square matrix A[n][n] is the set of
elements lying on the diagonal line connecting the elements A[0][n − 1] and A[n − 1][0]).

Print the lower diagonal matrix (the lower diagonal matrix of a square matrix A[n][n] is the set of
elements lying strictly ‘below’ its left diagonal).

 Print the upper diagonal matrix (the upper diagonal matrix of a square matrix A[n][n] is the set of
elements lying strictly ‘above’ its left diagonal)One blank can have AT MOST ONE statement.

Code:

1. import java.util.*;
2. public class DS_Assignment4
3. {
4. static int n = 10;
5. static int[][] arr = new int[n][n];
6. static int left()
7. {
8. int sum =0;
9. for(int i=0; i<n; i++)
10. {
11. for(int j=0; j<n; j++)
12. {
13. if(i==j)
14. {
15. sum = sum + arr[i][j];
16. }
17. }
18. }
19. return sum;
20. }
21. static int right()
22. {
23. int sum =0;
24. for(int i=0; i<n; i++)
NAME – SACHIN ROLL NUMBER – BTECH/60055/19 BRANCH - CSE

25. {
26. for(int j=0; j<n; j++)
27. {
28. if(i+j==n-1)
29. {
30. sum = sum + arr[i][j];
31. }
32. }
33. }
34. return sum;
35. }
36. static void lower()
37. {
38. System.out.println("Lower diagonal matrix\n");
39. for(int i=0; i<n; i++)
40. {
41. for(int j=0; j<n; j++)
42. {
43. if(j<i)
44. {
45. System.out.print(arr[i][j]+"\t");
46. }
47. else
48. {
49. System.out.print("\t");
50. }
51. }
52. System.out.println();
53. }
54. }
55. static void upper()
56. {
57. System.out.println("Upper diagonal matrix\n");
58. for(int i=0; i<n; i++)
59. {
60. for(int j=0; j<n; j++)
61. {
62. if(i<j)
63. {
64. System.out.print(arr[i][j]+"\t");
65. }
66. else
67. {
68. System.out.print(" \t");
69. }
70. }
71. System.out.println();
72. }
73. }
74. public static void main (String []Args)
75. {
76. Scanner in=new Scanner(System.in);
77. System.out.println("SACHIN \t BTECH/60055/19 ");
78. System.out.println("Enter the dimention");
79. n = in.nextInt();
80. System.out.println("Enter elements in the Square Matrix");
81. for(int i=0; i<n; i++)
82. {
83. for(int j=0; j<n; j++)
84. {
85. arr[i][j]=in.nextInt();
86. }
87. }
88. System.out.println("Left Diagonal sum = "+left());
89. System.out.println("Right Diagonal sum = "+right());
90. lower();
91. upper();
92. }
93. }
NAME – SACHIN ROLL NUMBER – BTECH/60055/19 BRANCH - CSE

   Input/Output:

You might also like