import [Link].
Scanner;
public class MirrorImageMatrix {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Input the order of the square matrix
[Link]("Enter the order of the square matrix (M x M): ");
int M = [Link]();
// Check for valid input range
if (M <= 2 || M >= 20) {
[Link]("Invalid input! Please enter a number between 3 and 19 for the order of
the matrix.");
return;
// Create the square matrix
int[][] A = new int[M][M];
// Input integers into the matrix
[Link]("Enter the elements of the matrix:");
for (int i = 0; i < M; i++) {
for (int j = 0; j < M; j++) {
A[i][j] = [Link]();
}
// Display the input matrix
[Link]("Input Matrix:");
for (int i = 0; i < M; i++) {
for (int j = 0; j < M; j++) {
[Link](A[i][j] + "\t");
[Link]();
// Create and display the mirror image matrix
[Link]("Mirror Image Matrix:");
for (int i = 0; i < M; i++) {
for (int j = M - 1; j >= 0; j--) {
[Link](A[i][j] + "\t");
[Link]();
[Link]();
OUTPUT:
m=
n=
100
No. are
11
13
31
Frequency =3
Question 2
import [Link].*;
public class question2 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
// Input size of array
[Link]("Enter the size of the array (N > 2 and N < 10): ");
int N = [Link]();
if (N <= 2 || N >= 10) {
[Link]("Invalid size! Size should be greater than 2 and less than 10.");
return;
int[] a = new int[N];
// Input elements for the array
[Link]("Enter " + N + " positive integers for the array:");
for (int i = 0; i < N; i++) {
a[i] = [Link]();
if (a[i] <= 0) {
[Link]("Invalid input! Please enter a positive integer.");
return;
// Sort the array using Bubble Sort
for (int i = 0; i < N - 1; i++) {
for (int j = 0; j < N - i - 1; j++) {
if (a[j] > a[j + 1]) {
// Swap a[j] and a[j + 1]
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
// Display sorted array
[Link]("Sorted array:");
for (int i = 0; i < N; i++) {
[Link](a[i] + " ");
[Link]();
// Fill the square matrix
int[][] b = new int[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
b[i][j] = a[(i + j) % N];
// Display the filled matrix
[Link]("Filled Matrix:");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
[Link](b[i][j] + " ");
[Link]();
[Link]();
OUTPUT:
Enter the size of the array (N > 2 and N < 10): 3
Enter 3 positive integers for the array:
Sorted array:
345
Filled Matrix:
345
453
534
Question 3:
import [Link];
public class ques3 {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Input the number of boxes to be packed
[Link]("Enter the number of boxes to be packed (up to 1000 boxes): ");
int N = [Link]();
if (N <= 0 || N > 1000) {
[Link]("Invalid input! Please enter a number between 1 and 1000.");
return;
int cartons48 = N / 48;
int remainingBoxes = N % 48;
int cartons24 = remainingBoxes / 24;
remainingBoxes %= 24;
int cartons12 = remainingBoxes / 12;
remainingBoxes %= 12;
int cartons6 = remainingBoxes / 6;
remainingBoxes %= 6;
// Display the breakup of cartons used in descending capacity
[Link]("Breakup of Cartons Used:");
[Link]("48 * " + cartons48 + " = " + (cartons48 * 48));
[Link]("24 * " + cartons24 + " = " + (cartons24 * 24));
[Link]("12 * " + cartons12 + " = " + (cartons12 * 12));
[Link]("6 * " + cartons6 + " = " + (cartons6 * 6));
// Display remaining boxes
[Link]("Remaining boxes: " + remainingBoxes);
// Calculate total number of boxes and cartons
int totalBoxes = N;
int totalCartons = cartons48 + cartons24 + cartons12 + cartons6;
[Link]("Total number of boxes: " + totalBoxes);
[Link]("Total number of cartons: " + totalCartons);
[Link]();
OUTPUT:
Enter the number of boxes to be packed (up to 1000 boxes): 140
Breakup of Cartons Used:
48 * 2 = 96
24 * 1 = 24
12 * 1 = 12
6*1=6
Remaining boxes: 2
Total number of boxes: 140
Total number of cartons: 5
Questions 4
import [Link];
public class ques4 {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number:");
int n = [Link](); // Entering a number
if (n < 4 || n % 2 != 0) {
[Link]("Not Goldbach"); // Printing invalid number
} else {
for (int i = 3; i < n / 2; i += 2) {
int b = n - i;
if (isPrime(i) == 2 && isPrime(b) == 2) {
[Link](i + "," + b);
static int isPrime(int k) {
int c = 0;
for (int i = 1; i <= k; i++) {
if (k % i == 0) {
c++;
return c;
OUTPUT:
Enter the number:
30
7,23
11,19
13,17
Questions 5
import [Link].*;
class ques5
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the value for m (between 100 and 1000): "); //entering the data of m
and n
int m = [Link]();
[Link]("Enter the value for n (less than 100): ");
int n = [Link]();
int i;
if (m > 100 || m < 1000 || n < 100) //
for ( i = m ; ; i++)
if (sumOfDigits(i) == n)
[Link]("The required number is = " + i);
break;
[Link]("Total number of digits = " + [Link](i).length());
else
[Link]("invalid");
static int sumOfDigits(int n) //to calculate sum of digits
int s = 0;
while (n > 0)
s = s+ n % 10;
n = n / 10;
return s;
}
OUTPUT:
Enter the value for m (between 100 and 1000):
100
Enter the value for n (less than 100):
11
The required number is = 119
Total number of digits = 3
Question 6
import [Link].*;
class Ques6
int isLeap(int y) //checking for leap year
if((y%400 == 0) || (y%100 != 0 && y%4 == 0))
return 366;
else
return 365;
String postfix(int n) //inputing postfix
int r = n%10;
if(r == 1 && n != 11)
return "ST";
else if(r == 2 && n != 12)
return "ND";
else if(r == 3 && n != 13)
return "RD";
else
return "TH";
void compute(int d, int y) //calculating date
int D[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
String MO[] = {"", "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY",
"AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"};
if(isLeap(y)==366)
D[2] = 29;
int m = 1;
while(d > D[m])
d = d - D[m];
m++;
[Link](d+postfix(d)+" "+MO[m]+", "+y);
void future(int d, int y, int n)
int max = isLeap(y);
d = d + n;
if(d>max)
d = d - max;
y++;
compute(d,y);
public static void main()
Ques6 ob = new Ques6();
Scanner sc = new Scanner([Link]);
[Link]("Enter the day number : ");
int day = [Link]();
[Link]("Enter the year : ");
int year = [Link]();
int max = [Link](year);
if(day > max)
[Link]("DAY NUMBER OUT OF RANGE");
else
[Link]("Enter the number of days after : ");
int n = [Link]();
if(n<1 || n>100)
{
[Link]("DATE AFTER (N DAYS) OUT OF RANGE");
else
[Link]("DATE :\t\t\t");
[Link](day,year);
[Link]("DATE AFTER "+n+" DAYS :\t");
[Link](day,year,n);
OUTPUT:
Enter the day number : 360
Enter the year : 2018
Enter the number of days after : 45
DATE : 26TH DECEMBER, 2018
DATE AFTER 45 DAYS : 9TH FEBRUARY, 2019
Question 7
import [Link];
public class question7 {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Input order of the square matrix
[Link]("Enter the order of the square matrix (M > 3 and M < 10): ");
int M = [Link]();
if (M <= 3 || M >= 10) {
[Link]("Invalid order! Order should be greater than 3 and less than 10.");
return;
int[][] A = new int[M][M];
// Input elements for the matrix
[Link]("Enter " + M * M + " positive integers for the matrix:");
for (int i = 0; i < M; i++) {
for (int j = 0; j < M; j++) {
A[i][j] = [Link]();
if (A[i][j] <= 0) {
[Link]("Invalid input! Please enter a positive integer.");
return;
// Sort non-boundary elements using Bubble Sort
for (int i = 1; i < M - 1; i++) {
for (int j = 1; j < M - 1; j++) {
for (int k = 1; k < M - 1 - j + 1; k++) {
if (A[i][k] > A[i][k + 1]) {
int temp = A[i][k];
A[i][k] = A[i][k + 1];
A[i][k + 1] = temp;
// Calculate sum of diagonals
int sumDiagonal1 = 0;
int sumDiagonal2 = 0;
for (int i = 0; i < M; i++) {
sumDiagonal1 += A[i][i];
sumDiagonal2 += A[i][M - 1 - i];
// Display original matrix
[Link]("Original Matrix:");
for (int i = 0; i < M; i++) {
for (int j = 0; j < M; j++) {
[Link](A[i][j] + " ");
[Link]();
// Display rearranged matrix
[Link]("\nRearranged Matrix:");
for (int i = 0; i < M; i++) {
for (int j = 0; j < M; j++) {
[Link](A[i][j] + " ");
[Link]();
// Display diagonal elements and their sum
[Link]("\nDiagonal elements of rearranged matrix:");
for (int i = 0; i < M; i++) {
[Link](A[i][i] + " ");
[Link]("\nSum of diagonal 1: " + sumDiagonal1);
[Link]("Sum of diagonal 2: " + sumDiagonal2);
[Link]();
OUTPUT:
Enter the order of the square matrix (M > 3 and M < 10): 4
Enter 16 positive integers for the matrix:
9
6
Original Matrix:
3465
7689
4566
7564
Rearranged Matrix:
3465
7689
4566
7564
Diagonal elements of rearranged matrix:
3664
Sum of diagonal 1: 19
Sum of diagonal 2: 25
Questions 9
import [Link];
import [Link];
import [Link];
public class ques9{
public static void main(String args[]) throws IOException {
InputStreamReader is = new InputStreamReader([Link]);
BufferedReader br = new BufferedReader(is);
[Link] scanner = new [Link]([Link]); // changed the scanner object
name
[Link]("Enter n where 2 < n < 9: "); // size of array
int n = [Link]();
if (n < 2 || n > 9) // checking validity
[Link]("Invalid");
else {
String a[] = new String[n];
[Link]("Enter names of teams:");
int i, l = 0;
for (i = 0; i < n; i++) { // input loop
a[i] = [Link]();
int l1 = a[i].length();
if (l < l1)
l = l1;
for (i = 0; i < n; i++) { // addition of spaces
int l1 = a[i].length();
for (int j = l1; j < l; j++)
a[i] = a[i] + " ";
}
for (int j = 0; j < l; j++) { // printing
for (i = 0; i < n; i++)
[Link](a[i].charAt(j) + "\t");
[Link]();
OUTPUT:
Enter n where 2 < n < 9:
Enter names of teams:
Emus
Royald Roys
Coyote
E R C
m o o
u y y
s a o
l t
d e
s
Question 8
import [Link];
class questi8 //class
public static void main(String args[]) //main method
Scanner sc = new Scanner([Link]);
[Link]("Enter a sentence"); //input
String a = [Link]();
char ch = [Link]([Link]()-1);
if(ch!='.'&&ch!='?'&&ch!='!') //checking validity
[Link]("Invalid");
else
a = [Link](0,[Link]()-1);
String s[] = [Link](" ");
for(int i = 0;i<[Link];i++){
if(palin(s[i]))
[Link](s[i]+" ");
else
[Link](convert (s[i])+" ");
[Link]();
static boolean palin(String a ) //palin method
{
String b = " ";
for(int i = 0;i<[Link]();i++)
b= [Link](i)+b;
return [Link](b);
static String convert (String a) //convert method
int i,l=[Link]();
String p = a;
for(i=l-1;i>=0;i--)
char ch = [Link](i);
char ch1 = [Link](i-1);
if(ch!=ch1)
break;
for(int j = i-1;j>=0;j--)
p= p+[Link](j);
return p;
OUTPUT:
Enter a sentence
help.
Helpleh
Question 10
import [Link];
public class question10 {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
// Input the order of the square matrix
[Link]("Enter the order of the square matrix (M x M): ");
int M = [Link]();
// Check for valid input range
if (M <= 2 || M >= 20) {
[Link]("Invalid input! Please enter a number between 3 and 19 for the order of
the matrix.");
return;
// Create the square matrix
int[][] A = new int[M][M];
// Input integers into the matrix
[Link]("Enter the elements of the matrix:");
for (int i = 0; i < M; i++) {
for (int j = 0; j < M; j++) {
A[i][j] = [Link]();
}
// Display the input matrix
[Link]("Input Matrix:");
for (int i = 0; i < M; i++) {
for (int j = 0; j < M; j++) {
[Link](A[i][j] + "\t");
[Link]();
// Create and display the mirror image matrix
[Link]("Mirror Image Matrix:");
for (int i = 0; i < M; i++) {
for (int j = M - 1; j >= 0; j--) {
[Link](A[i][j] + "\t");
[Link]();
[Link]();
OUTPUT:
Enter the order of the square matrix (M x M): 3
Enter the elements of the matrix:
5
6
Input Matrix:
4 3 5
6 3 7
7 8 9
Mirror Image Matrix:
5 3 4
7 3 6
9 8 7