Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

Import Java

The document contains code for several Java programs that take user input, perform calculations, and output results. The first program takes input for the order of a square matrix between 3-19, fills the matrix with user input integers, and outputs the original matrix and its mirror image. The other programs implement sorting arrays, filling matrices, calculating carton packing, checking for Goldbach partitions, finding a number with digit sum, computing dates, and other numerical/logical operations on user-provided values.

Uploaded by

NooB
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Import Java

The document contains code for several Java programs that take user input, perform calculations, and output results. The first program takes input for the order of a square matrix between 3-19, fills the matrix with user input integers, and outputs the original matrix and its mirror image. The other programs implement sorting arrays, filling matrices, calculating carton packing, checking for Goldbach partitions, finding a number with digit sum, computing dates, and other numerical/logical operations on user-provided values.

Uploaded by

NooB
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

import java.util.

Scanner;

public class MirrorImageMatrix {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Input the order of the square matrix

System.out.print("Enter the order of the square matrix (M x M): ");

int M = scanner.nextInt();

// Check for valid input range

if (M <= 2 || M >= 20) {

System.out.println("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

System.out.println("Enter the elements of the matrix:");

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

for (int j = 0; j < M; j++) {

A[i][j] = scanner.nextInt();

}
// Display the input matrix

System.out.println("Input Matrix:");

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

for (int j = 0; j < M; j++) {

System.out.print(A[i][j] + "\t");

System.out.println();

// Create and display the mirror image matrix

System.out.println("Mirror Image Matrix:");

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

for (int j = M - 1; j >= 0; j--) {

System.out.print(A[i][j] + "\t");

System.out.println();

scanner.close();

OUTPUT:
m=

n=
100

No. are

11

13

31

Frequency =3

Question 2
import java.util.*;

public class question2 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

// Input size of array

System.out.print("Enter the size of the array (N > 2 and N < 10): ");

int N = sc.nextInt();

if (N <= 2 || N >= 10) {

System.out.println("Invalid size! Size should be greater than 2 and less than 10.");

return;

int[] a = new int[N];

// Input elements for the array

System.out.println("Enter " + N + " positive integers for the array:");

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

a[i] = sc.nextInt();
if (a[i] <= 0) {

System.out.println("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

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

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

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

System.out.println();

// 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

System.out.println("Filled Matrix:");

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

for (int j = 0; j < N; j++) {

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

System.out.println();

sc.close();

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 java.util.Scanner;

public class ques3 {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Input the number of boxes to be packed

System.out.print("Enter the number of boxes to be packed (up to 1000 boxes): ");

int N = scanner.nextInt();

if (N <= 0 || N > 1000) {

System.out.println("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

System.out.println("Breakup of Cartons Used:");

System.out.println("48 * " + cartons48 + " = " + (cartons48 * 48));

System.out.println("24 * " + cartons24 + " = " + (cartons24 * 24));

System.out.println("12 * " + cartons12 + " = " + (cartons12 * 12));

System.out.println("6 * " + cartons6 + " = " + (cartons6 * 6));

// Display remaining boxes

System.out.println("Remaining boxes: " + remainingBoxes);

// Calculate total number of boxes and cartons

int totalBoxes = N;

int totalCartons = cartons48 + cartons24 + cartons12 + cartons6;

System.out.println("Total number of boxes: " + totalBoxes);

System.out.println("Total number of cartons: " + totalCartons);

scanner.close();

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 java.util.Scanner;

public class ques4 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number:");

int n = sc.nextInt(); // Entering a number

if (n < 4 || n % 2 != 0) {

System.out.println("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) {

System.out.println(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 java.util.*;

class ques5

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the value for m (between 100 and 1000): "); //entering the data of m
and n

int m = sc.nextInt();

System.out.println("Enter the value for n (less than 100): ");

int n = sc.nextInt();
int i;

if (m > 100 || m < 1000 || n < 100) //

for ( i = m ; ; i++)

if (sumOfDigits(i) == n)

System.out.println("The required number is = " + i);

break;

System.out.println("Total number of digits = " + String.valueOf(i).length());

else

System.out.println("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 java.util.*;

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

System.out.println(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(System.in);

System.out.print("Enter the day number : ");

int day = sc.nextInt();

System.out.print("Enter the year : ");

int year = sc.nextInt();

int max = ob.isLeap(year);

if(day > max)

System.out.println("DAY NUMBER OUT OF RANGE");

else

System.out.print("Enter the number of days after : ");

int n = sc.nextInt();

if(n<1 || n>100)

{
System.out.println("DATE AFTER (N DAYS) OUT OF RANGE");

else

System.out.print("DATE :\t\t\t");

ob.compute(day,year);

System.out.print("DATE AFTER "+n+" DAYS :\t");

ob.future(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 java.util.Scanner;

public class question7 {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Input order of the square matrix

System.out.print("Enter the order of the square matrix (M > 3 and M < 10): ");
int M = scanner.nextInt();

if (M <= 3 || M >= 10) {

System.out.println("Invalid order! Order should be greater than 3 and less than 10.");

return;

int[][] A = new int[M][M];

// Input elements for the matrix

System.out.println("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] = scanner.nextInt();

if (A[i][j] <= 0) {

System.out.println("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

System.out.println("Original Matrix:");

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

for (int j = 0; j < M; j++) {

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

System.out.println();

// Display rearranged matrix

System.out.println("\nRearranged Matrix:");

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

for (int j = 0; j < M; j++) {


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

System.out.println();

// Display diagonal elements and their sum

System.out.println("\nDiagonal elements of rearranged matrix:");

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

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

System.out.println("\nSum of diagonal 1: " + sumDiagonal1);

System.out.println("Sum of diagonal 2: " + sumDiagonal2);

scanner.close();

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 java.io.BufferedReader;

import java.io.InputStreamReader;

import java.io.IOException;

public class ques9{

public static void main(String args[]) throws IOException {

InputStreamReader is = new InputStreamReader(System.in);

BufferedReader br = new BufferedReader(is);

java.util.Scanner scanner = new java.util.Scanner(System.in); // changed the scanner object


name

System.out.println("Enter n where 2 < n < 9: "); // size of array

int n = scanner.nextInt();

if (n < 2 || n > 9) // checking validity

System.out.println("Invalid");

else {

String a[] = new String[n];

System.out.println("Enter names of teams:");

int i, l = 0;

for (i = 0; i < n; i++) { // input loop

a[i] = br.readLine();

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

System.out.print(a[i].charAt(j) + "\t");

System.out.println();

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 java.util.Scanner;

class questi8 //class

public static void main(String args[]) //main method

Scanner sc = new Scanner(System.in);

System.out.println("Enter a sentence"); //input

String a = sc.nextLine();

char ch = a.charAt(a.length()-1);

if(ch!='.'&&ch!='?'&&ch!='!') //checking validity

System.out.println("Invalid");

else

a = a.substring(0,a.length()-1);

String s[] = a.split(" ");

for(int i = 0;i<s.length;i++){

if(palin(s[i]))

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

else

System.out.print(convert (s[i])+" ");

System.out.println();

static boolean palin(String a ) //palin method


{

String b = " ";

for(int i = 0;i<a.length();i++)

b= a.charAt(i)+b;

return a.equals(b);

static String convert (String a) //convert method

int i,l=a.length();

String p = a;

for(i=l-1;i>=0;i--)

char ch = a.charAt(i);

char ch1 = a.charAt(i-1);

if(ch!=ch1)

break;

for(int j = i-1;j>=0;j--)

p= p+a.charAt(j);

return p;

OUTPUT:
Enter a sentence

help.

Helpleh

Question 10
import java.util.Scanner;

public class question10 {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Input the order of the square matrix

System.out.print("Enter the order of the square matrix (M x M): ");

int M = scanner.nextInt();

// Check for valid input range

if (M <= 2 || M >= 20) {

System.out.println("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

System.out.println("Enter the elements of the matrix:");

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

for (int j = 0; j < M; j++) {

A[i][j] = scanner.nextInt();

}
// Display the input matrix

System.out.println("Input Matrix:");

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

for (int j = 0; j < M; j++) {

System.out.print(A[i][j] + "\t");

System.out.println();

// Create and display the mirror image matrix

System.out.println("Mirror Image Matrix:");

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

for (int j = M - 1; j >= 0; j--) {

System.out.print(A[i][j] + "\t");

System.out.println();

scanner.close();

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

You might also like