Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Write a program to enter a size of an array, fill the

array and the rotate the array left column-wise.


import java.util.*;

class rotation

static void main()

Scanner sc=new Scanner(System.in);

System.out.print("Enter row: ");

int r=sc.nextInt();int x,j;

System.out.print("Enter column: ");

int c=sc.nextInt();

int ar[][]=new int[r][c];

System.out.println("Fill the array");

for(int i=0;i<r;i++)

for(j=0;j<c;j++)

ar[i][j]=sc.nextInt();

for(int i=0;i<r;i++)
{

for(j=0;j<c;j++)

System.out.print(ar[i][j]+" ");

System.out.println();

for(int i=0;i<r;i++)

x=ar[i][0];

for(j=0;j<c-1;j++)

ar[i][j]=ar[i][j+1];

ar[i][j]=x;

System.out.println("new array:");

for(int i=0;i<r;i++)

for(j=0;j<c;j++)

{
System.out.print(ar[i][j]+" ");

System.out.println();

}
Sample Input:
Enter row: 5
Enter column: 5
Fill the array
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Sample Output:
12345
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
new array:
23451
7 8 9 10 6
12 13 14 15 11
17 18 19 20 16
22 23 24 25 21
Sample Input:
Enter row: 3
Enter column: 3
Fill the array
1
2
3
4
5
6
7
8
9
Sample Output:
123

456

789

new array:

231

564

897
Write a program to enter an odd number and print the
magic square of that size.
import java.util.*;

class magic_s

void main()

Scanner sc=new Scanner(System.in);

System.out.println("Enter an odd number:");

int n=sc.nextInt();

int c=n/2;int r=n-1;

int ar[][]=new int[n][n];

ar[r][c]=1;

for(int i=2;i<=n*n;i++)

if(ar[(r+1)%n][(c+1)%n]==0)

r=(r+1)%n;

c=(c+1)%n;

else

{
r=(r-1+n)%n;

ar[r][c]=i;

for(int i=0;i<n;i++)

for(int j=0;j<n;j++)

System.out.print(ar[i][j]+" ");

System.out.println();

}
Sample Input:
Enter an odd number:
5
Sample Output:
11 18 25 2 9
10 12 19 21 3
4 6 13 20 22
23 5 7 14 16
17 24 1 8 15
Sample Input:
Enter an odd number:
11
Sample Output:
56 69 82 95 108 121 2 15 28 41 54

55 57 70 83 96 109 111 3 16 29 42

43 45 58 71 84 97 110 112 4 17 30

31 44 46 59 72 85 98 100 113 5 18

19 32 34 47 60 73 86 99 101 114 6

7 20 33 35 48 61 74 87 89 102 115

116 8 21 23 36 49 62 75 88 90 103

104 117 9 22 24 37 50 63 76 78 91

92 105 118 10 12 25 38 51 64 77 79
80 93 106 119 11 13 26 39 52 65 67

68 81 94 107 120 1 14 27 40 53 66

You might also like