KEMBAR78
Computer Project | PDF | Integer (Computer Science) | Matrix (Mathematics)
0% found this document useful (0 votes)
31 views28 pages

Computer Project

The document contains multiple Java programs for handling square matrices and strings. Each program includes user input validation, matrix operations such as displaying, mirroring, rotating, checking symmetry, and calculating diagonal sums, as well as string manipulation tasks like removing extra spaces and deleting words. The programs are structured with clear steps and comments for easy understanding.

Uploaded by

mandsaurwalam6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views28 pages

Computer Project

The document contains multiple Java programs for handling square matrices and strings. Each program includes user input validation, matrix operations such as displaying, mirroring, rotating, checking symmetry, and calculating diagonal sums, as well as string manipulation tasks like removing extra spaces and deleting words. The programs are structured with clear steps and comments for easy understanding.

Uploaded by

mandsaurwalam6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

matrix1

Write a program to declare a square matrix A[ ] [ ] of order (M x M) where ‘M’ is the number of rows
and the number of columns such that M must be greater than 2 and less than 20. Allow the user to
input integers into this matrix. Display appropriate error message for an invalid input. Perform the
following tasks:

(a) Display the input matrix.

(b) Create a mirror image matrix.

(c) Display the mirror image matrix.

/* Name: MOHAMMED MANDSAURWALA

* unique id: 8495841

*/

import java.util.*;

class Matrix1 {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

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

int m = sc.nextInt();

if (m < 2 || m > 20) {

System.out.println("SIZE OUT OF RANGE");

} else {

int b[][] = new int[m][m];

int a[][] = new int[m][m];

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

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

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

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

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

b[i][j] = a[i][k];

k--;

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();

System.out.println("MIRRIOR IMAGE");

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

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

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

System.out.println();

Step 1: Start

Step 2: Input the size m of the square matrix

Step 3:

→ If m < 2 or m > 20

→ Display: "SIZE OUT OF RANGE"

→ Stop

→ Else, continue
Step 4: Declare two matrices:

→ a[m][m] to store the original values

→ b[m][m] to store the mirror image

Step 5: Input elements into matrix a

→ Use nested loops

- Loop i from 0 to m - 1 (rows)

- Loop j from 0 to m - 1 (columns)

→ Input a[i][j]

Step 6: Create the mirror image matrix b

→ Loop i from 0 to m - 1

- Loop j from 0 to m - 1

→ Set b[i][j] = a[i][m - 1 - j]

// This mirrors each row

Step 7: Display the original matrix a

→ Loop through a[i][j] using nested loops

→ Print each row in a new line

Step 8: Display the mirror image matrix b

→ Loop through b[i][j] using nested loops

→ Print each row in a new line

Step 9: Stop

matrix2

Question 2:

Write a program to declare a square matrix A[ ] [ ] of order (M x M) where ‘M’ is the number of rows
and the number of columns such that M must be greater than 2 and less than 10. Accept the value of
M as user input. Display an appropriate message for an invalid input. Allow the user to input integers
into this matrix. Perform the following tasks:

(a) Display the original matrix.

(b) Check if the given matrix is Symmetric or not.


A square matrix is said to be Symmetric, if the element of the (i)th row and (j)th column is equal to
the element of the (i)th row and (j)th column.

(c) Find the sum of the elements of left diagonal and the sum of the elements of right diagonal of the
matrix and display them.

/* Name: MOHAMMED MANDSAURWALA

* unique id: 8495841

*/

import java.util.*;

public class Matrix2 {

public static void main() {

Scanner in = new Scanner(System.in);

int m, i, j, flag = 0, ld = 0, rd = 0;

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

m = in.nextInt();

if ((m > 2) && (m < 10)) {

int a[][] = new int[m][m];

System.out.println("Enter Elements in the matrix : ");

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

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

a[i][j] = in.nextInt();

System.out.println("ORIGINAL MATRIX");

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

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

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

System.out.println();
}

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

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

if (a[i][j] != a[j][i]) {

flag = 1;

break;

if (flag == 0)

System.out.println("THE GIVEN MATRIX IS SYMMETRIC");

else

System.out.println("THE GIVEN MATRIX IS NOT SYMMETRIC");

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

ld = ld + a[i][i];

rd = rd + a[i][m - 1 - i];

System.out.println("The sum of the left diagonal : " + ld);

System.out.println("The sum of the right diagonal : " + rd);

} else {

System.out.println("THE MATRIX SIZE IS OUT OF RANGE");

Step 1: Start

Step 2: Input the size m of the square matrix

Step 3:
→ If m ≤ 2 or m ≥ 10, then

→ Display: "THE MATRIX SIZE IS OUT OF RANGE"

→ Stop

→ Else, continue

Step 4: Declare a 2D array a[m][m] and initialize flag = 0, ld = 0, rd = 0

Step 5: Input the matrix elements

→ Loop i from 0 to m - 1

- Loop j from 0 to m - 1

→ Input a[i][j]

Step 6: Display the original matrix

→ Loop i from 0 to m - 1

- Loop j from 0 to m - 1

→ Print a[i][j] with a tab

- After each row, print a new line

Step 7: Check for symmetry

→ Loop i from 0 to m - 1

- Loop j from 0 to m - 1

→ If a[i][j] ≠ a[j][i], then

→ Set flag = 1 and break

Step 8:

→ If flag == 0, print "THE GIVEN MATRIX IS SYMMETRIC"

→ Else, print "THE GIVEN MATRIX IS NOT SYMMETRIC"

Step 9: Calculate diagonals

→ Loop i from 0 to m - 1

- Add a[i][i] to ld (left diagonal)

- Add a[i][m - 1 - i] to rd (right diagonal)

Step 10:

→ Print "The sum of the left diagonal : " + ld

→ Print "The sum of the right diagonal : " + rd


Step 11: Stop

Matrix3

Write a program to declare a square matrix A[ ][ ] of order MxM where ‘M’ is the number of rows
and the number of columns, such that M must be greater than 2 and less than 10. Accept the value
of M as user input. Display an appropriate message for an invalid input. Allow the user to input
integers into this matrix. Perform the following tasks:

(a) Display the original matrix.

(b) Rotate the matrix 90° clockwise as shown below:

Original matrix Rotated matrix

123 741

456 852

789 963

(c) Find the sum of the elements of the four corners of the matrix.

/* Name: MOHAMMED MANDSAURWALA

* unique id: 8495841

*/

import java.util.*;

class Martrix3 {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter the size of the matrix : ");

int m = sc.nextInt();

if (m < 3 || m > 9)

System.out.println("Size Out Of Range");

else {

int A[][] = new int[m][m];

/* Inputting the matrix */


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

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

System.out.print("Enter an element : ");

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

/* Printing the original matrix */

System.out.println("*");

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

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

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

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

System.out.println();

System.out.println("*");

int B[][] = new int[m][m];

int x;

/* Rotation of matrix begins here */

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

x = m - 1;

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

B[i][j] = A[x][i];

x--;

/* Printing the rotated matrix */


System.out.println("Matrix After Rotation is : ");

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

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

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

System.out.println();

System.out.println("*");

int sum = A[0][0] + A[0][m - 1] + A[m - 1][0] + A[m - 1][m - 1]; // Sum of corner elements

System.out.println("Sum of the corner elements = " + sum);

Step 1: Start

Step 2: Input m (size of square matrix)

Step 3:

→ If m < 3 or m > 9

→ Display "Size Out Of Range"

→ Stop

→ Else continue

Step 4: Declare 2D arrays A[m][m] and B[m][m]

Step 5: Input elements into matrix A

→ Loop i from 0 to m - 1

→ Loop j from 0 to m - 1

→ Input A[i][j]

Step 6: Display the original matrix A

→ Loop i from 0 to m - 1

→ Loop j from 0 to m - 1

→ Print A[i][j] with tab

Step 7: Rotate matrix A into matrix B by 90° clockwise


→ Loop i from 0 to m - 1

→ Set x = m - 1

→ Loop j from 0 to m - 1

→ Set B[i][j] = A[x][i]

→ Decrement x

Step 8: Display the rotated matrix B

→ Loop i from 0 to m - 1

→ Loop j from 0 to m - 1

→ Print B[i][j] with tab

Step 9: Calculate the sum of corner elements of A:

→ sum = A[0][0] + A[0][m-1] + A[m-1][0] + A[m-1][m-1]

Step 10: Display "Sum of the corner elements = " + sum

Step 11: Stop

String1

Write a program to accept a sentence which may be terminated by either ‘.’ ‘?’ or ‘!’ only. Any other
character may be ignored. The words may be separated by more than one blank space and are in
UPPER CASE.

Perform the following tasks:

(a) Accept the sentence and reduce all the extra blank space between two words to

a single blank space.

(b) Accept a word from the user which is part of the sentence along with its

position number and delete the word and display the sentence.

/* Name: MOHAMMED MANDSAURWALA

* unique id: 8495841

*/

import java.util.*;

class String1{

public static void main(String args[]) {


Scanner sc = new Scanner(System.in);

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

String s = sc.nextLine();

s = s.toUpperCase();

int l = s.length();

char last = s.charAt(l - 1);

if (last != '.' && last != '?' && last != '!') {

System.out.println("Invalid Input. End a sentence with either '.', '?' or '!' only");

} else {

StringTokenizer str = new StringTokenizer(s, " .?!");

int c = str.countTokens();

String w = "", ans = "";

System.out.print("Enter the word to delete : ");

String del = sc.next();

System.out.print("Enter the word position in the sentence : ");

int x = sc.nextInt();

if (x < 1 || x > c) {

System.out.println("Sorry! The word position entered is out of range");

} else {

for (int i = 1; i <= c; i++) {

w = str.nextToken();

if (w.equals(del) == true && i == x)

continue;

ans = ans + w + " ";

System.out.print("Output : " + ans.trim() + last);


}

Step 1: Start

Step 2: Input a sentence s

Step 3: Convert s to uppercase

Step 4:

If the last character of s is not ., ?, or !, then

Print "Invalid Input. End a sentence with either '.', '?' or '!' only"

Stop

Step 5:

Tokenize the sentence s using delimiters " .?!"

Let c be the number of tokens (words)

Step 6: Input the word del to delete

Step 7: Input the word position x

Step 8:

If x < 1 or x > c, then

Print "Sorry! The word position entered is out of range"

Stop

Step 9:

Initialize empty string ans

For i = 1 to c

Extract next token as word w

If w equals del and i == x, then

Skip (do not add to result)

Else

Append w and a space to ans

Step 10: Print "Output : " followed by ans (trimmed) and the original sentence-ending punctuation

Step 11: Stop

String2
Question 1:

An ISBN (International Standard Book Number) is a ten digit code which uniquely identifies a book.

The first nine digits represent the Group, Publisher and Title of the book and the last digit is used to
check whether ISBN is correct or not.

Each of the first nine digits of the code can take a value between 0 and 9. Sometimes it is necessary
to make the last digit equal to ten; this is done by writing the last digit of the code as X.

To verify an ISBN, calculate 10 times the first digit, plus 9 times the second digit, plus 8 times the
third and so on until we add 1 time the last digit. If the final number leaves no remainder when
divided by 11, the code is a valid ISBN.

/* Name: MOHAMMED MANDSAURWALA

* unique id: 8495841

*/

import java.util.*;

class String2 {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a 10 digit code : ");

String s = sc.nextLine();

int l = s.length();

if (l != 10) {

System.out.println("Output : Invalid Input");

} else {

String ch;

int a = 0, sum = 0, k = 10;

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

ch = Character.toString(s.charAt(i));
// Check for 'X' only in the last position

if (ch.equalsIgnoreCase("X")) {

if (i != 9) {

System.out.println("Output : Invalid Input - 'X' only allowed at last position");

return;

a = 10;

} else {

try {

a = Integer.parseInt(ch); // Will throw if not a digit

} catch (NumberFormatException e) {

System.out.println("Output : Invalid Input - Only digits or 'X' allowed");

return;

sum = sum + a * k;

k--;

System.out.println("Output : Sum = " + sum);

if (sum % 11 == 0)

System.out.println("Leaves No Remainder - Valid ISBN Code");

else

System.out.println("Leaves Remainder - Invalid ISBN Code");

Step 1: Start
Step 2: Input a 10-character string s

Step 3:

If length of s ≠ 10, then

Print "Output : Invalid Input"

Stop

Step 4:

Initialize sum = 0, k = 10

Step 5:

For i = 0 to 9

Set ch = s.charAt(i)

If ch is 'X' or 'x'

If i ≠ 9, then

Print "Output : Invalid Input - 'X' only allowed at last position"

Stop

Set a = 10

Else

Try to convert ch to integer a

If conversion fails (i.e., not a digit), then

Print "Output : Invalid Input - Only digits or 'X' allowed"

Stop

Update sum = sum + a * k

Decrement k by 1

Step 6: Print "Output : Sum = " followed by sum

Step 7:

If sum % 11 == 0, then

Print "Leaves No Remainder - Valid ISBN Code"

Else

Print "Leaves Remainder - Invalid ISBN Code"

Step 8: Stop

String3
The names of the teams participating in a competition should be displayed on a banner vertically, to
accommodate as many teams as possible in a single banner. Design a program to accept the names
of N teams, where 2 < N < 9 and display them in vertical order, side by side with a horizontal tab (i.e.
eight spaces).

/* Name: MOHAMMED MANDSAURWALA

* unique id: 8495841

*/

import java.util.*;

public class String3 {

public static void main() {

Scanner sc = new Scanner(System.in);

String ar[];

int n, i, j;

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

n = sc.nextInt();

sc.nextLine(); // consume the leftover newline

ar = new String[n];

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

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

ar[i] = sc.nextLine();

int max = 0;

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

if (max < ar[i].length())

max = ar[i].length();

System.out.println("OUTPUT:");

for (i = 0; i < max; i++) {


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

if (i < ar[j].length())

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

else

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

System.out.println();

*Step 1:* Start

*Step 2:* Input the number of names n

*Step 3:* Create a string array ar[n]

*Step 4:*

For i = 0 to n - 1

Input ar[i]

*Step 5:*

Initialize max = 0

For i = 0 to n - 1

If length of ar[i] > max

Set max = length of ar[i]

*Step 6:* Print "OUTPUT:"

*Step 7:*

For i = 0 to max - 1

For j = 0 to n - 1

If i < length of ar[j]

Print ar[j].charAt(i) followed by a tab

Else

Print a tab

Print a newline

*Step 8:* Stop


Number1

Question 1:

A Circular Prime is a prime number that remains prime under cyclic shifts of its digits.

When the leftmost digit is removed and replaced at the end of the remaining string of digits,

the generated number is still prime. The process is repeated until the original number is reached

again.A number is said to be prime if it has only two factors I and itself.

/* Name: MOHAMMED MANDSAURWALA

* unique id: 8495841

*/

import java.util.*;

class Number1 {

boolean isPrime(int n) // Function for checking whether a number is prime or not

int c = 0;

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

if (n % i == 0)

c++;

if (c == 2)

return true;

else

return false;

int circulate(int n) // Function for circulating the digits to form new number

String s = Integer.toString(n);

String p = s.substring(1) + s.charAt(0);

int a = Integer.parseInt(p);

return a;
}

void isCircularPrime(int n) // Function to check for circular prime

int f = 0, a = n;

do {

System.out.println(a);

if (isPrime(a) == false) {

f = 1;

break;

a = circulate(a);

} while (a != n);

if (f == 1)

System.out.println(n + " IS NOT A CIRCULAR PRIME");

else

System.out.println(n + " IS A CIRCULAR PRIME");

public static void main(String args[]) {

Number1 ob = new Number1();

Scanner sc = new Scanner(System.in);

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

int n = sc.nextInt();

ob.isCircularPrime(n);

*Step 1:* Start


*Step 2:* Input a number n

*Step 3:* Set a = n and f = 0

*Step 4:* Repeat

Print a

Call function isPrime(a)

If a is not prime

Set f = 1

Break the loop

Call function circulate(a) and store result in a

Until a == n

*Step 5:*

If f == 1, then

Print n + " IS NOT A CIRCULAR PRIME"

Else

Print n + " IS A CIRCULAR PRIME"

*Step 6:* Stop

Number2

Question 1:

A company manufactures packing cartons in four sizes,

i.e. cartons to accommodate 6 boxes, 12 boxes, 24 boxes and 48 boxes.

Design a program to accept the number of boxes to be packed (N)

by the user (maximum up to 1000 boxes) and display the break-up

of the cartons used in descending order of capacity (i.e. preference

should be given to the highest capacity available,

and if boxes left are less than 6, an extra carton of capacity 6 should be used.)

/* Name: MOHAMMED MANDSAURWALA

* unique id: 8495841

*/
import java.util.*;

class NUMBER2

public static void main(String args[])

Scanner sc = new Scanner(System.in);

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

int N = sc.nextInt();

if (N < 1 || N > 1000)

System.out.println("INVALID INPUT");

else

int cart[] = {48, 24, 12, 6};

int copy = N;

int totalCart = 0, count = 0;

System.out.println("OUTPUT :");

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

count = N / cart[i];

if (count != 0)

System.out.println("\t" + cart[i] + "\tx\t" + count + "\t= " + (cart[i] * count));

totalCart = totalCart + count;

N = N % cart[i];
}

if (N > 0)

System.out.println("\tRemaining Boxes " + N + " x 1 = " + N);

totalCart = totalCart + 1;

else

System.out.println("\tRemaining Boxes\t\t= 0");

System.out.println("\tTotal number of boxes = " + copy);

System.out.println("\tTotal number of cartons = " + totalCart);

*Step 1:* Start

*Step 2:* Input the number of boxes N

*Step 3:*

If N < 1 or N > 1000, then

Print "INVALID INPUT"

Stop

*Step 4:*

Initialize array cart = {48, 24, 12, 6}

Set copy = N, totalCart = 0, count = 0

*Step 5:* Print "OUTPUT :"

*Step 6:*

For i = 0 to 3

Set count = N / cart[i]

If count ≠ 0, then
Print cart[i] x count = cart[i] * count

Add count to totalCart

Set N = N % cart[i]

*Step 7:*

If N > 0, then

Print "Remaining Boxes N x 1 = N"

Add 1 to totalCart

Else

Print "Remaining Boxes = 0"

*Step 8:*

Print "Total number of boxes = copy"

Print "Total number of cartons = totalCart"

*Step 9:* Stop

Number3

Question 1:

Given two positive numbers M and N, such that M is between 100 and 10000 and N is less than 100.
Find the smallest integer that is greater than M and whose digits add up to N. For example, if M =
100 and N = 11, then the smallest integer greater than 100 whose digits add up to 11 is 119.

Write a program to accept the numbers M and N from the user and print the smallest required
number whose sum of all its digits is equal to N. Also, print the total number of digits present in the
required number. The program should check for the validity of the inputs and display an appropriate
message for an invalid input.

/* Name: MOHAMMED MANDSAURWALA

* unique id: 8495841

*/

import java.util.*;

class Number3

int sumDig(long n)

int sum = 0, d;
while (n > 0)

d = (int)(n % 10);

sum = sum + d;

n = n / 10;

return sum;

int countDig(long n)

String s = Long.toString(n);

int len = s.length();

return len;

public static void main() throws Exception

Number3 ob = new Number3();

Scanner sc = new Scanner(System.in);

System.out.print("Enter a value of 'm' from 100 to 10000 : ");

int m = sc.nextInt();

System.out.print("Enter a value of n from 1 to 99 : ");

int n = sc.nextInt();

if (m < 100 || m > 10000 || n < 1 || n > 99)

System.out.println("Invalid Input");

}
else

long i = (long) m;

while (ob.sumDig(i) != n)

i = i + 1;

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

System.out.println("Total number of digits = " + ob.countDig(i));

*Step 1:* Start

*Step 2:* Input m (100 ≤ m ≤ 10000)

*Step 3:* Input n (1 ≤ n ≤ 99)

*Step 4:*

If m < 100 or m > 10000 or n < 1 or n > 99, then

Print "Invalid Input"

Stop

*Step 5:*

Set i = m

Repeat

Call sumDig(i) to compute sum of digits of i

If result ≠ n, then increment i by 1

Until sumDig(i) == n

*Step 6:*

Print "The required number = " followed by i

Call countDig(i) to get number of digits in i

Print "Total number of digits = " followed by the result

*Step 7:* Stop


DATA PROGRAM

A class Admission contains the admission numbers of 100 students. Some of the data
members/member functions are given below: [10]
Class name: Admission
Data member/instance variable:
Adno[]: integer array to store admission numbers
Member functions/methods:
Admission(): constructor to initialize the array elements
void fill Array(): to accept the elements of the array in ascending order
int binSearch(int 1, int u, int v): to search for a particular admission number (v) using binary
search and recursive technique and returns 1 if found otherwise returns -1
Specify the class Admission giving details of the constructor, void fill Array() and int
binSearch(int, int, int). Define the main() function to create an object and call the functions
accordingly to enable the task.
/* Name: MOHAMMED MANDSAURWALA

* unique id: 8495841

*/

import java.util.Scanner;

class Admission {

int adno[];

public Admission() {

adno = new int[100];

public void fillArray(int n) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter " + n + " admission numbers in ascending order:");

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

adno[i] = sc.nextInt();

public int binSearch(int low, int high, int val) {

if (low > high)

return -1;
int mid = (low + high) / 2;

if (val == adno[mid])

return mid;

else if (val > adno[mid])

return binSearch(mid + 1, high, val);

else

return binSearch(low, mid - 1, val);

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

Admission obj = new Admission();

System.out.print("Enter number of admission numbers (max 100): ");

int n = sc.nextInt();

obj.fillArray(n);

System.out.print("Enter the admission number to search: ");

int key = sc.nextInt();

int result = obj.binSearch(0, n - 1, key);

if (result != -1)

System.out.println(key + " available at index " + result + ".");

else

System.out.println(key + " unavailable.");

🔄 *Algorithm: Binary Search for Admission Number*

*Step 1:* Start

*Step 2:* Input the number of admission numbers n (max 100)

*Step 3:*

Create array adno[100]

For i = 0 to n - 1
Input adno[i] in ascending order

*Step 4:* Input the admission number key to search

*Step 5:*

Call function binSearch(0, n - 1, key) and store result in result

*Step 6:*

If result ≠ -1

Print "key available at index result"

Else

Print "key unavailable"

*Step 7:* Stop

You might also like