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

Computer16

The document contains multiple Java programs that demonstrate various functionalities such as multiplying principal axis elements of 2D arrays, finding mystic numbers, displaying upper and lower parts of a 2D array, and manipulating strings. Each program includes user input, validation, and methods for specific tasks, such as checking for prime numbers or rearranging strings. The document serves as a collection of programming exercises focused on arrays and string manipulation.

Uploaded by

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

Computer16

The document contains multiple Java programs that demonstrate various functionalities such as multiplying principal axis elements of 2D arrays, finding mystic numbers, displaying upper and lower parts of a 2D array, and manipulating strings. Each program includes user input, validation, and methods for specific tasks, such as checking for prime numbers or rearranging strings. The document serves as a collection of programming exercises focused on arrays and string manipulation.

Uploaded by

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

PROGRAM - 16

Write a program in java to Declare two 2D arrays and find the multiplication of the principal Axis
elements of both the arrays and store them in a 1D array. Print the two arrays and also the 1D
array.

import java.util.Scanner;

public class PrincipalAxisMultiplication {

public static void main(String[] args) {

// Declare two 2D arrays

int[][] array1 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

int[][] array2 = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};

// Print both arrays

System.out.println("Array 1:");

printArray(array1);

System.out.println("Array 2:");

printArray(array2);

// Calculate the multiplication of principal axis elements

int[] result = multiplyPrincipalAxis(array1, array2);

// Print the result

System.out.println("Multiplication of Principal Axis Elements:");

printArray(result);

}
// Method to print 2D array

public static void printArray(int[][] array) {

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

for (int j = 0; j < array[i].length; j++) {

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

System.out.println();

// Method to print 1D array

public static void printArray(int[] array) {

for (int i : array) {

System.out.print(i + " ");

System.out.println();

// Method to multiply principal axis elements and return as 1D array

public static int[] multiplyPrincipalAxis(int[][] array1, int[][] array2) {

int length = array1.length;

int[] result = new int[length];

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

result[i] = array1[i][i] * array2[i][i];


}

return result;

____________________________________________________________________________________________

OUTPUT
PROGRAM 17
Write a program in Java to accept 2 ranges from the user the ranges should be between 11 to
1000 find out all the mystic numbers between the ranges accepted from the user and store
them in a 2D array column major wise and find the number of even and Odd mystic numbers
separately and print their sum.

import java.util.Scanner;

public class MysticNumbers {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

// Accept ranges from the user

System.out.print("Enter the lower range (between 11 and 1000): ");

int lower = scanner.nextInt();

System.out.print("Enter the upper range (between 11 and 1000): ");

int upper = scanner.nextInt();

// Validate ranges

if (lower < 11 || upper > 1000 || lower > upper) {

System.out.println("Invalid range!");

return;

// Find mystic numbers

int[][] mysticNumbers = findMysticNumbers(lower, upper);

// Display mystic numbers in column-major order


System.out.println("Mystic Numbers:");

printMysticNumbers(mysticNumbers);

// Count even and odd mystic numbers

int evenCount = 0, oddCount = 0;

for (int[] row : mysticNumbers) {

for (int num : row) {

if (num % 2 == 0) {

evenCount++;

} else {

oddCount++;

// Display counts

System.out.println("Even Mystic Numbers Count: " + evenCount);

System.out.println("Odd Mystic Numbers Count: " + oddCount);

// Method to find mystic numbers

public static int[][] findMysticNumbers(int lower, int upper) {

int count = 0;

for (int i = lower; i <= upper; i++) {

if (isMysticNumber(i)) {
count++;

// 2D array to store mystic numbers

int[][] mysticNumbers = new int[count][1];

int index = 0;

for (int i = lower; i <= upper; i++) {

if (isMysticNumber(i)) {

mysticNumbers[index][0] = i;

index++;

return mysticNumbers;

// Method to check if a number is a mystic number

public static boolean isMysticNumber(int num) {

int sum = 0;

int temp = num;

while (temp > 0) {

sum += temp % 10;

temp /= 10;

}
return (sum == 7); // Example mystic number condition

// Method to print mystic numbers in column-major order

public static void printMysticNumbers(int[][] mysticNumbers) {

for (int[] row : mysticNumbers) {

for (int num : row) {

System.out.print(num + " ");

System.out.println();

____________________________________________________________________________________________

OUTPUT
PROGRAM 18
Write a program in Java to declare a 2D array of size m×m enter values randomly into the array
display the upper part and the lower part from the intersecting point to the four corners of the
2D array find the sum of the displayed values and print the sum.

import java.util.Scanner;

public class ArrayUpperLowerSumUsingScanner {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

// Accept size of the 2D array from the user

System.out.print("Enter the number of rows (m): ");

int m = sc.nextInt();

System.out.print("Enter the number of columns (n): ");

int n = sc.nextInt();

// Create a 2D array of size m x n

int[][] array = new int[m][n];

// Populate the array with values entered by the user

System.out.println("Enter the values for the array:");

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

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

System.out.print("Enter value for position [" + i + "][" + j + "]: ");

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

}
// Print the original array

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

printArray(array);

// Display upper and lower parts and calculate sum

int sum = 0;

System.out.println("Upper Part:");

for (int i = 0; i < m / 2; i++) {

for (int j = i; j < n - i; j++) {

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

sum += array[i][j];

System.out.println();

System.out.println("Lower Part:");

for (int i = m / 2; i < m; i++) {

for (int j = i; j < n - i; j++) {

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

sum += array[i][j];

System.out.println();

}
// Print the sum of displayed values

System.out.println("Sum of displayed values: " + sum);

// Method to print the 2D array

static void printArray(int[][] array) {

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

for (int j = 0; j < array[i].length; j++) {

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

System.out.println();

____________________________________________________________________________________________

OUTPUT
PROGRAM 19
Write a program in Java to declare it to the array of size m*n take another two range from the
user and and find out all even numbers between that range include the even number into the 2D
array where size of m and n should be compatible with both the ranges.

import java.util.Scanner;

public class EvenNumbersIn2DArray {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

// Get the range from the user

System.out.print("Enter the first range (between 11 to 1000): ");

int range1 = sc.nextInt();

System.out.print("Enter the second range (between 11 to 1000): ");

int range2 = sc.nextInt();

// Calculate m and n based on the range

int m = (range2 - range1) / 5; // Number of rows

int n = 5; // Number of columns for simplicity

int[][] array = new int[m][n];

int count = 0;

// Insert even numbers into the array

for (int i = range1; i <= range2; i++) {

if (i % 2 == 0) {
array[count / n][count % n] = i;

count++;

// Print the 2D array with even numbers

System.out.println("Even Numbers in 2D Array:");

printArray(array);

// Method to print the 2D array

static void printArray(int[][] array) {

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

for (int j = 0; j < array[i].length; j++) {

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

System.out.println();

____________________________________________________________________________________________
OUTPUT
PROGRAM 20
Write a program in Java to declare a 2D array of size m into n accept the values from the user
and insert these values which are prime into the array display the array in the matrix form raw
major wise.

import java.util.Scanner;

public class PrimeNumbersIn2DArray {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

// Get the size of the 2D array

System.out.print("Enter number of rows: ");

int m = sc.nextInt();

System.out.print("Enter number of columns: ");

int n = sc.nextInt();

int[][] array = new int[m][n];

int count = 0;

// Insert prime numbers into the array

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

if (isPrime(i)) {

array[count / n][count % n] = i;

count++;

}
// Print the array

System.out.println("Prime Numbers in 2D Array:");

printArray(array);

// Method to check if a number is prime

static boolean isPrime(int num) {

if (num < 2) return false;

for (int i = 2; i <= Math.sqrt(num); i++) {

if (num % i == 0) return false;

return true;

// Method to print the 2D array

static void printArray(int[][] array) {

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

for (int j = 0; j < array[i].length; j++) {

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

System.out.println();

}
OUTPUT
PROGRAM 21

write a program to accept a string from the user and form a cross where the intersecting will be
accepted from the user (without using 2D array)

S S

C C

HH

OO

L L

import java.util.Scanner;

public class CrossPattern {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

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

String input = sc.nextLine();

int length = input.length();

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

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

if (i == j || i + j == length - 1) {

System.out.print(input.charAt(i) + " ");

} else {

System.out.print(" ");

}
System.out.println();

____________________________________________________________________________________________

OUTPUT
PROGRAM 22
Write a program in Java to accept a string from the user and check that the string is not more
than 100 characters including blank spaces extract each word of the string in a method void
extract() form a method arrange() with the return type and parameter in string type the original
sentence and the new sentence is displayed in the method extract() the method arrange()
rearrange each word of the sentence by placing all the vowels on the left hand side and the
consonants on the right hand side of the word if written the new word to the method extract()
where the new sentence is formed.

import java.util.Scanner;

public class RearrangeString {

public static void extract(String sentence) {

String newSentence = arrange(sentence);

System.out.println("Original Sentence: " + sentence);

System.out.println("New Sentence: " + newSentence);

public static String arrange(String sentence) {

String[] words = sentence.split(" ");

StringBuilder newSentence = new StringBuilder();

for (String word : words) {

StringBuilder vowels = new StringBuilder();

StringBuilder consonants = new StringBuilder();

for (char c : word.toCharArray()) {

if (isVowel(c)) {
vowels.append(c);

} else {

consonants.append(c);

newSentence.append(vowels).append(consonants).append(" ");

return newSentence.toString().trim();

public static boolean isVowel(char c) {

c = Character.toLowerCase(c);

return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a string (max 100 characters): ");

String input = sc.nextLine();

if (input.length() <= 100) {

extract(input);

} else {
System.out.println("String exceeds 100 characters.");

____________________________________________________________________________________________

OUTPUT
PROGRAM 23
A class called stringpop is defined to handle a string related operation the members of the class
are given below:-

->Txt - to store the given string of maximum length of 100.

->Stringpop()-constructor to initialize string to null.

->void readString() - accept String from the user.

->char CaseConvert() - to convert the letters to other case.

->void Circular() - to decode the string by replacing each Letters by converting it to opposite
case then by the next character in or circular way.

void main() should be there.

Hence, AbZ become bCa.

import java.util.Scanner;

public class Stringpop {

String Txt;

public Stringpop() {

Txt = null;

public void readString() {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a string: ");

Txt = sc.nextLine();

}
public char CaseConvert() {

if (Txt != null) {

StringBuilder newStr = new StringBuilder();

for (char c : Txt.toCharArray()) {

if (Character.isUpperCase(c)) {

newStr.append(Character.toLowerCase(c));

} else if (Character.isLowerCase(c)) {

newStr.append(Character.toUpperCase(c));

} else {

newStr.append(c);

Txt = newStr.toString();

return Txt.charAt(0); // returns the first character after case conversion

public void Circular() {

if (Txt != null) {

StringBuilder decodedStr = new StringBuilder();

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

char c = Txt.charAt(i);

if (Character.isLetter(c)) {

char newChar = (char) (c + 1);

if (Character.isUpperCase(c) && newChar > 'Z') {


newChar = 'A';

} else if (Character.isLowerCase(c) && newChar > 'z') {

newChar = 'a';

decodedStr.append(newChar);

} else {

decodedStr.append(c);

Txt = decodedStr.toString();

public static void main(String[] args) {

Stringpop sp = new Stringpop();

sp.readString();

System.out.println("Original String: " + sp.Txt);

sp.CaseConvert();

System.out.println("Case converted string: " + sp.Txt);

sp.Circular();

System.out.println("Circular decoded string: " + sp.Txt);

}
OUTPUT
PROGRAM 24
Write a program in Java to enter a string from the user and a number from the user the number
should be within range from 1 to 25 from the new string by chain in each characters at the nth
position in the alphabetical order display all new string.

import java.util.Scanner;

public class ShiftString {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a string: ");

String input = sc.nextLine();

System.out.print("Enter a number (1-25): ");

int shift = sc.nextInt();

if (shift < 1 || shift > 25) {

System.out.println("Invalid number. Please enter a number between 1 and 25.");

} else {

StringBuilder shiftedStr = new StringBuilder();

for (char c : input.toCharArray()) {

if (Character.isLetter(c)) {

char base = Character.isLowerCase(c) ? 'a' : 'A';

shiftedStr.append((char) ((c - base + shift) % 26 + base));

} else {

shiftedStr.append(c);
}

System.out.println("Shifted String: " + shiftedStr.toString());

____________________________________________________________________________________________

OUTPUT
PROGRAM 25
Write a method to declare a 2D array of integer type insert the values into the array within the
method void get() and initialise the values of m and n which stands for the rows and columns in
the constructor write another method int display() that returns an array that holds the transpose
value of the array that has been declared to the main method where both the arrays are
displayed.

import java.util.Scanner;

public class TransposeArray {

int[][] arr;

int m, n;

public TransposeArray(int m, int n) {

this.m = m;

this.n = n;

arr = new int[m][n];

public void get() {

Scanner sc = new Scanner(System.in);

System.out.println("Enter values into the array:");

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

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

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

}
public int[][] display() {

int[][] transpose = new int[n][m];

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

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

transpose[j][i] = arr[i][j];

return transpose;

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter number of rows (m): ");

int m = sc.nextInt();

System.out.print("Enter number of columns (n): ");

int n = sc.nextInt();

TransposeArray ta = new TransposeArray(m, n);

ta.get();

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

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

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

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


}

System.out.println();

int[][] transposed = ta.display();

System.out.println("Transposed Array:");

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

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

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

System.out.println();

____________________________________________________________________________________________

OUTPUT

You might also like