COMPPPPP
COMPPPPP
PROJECT
2024-25
NAME- SHASHANT
CHAUHAN
CLASS- XII- I
ROLL NO. 45
ACKNOWLEDGEMENT
I would like to express my sincere gratitude to all
these individuals for mentoring and supporting
me in completing this project.
My teacher Mr. Faizan sir, for providing me with
invaluable insights and direction.
To my parents, their constant encouragement,
patience, and understanding have been the
pillars of my success.
I am grateful to my friends who contributed ideas
and perspectives that enriched the project.
Thank you everyone for shaping this project and
enhancing my learning experience.
INDEX
Question 1
Question 2
Question 3
Question 4
Question 5
Question 6
Question 7
Question 8
Question 9
Question 10
Question 11
Question 12
Question 13
Question 15
Question 16
Question 17
Question 18
Question 19
Question 20
QUESTION 1-
PROGRAM 1-
class prg1
{
int N,V,M,arr[][],S[][];
prg1()
{
Scanner sc=new Scanner(System.in);
System.out.println("input");
N=sc.nextInt();
System.out.println("please choose the next
number between 1- n^2");
V=sc.nextInt();
System.out.println("please choose choose the
order of submatrix and it should be less than first
inputted number");
M=sc.nextInt();
sc.close();
arr=new int[N][N];
S=new int[M][M];
}
void input()
{
int c=0;
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
arr[i][j]=++c;
}
void sub()
{
int c=V,temp=0,t=0;
for(int i=0;i<M;i++)
{
for(int j=0;j<M;j++)
{
if(j!=(M-1))
S[i][j]=c++;
else
{
S[i][j]=c-(N);
c=S[i][j];
}
}
c=S[i][0]+N;
}
}
void display()
{
System.out.println("matrix");
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
System.out.print(arr[i][j]+"\t");
System.out.println();
}
System.out.println("submatrix");
for(int i=0;i<M;i++)
{
for(int j=0;j<M;j++)
System.out.print(S[i][j]+"\t");
System.out.println();
}
}
void main()
{
prg1 obj=new prg1();
obj.input();
obj.sub();
obj.display();
}
}
ALGORITHM-
Start: Begin the program.
Class Definition:
Define a class named prg1.
Declare Variables:
Declare integer variables:
o N: Size of the main matrix (N x N).
o V: A user-defined integer (1 to N²).
o M: Size of the submatrix (M x M).
o arr[][]: 2D array to store the main matrix.
o S[][]: 2D array to store the submatrix.
Constructor (prg1):
Initialize N, V, and M to 0.
Method: Input (input):
Initialize Scanner: Create a Scanner object to
read user input.
Prompt User for Input:
1. Print "Input the order of the matrix".
2. Read N from user input.
3. Print "Please choose the next number between
1 and N²".
4. Read V from user input.
5. Print "Please choose the order of the
submatrix, and it should be less than the first
inputted number".
6. Read M from user input.
Close Scanner: Close the scanner object to avoid
resource leaks.
Initialize Arrays:
o Create and initialize the main matrix arr with
dimensions N x N.
o Create and initialize the submatrix S with
dimensions M x M.
Fill Main Matrix:
o Set a counter variable c to 0.
o Use nested loops to fill arr with sequential
numbers:
1. For each row i from 0 to N-1:
For each column j from 0 to N-1:
Increment c and assign it to arr[i]
[j].
Method: Submatrix (sub):
Initialize Variables:
o Set c to V.
Fill Submatrix:
o Use nested loops to fill S:
1. For each row i from 0 to M-1:
For each column j from 0 to M-1:
If j is not the last column (M-1):
Assign S[i][j] the value of c.
Increment c.
Else (if j is the last column):
Assign S[i][j] the value of c -
N.
Update c to the value
assigned to S[i][j].
After completing the inner loop, set c
to S[i][0] + N for the next row.
Method: Display (display):
Print Main Matrix:
o Print "Matrix:".
o Use nested loops to print elements of arr:
1. For each row i from 0 to N-1:
For each column j from 0 to N-1:
Print arr[i][j] with a tab.
Print a new line after each row.
Print Submatrix:
o Print "Submatrix:".
o Use nested loops to print elements of S:
1. For each row i from 0 to M-1:
For each column j from 0 to M-1:
Print S[i][j] with a tab.
Print a new line after each row.
Method: Main (main):
Create Object: Create an instance of the prg1
class.
Call Input Method: Call the input() method to
gather user input.
Call Submatrix Method: Call the sub() method to
fill the submatrix based on input.
Call Display Method: Call the display() method to
show both the main matrix and the submatrix.
End: Finish the program.
OUTPUT-
QUESTION 2-
PROGRAM 2-
import java.util.*;
class prg2
{
String ISBN;
prg2()
{
ISBN="";
}
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Input your ISBN");
ISBN=sc.next();
}
boolean check(int n)
{
if(n%11==0)
return true;
return false;
}
int sum()
{
int len=ISBN.length()-1;
String s=ISBN.substring(0,len);
int n=Integer.parseInt(s);
String s1=ISBN.substring(len);
int c=1,i=0,ss=0;
while(c<=10)
{
ss+=(n%10)*++c;
n/=10;
}
if(s1=="x")
ss+=10;
else
ss+=Integer.parseInt(s1);
return ss;
}
void display()
{
int summation=sum();
if(check(summation)==true&&ISBN.length()==10)
{
System.out.println("SUM="+summation);
System.out.println("This is a valid ISBN");
}
else if(ISBN.length()!=10)
{
System.out.println("SUm=invalid input");
}
else
{
System.out.println("SUM="+summation);
System.out.println("Leaves Remainder - invalid
ISBN");
}
}
void main()
{
prg2 obj=new prg2();
obj.input();
obj.sum();
obj.display();
}
ALGORITHM-
Start: Begin the program execution.
Class Definition: Define a class named prg2.
Declare Variables:
Declare a string variable ISBN to store the ISBN
number input by the user.
Constructor (prg2):
Initialize ISBN to an empty string.
Input Method (input):
Create a Scanner object to facilitate user input.
Display a prompt: "Input your ISBN".
Read the ISBN string input from the user and store
it in the ISBN variable.
Check Method (check(int n)):
Input: An integer n.
If n modulo 11 equals 0:
o Return true (indicating the number is valid).
Else:
o Return false (indicating the number is not
valid).
Sum Method (sum()):
Initialize an integer variable ss to 0 (this will hold
the weighted sum).
Get the length of the ISBN string and store it in an
integer variable len.
For c from 1 to len (inclusive):
o Retrieve the character at index c - 1 of ISBN
and store it in a variable currentChar.
o If currentChar is 'X' or 'x':
Add 10 multiplied by c to ss.
o Else:
Convert currentChar to its integer value
using Character.getNumericValue() and
add its value multiplied by c to ss.
Return the total weighted sum ss.
Display Method (display):
Call the sum() method and store the result in an
integer variable summation.
If the length of ISBN is 10:
o Print "SUM = " followed by the value of
summation.
o If check(summation) returns true:
Print "This is a valid ISBN".
o Else:
Print "Leaves Remainder - invalid ISBN".
Else:
o Print "SUM = invalid input".
Main Method (main):
Create an instance of the prg2 class.
Call the input() method to gather user input.
Call the display() method to validate and show the
result for the ISBN.
OUTPUT-
QUESTION 3
PROGRAM-3
import java.util.*;
class Numbers
{
int N, Num,Prev,Arr[][];
Numbers(int a,int b)
{
N=a;
Num=b;
Arr=new int[N][N];
Prev=0;
}
Numbers check(int n)
{
int i=0;
for(i=1;;i++)
{
if((int)(i*(i+1)/2)==n)
{
this.Num=n;
this.Prev=i;
return this;
}
else if((int)(i*(i+1)/2)>n)
{
this.Num=((int)(i*(i+1))/2);
this.Prev=i;
return this;
}
}
}
void fill()
{
int c=Num;
for(int i=0;i<Arr.length;i++)
{
for(int j=0;j<Arr.length;j++)
{
if(j==0||i==0||i==Arr.length-1||
j==Arr.length-1)
{
check(c);
c=Num;
Arr[i][j]=c;
c=Prev+Num;
}
}
}
}
void display()
{
for(int i=0;i<Arr.length;i++)
{
for(int j=0;j<Arr.length;j++)
{
System.out.print(Arr[i][j]+"\t");
}
System.out.println();
}
}
void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("input order of matrix");
int a=sc.nextInt();
System.out.println("input number");
int b=sc.nextInt();
Numbers obj=new Numbers(a,b);
obj.fill();
obj.display();}}
ALGORITHM
Start: Begin the program execution.
Class Definition:
Define a class named Numbers.
Declare Variables:
Declare four instance variables:
o int N: to store the order of the matrix.
o int Num: to store the starting value of
triangular numbers.
o int Prev: to store the last value which
generated a triangular number.
o int Arr[][]: a 2D array to store the triangular
numbers in the matrix.
Constructor (Numbers(int a, int b)):
Initialize N with a (the size of the matrix).
Initialize Num with b (the starting number for
triangular numbers).
Initialize Arr as a 2D array of size N x N.
Initialize Prev to 0.
Check Method (Numbers check(int n)):
Input: An integer n (the number to check).
Use a loop to determine whether n is a triangular
number:
o For each integer i starting from 1:
Calculate the triangular number T(i) = i *
(i + 1) / 2.
If T(i) equals n:
Set this.Num to n.
Set this.Prev to i.
Return the current object.
Else if T(i) is greater than n:
Set this.Num to T(i).
Set this.Prev to i.
Return the current object.
Fill Method (void fill()):
Initialize a variable c with Num.
Use nested loops to iterate through the rows (i)
and columns (j) of Arr:
o If the current position is on the boundary (i.e.,
first row, last row, first column, or last
column):
Call check(c) to find the next triangular
number.
Set c to Num.
Assign Arr[i][j] to c (the current triangular
number).
Update c to Prev + Num (to get the next
triangular number).
Display Method (void display()):
Use nested loops to print the contents of the
matrix Arr:
o Print each element followed by a tab for
spacing.
o Move to the next line after completing a row.
Main Method (void main()):
Create a Scanner object to read user input.
Prompt the user for the order of the matrix and
read the input into a.
Prompt the user for the starting number and read
the input into b.
Create an instance of the Numbers class with a and
b.
Call the fill() method to populate the matrix.
Call the display() method to print the matrix.
End: Finish the program.
OUTPUT-
QUESTION 4
PROGRAM 4-
import java.util.*;
class point
{
int A[][],R,C;
point(int p, int q)
{
R=p;
C=q;
A=new int[R][C];
}
void get_array()
{
Scanner sc=new Scanner(System.in);
System.out.println("input values");
for(int i=0;i<R;i++)
{
for(int j=0;j<C;j++)
{
A[i][j]=sc.nextInt();
}
}
}
void display_mat()
{
System.out.println("input matrix");
for(int i=0;i<R;i++)
{
for(int j=0;j<C;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
}
int check_pseudo(int a[])
{
int temp=0,t=0,flag=0;
if(C%2==0)
{
t=a[0]+a[C-1];
for(int i=0;i<(C/2);i++)
{
temp=a[i]+a[C-i-1];
if(t!=temp)
{
flag++;
break;
}
}
}
else
{
t=a[0]+a[C-1];
for(int i=0;i<(C+1)/2;i++)
{
if(i==((C+1)/2)-1)
{
temp=2*a[i];
}
else
{
temp=a[i]+a[C-i-1];
}
if(t!=temp)
{
flag++;
break;
}
}
}
if(flag==1)
return 0;
return 1;
}
void result()
{
int b[]=new int[C];
for(int i=0;i<R;i++)
{
for(int j=0;j<C;j++)
{
b[j]=A[i][j];
}
int f=check_pseudo(b);
if(f!=0)
{
System.out.println("row "+(i+1)+ " pseudo
numbers");
}
else
{
System.out.println("row "+(i+1)+ "not
pseudo numbers");
}
}
}
void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("input no.of rows");
int a=sc.nextInt();
System.out.println("input no. of columns");
int b=sc.nextInt();
point obj=new point(a,b);
obj.get_array();
obj.display_mat();
obj.result();
}
}
ALGORITHM-
Start the program.
Define the Class point:
Data Members:
o int A[][]: 2D array to store the matrix.
o int R: Integer to store the number of rows.
o int C: Integer to store the number of columns.
Constructor: point(int p, int q)
Input: Two integers, p (rows) and q (columns).
Operation:
o Assign p to R.
o Assign q to C.
o Initialize the 2D array A with dimensions R x C.
Method: void get_array()
Operation:
o Create a Scanner object to read input.
o Print "input values".
o Use nested loops to read values into the
matrix A.
For each row i from 0 to R-1:
For each column j from 0 to C-1:
Read an integer value and assign
it to A[i][j].
Method: void display_mat()
Operation:
o Print "input matrix".
o Use nested loops to display the matrix A.
For each row i from 0 to R-1:
For each column j from 0 to C-1:
Print A[i][j] followed by a tab
character.
Print a newline after each row.
Method: int check_pseudo(int a[])
Input: An integer array a[] (a row of the matrix).
Operation:
o Initialize temp, t, and flag to 0.
o If C is even:
Set t to the sum of the first and last
elements of a[].
Loop from i = 0 to C/2 - 1:
Calculate temp as the sum of a[i] and
a[C-i-1].
If t is not equal to temp, set flag to 1
and break the loop.
o Else (if C is odd):
Set t to the sum of the first and last
elements of a[].
Loop from i = 0 to (C+1)/2 - 1:
If i is the middle index, set temp to 2
* a[i].
Else, calculate temp as the sum of
a[i] and a[C-i-1].
If t is not equal to temp, set flag to 1
and break the loop.
o Return 1 if flag is 0 (indicating a pseudo
number row), otherwise return 0.
Method: void result()
Operation:
o Create an integer array b[] with size C to hold
each row's values temporarily.
o Loop through each row i from 0 to R-1:
Use a nested loop to copy the i-th row of A
into b[].
Call check_pseudo(b) and store the result
in f.
If f is not equal to 0:
Print "row (i+1) pseudo numbers".
Else:
Print "row (i+1) not pseudo numbers".
Method: void main()
Operation:
o Create a Scanner object to read input.
o Print "input no. of rows" and read an integer a.
o Print "input no. of columns" and read an
integer b.
o Create an instance of the point class with a
and b.
o Call get_array(), display_mat(), and result()
methods in sequence.
End of the Program
OUTPUT-
QUESTION 5
PROGRAM 5-
import java.util.*;
class Replace
{
String str,substr;
Replace()
{
str="";
substr="";
}
Replace(String s)
{
str=s;
substr="";
}
void display()
{
System.out.println("Str= "+str);
System.out.println("substr= "+substr);
System.out.println("It should return "+
(substr.length()));
}
void getword(String ss)
{
substr=ss;
}
void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("input");
String a=sc.nextLine();
String b=sc.nextLine();
Replace obj=new Replace(a);
obj.getword(b);
obj.display();
}
}
ALGORITHM-
Start the Program.
Define the Class Replace:
Data Members:
o String str: A string to store the main string.
o String substr: A string to store the substring.
Constructor: Replace()
Operation:
o Initialize str to an empty string.
o Initialize substr to an empty string.
Constructor: Replace(String s)
Input: A string s.
Operation:
o Assign the input string s to str.
o Initialize substr to an empty string.
Method: void display()
Operation:
o Print "Str= " followed by the value of str.
o Print "substr= " followed by the value of
substr.
o Print "It should return " followed by the length
of substr.
Method: void getword(String ss)
Input: A string ss.
Operation:
o Assign the input string ss to substr.
Method: void main()
Operation:
o Create a Scanner object to read input.
o Print "input".
o Read a line of input into string a.
o Read a second line of input into string b.
o Create an instance of the Replace class,
passing a as an argument to the constructor.
o Call the getword(b) method on the object to
set substr.
o Call the display() method to print the details of
str and substr.
End of the Program.
OUTPUT-
QUESTION 6
PROGRAM 6-
import java.util.*;
class prg3
{
void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("INPUT:");
int p=sc.nextInt();
int q=sc.nextInt();
int freq=0;
System.out.println("OUTPUT");
System.out.println("THE KAPREKAR NUMBERS
ARE:");
for(int i=p;i<q;i++)
{
int temp=(int)Math.pow(i,2);
int c=0,t=temp;
while(t!=0)
{
c++;
t/=10;
}
if(c%2==0)
{
int v=(int)(temp%Math.pow(10,c/2))+(int)
(temp/Math.pow(10,(int)c/2));
if(v==i)
{
System.out.print(i+"\t");
freq++;
}
}
else
{
int v=(int)(temp%Math.pow(10,(c+1)/2))+
(int)(temp/Math.pow(10,(c+1)/2));
if(v==i)
{
System.out.print(i+"\t");
freq++;
}
}
}
System.out.println();
System.out.print("FREQUENCY OF KAPREKAR
NUMBERS IS:"+freq);}}
ALGORITHM-
Start the Program.
Define the Class prg3.
Method: void main()
Initialize a Scanner object to read input from
the user.
Print: "INPUT:".
Read input:
o Read an integer p (the lower bound of the
range).
o Read an integer q (the upper bound of the
range).
Initialize a variable freq to 0 to keep track of
the number of Kaprekar numbers found.
Print: "OUTPUT".
Print: "THE KAPREKAR NUMBERS ARE:".
Loop through each integer i from p to q-1:
Calculate temp as i squared (temp = i^2).
Initialize a variable c to 0 to count the number
of digits in temp.
Create a variable t and set it equal to temp
(used for counting digits).
While t is not 0:
o Increment c by 1 (counting digits).
o Divide t by 10 (removing the last digit).
Check if the number of digits c is even:
o If true:
Calculate v as the sum of:
The last c/2 digits of temp (temp %
Math.pow(10, c/2)).
The first c/2 digits of temp (temp /
Math.pow(10, c/2)).
o If false:
Calculate v as the sum of:
The last (c+1)/2 digits of temp (temp
% Math.pow(10, (c+1)/2)).
The first (c-1)/2 digits of temp (temp /
Math.pow(10, (c+1)/2)).
Check if v equals i:
o If true:
Print i (as it is a Kaprekar number).
Increment freq by 1 (counting the
Kaprekar number).
End of Loop.
Print a newline.
Print: "FREQUENCY OF KAPREKAR NUMBERS IS:"
followed by the value of freq.
End of the Program.
OUTPUT-
QUESTION 7-
PROGRAM 7:
import java.util.*;
class prg4
{
void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("input M");
int M=sc.nextInt();
System.out.println("input N");
int N=sc.nextInt();
int n=M*N,c=0;
int A[][]=new int[M][N];
int B[]=new int[n];
for(int i=11;c<n;i++)
{
int t=i;
int s=0;
while(t!=0)
{
s=s*10+t%10;
t/=10;
}
if(s==i)
{
B[c]=i;
c++;
}
}
c=0;
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
{
A[i][j]=B[c++];
}
}
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
long s=0;
for(int i=1;i<M-1;i++)
{
for(int j=1;j<N-1;j++)
{
s+=A[i][j];
}
}
System.out.println("Sum of non-border elements-
"+s);
}
}
ALGORITHM
Start the Program.
Define the Class prg4.
Method: void main()
Initialize a Scanner object to read input from
the user.
Print: "input M".
Read integer M (the number of rows of the
matrix).
Print: "input N".
Read integer N (the number of columns of the
matrix).
Calculate n as M * N (the total number of
elements required).
Initialize a variable c to 0 (to count how many
palindrome numbers have been found).
Create a 2D array A of size M x N to store the
palindrome numbers.
Create a 1D array B of size n to temporarily
hold palindrome numbers.
Loop through integers starting from 11:
Set t equal to i (the current integer).
Initialize s to 0 (to reverse the digits of t).
While t is not 0:
o Update s to append the last digit of t (s = s *
10 + t % 10).
o Divide t by 10 (removing the last digit).
Check if s is equal to i:
o If true:
Store i in array B at index c.
Increment c by 1 (counting the
palindrome found).
Repeat until c reaches n (enough palindromes
are found).
Reset c to 0 (to fill the 2D array).
Loop through the indices of the 2D array A:
For each row i from 0 to M-1:
o For each column j from 0 to N-1:
Assign A[i][j] the value from B[c] (filling
the matrix with palindromes).
Increment c by 1.
Display the 2D array A:
Loop through the indices of the 2D array:
o For each row i:
For each column j:
Print A[i][j] followed by a tab.
Print a newline after each row.
Initialize a variable s to 0 (to calculate the sum of
non-border elements).
Loop through the non-border elements of the
matrix:
For each row i from 1 to M-2:
o For each column j from 1 to N-2:
Add the value of A[i][j] to s (summing the
non-border elements).
Print: "Sum of non-border elements- " followed by the
value of s.
End of the Program.
OUTPUT-
QUESTION 8-
PROGRAM 8-
import java.util.*;
class prg5
{
String S;
int m,n,c;
prg5()
{
S="";
n=0;
m=0;
c=0;
}
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("input");
S=sc.nextLine();
int flag=0;
for(;flag==1;)
if(S.charAt(S.length()-1)!='.'||S.charAt(S.length()-
1)!='?'||S.charAt(S.length()-1)!='!')
{
System.out.println("Wrong input. do it again");
S=sc.nextLine();
}
else
{
flag++;
}
System.out.println("input m");
m=sc.nextInt();
System.out.println("input n");
n=sc.nextInt();
c=S.length();
}
void main()
{
int count=1;
S=S+" ";
String w="",s="";
for(int i=0;i<=c;i++)
{
char a=S.charAt(i);
if(a!=' ')
{
if(a=='.'||a=='?'||a=='!')
w=w+a;
else
if(count==m||count==n)
{
if(a=='z'||a=='Z')
{
w=w+(char)(a-25);
}
else
{
w=w+(char)(a+1);
}
}
else
w=w+a;
}
else
{
count++;
s=s+w+a;
w="";
}
}
if(m<count&&n<count)
System.out.println(s);
else
System.out.println("invalid");
}
void start()
{
prg5 obj=new prg5();
obj.input();
obj.main();
}
}
ALGORITHM-
Start the Program.
Define the Class prg5.
Class Variables:
String S: To hold the input sentence.
Integers m, n, c: To hold specific word positions
and the length of the string.
Constructor prg5():
Initialize S to an empty string.
Initialize n, m, and c to 0.
Method: void input()
Create a Scanner object for user input.
Print: "input".
Read the input sentence S.
Initialize a variable flag to 0.
Loop until a valid sentence ending:
o Check the last character of S:
If the last character is not '.', '?', or '!',
print "Wrong input. do it again" and read S
again.
Otherwise, set flag to 1 (indicating valid
input).
Print: "input m".
Read integer m (the first specific word position).
Print: "input n".
Read integer n (the second specific word
position).
Set c to the length of S.
Method: void main()
Initialize an integer count to 1 (to count
words).
Append a space to S (to facilitate word
processing).
Initialize empty strings w and s.
Loop through each character of S (from index
0 to c):
o Store the current character in a.
o If a is not a space:
If a is a punctuation mark ('.', '?', '!'),
append it to w.
If count equals m or n:
If a is 'z' or 'Z', wrap around to the
start of the alphabet (e.g., change 'z'
to 'a' or 'Z' to 'A').
Otherwise, increment the character a
by 1 (e.g., change 'a' to 'b').
Append the modified character to w.
Otherwise, append the character a to w.
o If a is a space:
Increment count by 1 (indicating the end
of a word).
Append w and the space to s, then reset w
to an empty string.
Check if m and n are valid word positions:
o If both m and n are less than or equal to count,
print the modified sentence s.
o Otherwise, print "invalid".
Method: void start()
Create an object of prg5.
Call input() and then main().
End of the Program.
OUTPUT-
QUESTION 9-
PROGRAM 9-
import java.util.*;
class prg6
{
void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("input no. of rows");
int M=sc.nextInt();
System.out.println("Input no. of columns");
int N=sc.nextInt();
int A[][]=new int[M][N];
System.out.println("enter value");
for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
A[i][j]=sc.nextInt();
System.out.println("original matrix");
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
System.out.print(A[i][j]+"\t");
System.out.println();
}
int max=A[0][0],a=0,b=0;
int min=A[0][0],c=0,d=0;
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
{
if(A[i][j]>max)
{
max=A[i][j];
a=i;
b=j;
}
if(A[i][j]<min)
{
min=A[i][j];
c=i;
d=j;
}
}
}
System.out.println("Largest number: "+max);
System.out.println("Row: "+a);
System.out.println("Column: "+b);
System.out.println("Smallest number: "+min);
System.out.println("Row: "+c);
System.out.println("Column: "+d);
int e=0,f=0;
int s[]=new int[M*N];
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
{
s[e++]=A[i][j];
}
}
for(int i=0;i<=e;i++)
{
for(int j=0;j<e-i-1;j++)
{
if(s[j]>s[j+1])
{
f=s[j];
s[j]=s[j+1];
s[j+1]=f;
}
}
}
e=0;
for(int i=0;i<M;i++)
for(int j=0;j<N;j++)
A[i][j]=s[e++];
System.out.println("Rearranged matrix: ");
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
System.out.print(A[i][j]+"\t");
System.out.println();
}
}
}
ALGORITHM
Start the Program.
Define the Class prg6.
Method: void main()
Create a Scanner object for user input.
Print: "input no. of rows".
Read the number of rows M.
Print: "Input no. of columns".
Read the number of columns N.
Initialize a 2D array A of size M x N.
Print: "enter value".
Loop to populate the matrix:
o For each row i from 0 to M-1:
For each column j from 0 to N-1:
Read the value and store it in A[i][j].
Print: "original matrix".
Loop through the matrix to display its
contents:
o For each row i from 0 to M-1:
For each column j from 0 to N-1:
Print A[i][j] with a tab space.
Print a newline.
Find Maximum and Minimum Values in the
Matrix:
Initialize max to A[0][0], and variables a, b to
track the position of max.
Initialize min to A[0][0], and variables c, d to
track the position of min.
Loop through the matrix to find max and min:
o For each row i from 0 to M-1:
For each column j from 0 to N-1:
If A[i][j] is greater than max:
Update max to A[i][j].
Update a to i and b to j.
If A[i][j] is less than min:
Update min to A[i][j].
Update c to i and d to j.
Print the Results:
Print the largest number, its row and column
indices.
Print the smallest number, its row and column
indices.
Flatten the Matrix:
Initialize an array s of size M*N to hold the
matrix elements.
Loop to flatten the matrix:
o Initialize an index e to 0.
o For each row i from 0 to M-1:
For each column j from 0 to N-1:
Assign A[i][j] to s[e] and increment e.
Sort the Flattened Array:
Loop to sort the array s using Bubble Sort:
o For i from 0 to e-1:
For j from 0 to e-i-2:
If s[j] > s[j+1]:
Swap s[j] and s[j+1].
Refill the Original Matrix with Sorted Values:
Reset e to 0.
For each row i from 0 to M-1:
o For each column j from 0 to N-1:
Assign s[e] to A[i][j] and increment e.
Print the Rearranged Matrix:
Print: "Rearranged matrix:".
Loop through the rearranged matrix to
display its contents:
o For each row i from 0 to M-1:
For each column j from 0 to N-1:
Print A[i][j] with a tab space.
rint a newline.
End of the Program.
OUTPUT-
QUESTION 10-
PROGRAM 10-
import java.util.*;
class Arrange
{
int arr[],size;
Arrange(int n)
{
size=n;
arr=new int[n];
}
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("input values in array");
for(int i=0;i<size;i++)
{
arr[i]=sc.nextInt();
}
}
void arrange()
{
int mid=0,c=1;
mid=size/2;
int brr[]=new int[size];
for(int i=0;i<size;i++)
{
for(int j=0;j<size-1-i;j++)
{
if(arr[j]>arr[j+1])
{
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
brr[mid]=arr[0];
for(int i=1;i<size-1;i+=2)
{
brr[mid-c]=arr[i];
brr[mid+c]=arr[i+1];
c++;
}
arr=brr;
}
void display()
{
System.out.println("input");
for(int i=0;i<size;i++)
System.out.print(arr[i]+"\t");
System.out.println();
Arrange o=new Arrange(size);
o.size=size;
o.arr=arr;
o.arrange();
System.out.println("output");
for(int i=0;i<size;i++)
{
System.out.print(o.arr[i]+"\t");
}
}
void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("input size");
int a=sc.nextInt();
Arrange obj=new Arrange(a);
obj.input();
obj.display();
}
}
ALGORITHM-
Start the Program.
Define the Class prg6.
Method: void main()
Create a Scanner object for user input.
Print: "input no. of rows".
Read the number of rows M.
Print: "Input no. of columns".
Read the number of columns N.
Initialize a 2D array A of size M x N.
Print: "enter value".
Loop to populate the matrix:
o For each row i from 0 to M-1:
For each column j from 0 to N-1:
Read the value and store it in A[i][j].
Print: "original matrix".
Loop through the matrix to display its
contents:
o For each row i from 0 to M-1:
For each column j from 0 to N-1:
Print A[i][j] with a tab space.
Print a newline.
Find Maximum and Minimum Values in the
Matrix:
Initialize max to A[0][0], and variables a, b to
track the position of max.
Initialize min to A[0][0], and variables c, d to
track the position of min.
Loop through the matrix to find max and min:
o For each row i from 0 to M-1:
For each column j from 0 to N-1:
If A[i][j] is greater than max:
Update max to A[i][j].
Update a to i and b to j.
If A[i][j] is less than min:
Update min to A[i][j].
Update c to i and d to j.
Print the Results:
Print the largest number, its row and column
indices.
Print the smallest number, its row and column
indices.
Flatten the Matrix:
Initialize an array s of size M*N to hold the
matrix elements.
Loop to flatten the matrix:
o Initialize an index e to 0.
o For each row i from 0 to M-1:
For each column j from 0 to N-1:
Assign A[i][j] to s[e] and increment e.
Sort the Flattened Array:
Loop to sort the array s using Bubble Sort:
o For i from 0 to e-1:
For j from 0 to e-i-2:
If s[j] > s[j+1]:
Swap s[j] and s[j+1].
Refill the Original Matrix with Sorted Values:
Reset e to 0.
For each row i from 0 to M-1:
o For each column j from 0 to N-1:
Assign s[e] to A[i][j] and increment e.
Print the Rearranged Matrix:
Print: "Rearranged matrix:".
Loop through the rearranged matrix to
display its contents:
o For each row i from 0 to M-1:
For each column j from 0 to N-1:
Print A[i][j] with a tab space.
Print a newline.
End of the Program.
OUTPUT-
QUESTION 11-
PROGRAM 11-
import java.util.*;
class point1
{
int A[][],row,col,rowi,coli;
point1(int n,int m)
{
row=n;
col=m;
rowi=0;
coli=0;
A=new int[row][col];
}
void get_array()
{
Scanner sc=new Scanner(System.in);
System.out.println("input values");
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
A[i][j]=sc.nextInt();
}
}
}
void display_mat()
{
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
System.out.print(A[i][j]+"\t");
System.out.println();
}
}
void check_saddle()
{
int i=0,j=0,min=0,c=0,flag=0;
for( i=0;i<row;i++)
{
int temp=A[i][j];
c=0;
for(j=0;j<col;j++)
{
if(A[i][j]<temp)
{
temp=A[i][j];
rowi=i;
coli=j;
}
}
for(j=0;j<row;j++)
if(A[j][coli]>temp)
{c++;}
if(c==0)
{System.out.println("Saddle point= "+temp+"
i= "+rowi+" j= "+coli);flag++;}
}
if(flag==0)
{
System.out.println("no saddle point");
}
}
void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("input row");
int a=sc.nextInt();
System.out.println("input column");
int b=sc.nextInt();
point1 obj=new point1(a,b);
obj.get_array();
obj.display_mat();
obj.check_saddle();
}
}
ALGORITHM
Start the Program.
Define the Class point1.
Attributes:
o int A[][]: 2D array to hold the matrix values.
o int row, col: To store the number of rows and
columns.
o int rowi, coli: To track the position of the
identified saddle point.
Constructor: point1(int n, int m)
Initialize row to n and col to m.
Initialize rowi and coli to 0.
Instantiate the 2D array A with dimensions row x
col.
Method: void get_array()
Create a Scanner object for user input.
Print: "input values".
Loop to populate the matrix:
o For each row i from 0 to row-1:
For each column j from 0 to col-1:
Read the value and store it in A[i][j].
Method: void display_mat()
Loop through the matrix to display its
contents:
o For each row i from 0 to row-1:
For each column j from 0 to col-1:
Print A[i][j] followed by a tab space.
Print a newline.
Method: void check_saddle()
Initialize variables: i = 0, j = 0, min = 0, c = 0,
and flag = 0.
Loop through each row i:
o Set temp to the first element of the
current row: temp = A[i][j].
o Find the minimum value in the current
row:
For each column j from 0 to col-1:
If A[i][j] < temp:
Update temp to A[i][j].
Update rowi to i and coli to j.
o Check if temp is a saddle point:
Initialize a counter c = 0.
For each row j from 0 to row-1:
If A[j][coli] > temp, increment c.
o If c == 0 (indicating temp is less than all
elements in its column):
Print the saddle point information: "Saddle
point= temp i= rowi j= coli".
Increment flag.
After checking all rows, if flag == 0:
o Print: "no saddle point".
Method: void main()
Create a Scanner object for user input.
Print: "input row".
Read the number of rows a.
Print: "input column".
Read the number of columns b.
Instantiate an object obj of class point1 using
the provided dimensions.
Call obj.get_array() to populate the matrix.
Call obj.display_mat() to display the matrix.
Call obj.check_saddle() to check for saddle points.
End of the Program.
OUTPUT-
QUESTION 12-
PROGRAM 12-
import java.util.*;
class prg7
{
static void main()
{
int i=0,j=0;
Scanner sc=new Scanner(System.in);
System.out.println("input deposit");
int amt=sc.nextInt();
String
arr[]={"ZERO","ONE","TWO","THREE","FOUR","FIVE","SI
X","SEVEN","EIGHT","NINE"};
int temp=amt;String s="";
while(temp!=0)
{
int d=temp%10;;
temp/=10;j++;
s=arr[d]+" "+s;
}
System.out.println(s);
temp=amt;
if(j>5)
{
System.out.println("INVALID AMOUNT");
}
else
{
System.out.println("DENOMINATION:");
int a[]={1000,500,100,50,20,10,5,2,1};
int count=0,sum=0;
while(temp!=0)
{
count=(int)(temp/a[i]);
if(count>0)
System.out.println(a[i]+" X "+count+" =
"+(a[i]*count));
temp-=(a[i]*count);
sum+=count;
i++;
}
System.out.println("total="+amt);
System.out.println("total number of
notes"+sum);
}
}
}
ALGORITHM-
Start the Program.
Define the Class prg7.
Contains the main() method which performs all
operations.
Method: static void main()
Initialize variables:
o i = 0, j = 0: Used for looping and counting
digits.
o Scanner sc = new Scanner(System.in): To take
input from the user.
Input the Deposit Amount.
Print: "input deposit".
Read the amount using sc.nextInt() and store it
in int amt.
Define Array for Number Words.
Initialize a String arr[] array to store words for
numbers 0 to 9.
Convert Deposit Amount to Words.
Set temp = amt to store the amount in a
temporary variable.
Initialize String s to accumulate the words.
While temp is not 0:
o Extract the last digit d = temp % 10.
o Divide temp by 10 to remove the last digit.
o Increment j to count the number of digits.
o Prepend arr[d] to s to build the amount in
words from right to left.
Print: s (the amount in words).
Check for Valid Amount.
If j > 5:
o Print "INVALID AMOUNT" and exit the
denomination calculation.
Calculate Denominations if Amount is Valid.
Print: "DENOMINATION:".
Define an array a[] with denominations [1000,
500, 100, 50, 20, 10, 5, 2, 1].
Initialize count = 0, sum = 0 to track the
number of notes.
While temp is not 0:
o Calculate the count of each denomination as
count = temp / a[i].
o If count > 0:
Print the denomination and its count as
a[i] + " X " + count + " = " + (a[i] *
count).
o Subtract the denomination value from temp as
temp -= (a[i] * count).
o Add count to sum to accumulate the total
number of notes.
o Increment i to proceed to the next
denomination.
Print the Total Amount and Number of Notes.
Print "total=" + amt.
Print "total number of notes" + sum.
End of the Program.
OUTPUT-
QUESTION 13
PROGRAM 13
import java.util.*;
class prg8
{
static void main(int n)
{
String[] arr=new String[n];
System.out.println("input sentences");
Scanner sc=new Scanner(System.in);
int len=arr.length,flag=0;;
if(n>1&&n<10)
{
for(int i=0;i<len;i++)
{
arr[i]=sc.nextLine();
}
flag++;
}
else
{
System.out.println("Condition not
satisfied");
}
if(flag==1){
for(int i=0;i<len;i++)
{
if(i%2!=0)
{
String w="",s="";
arr[i]+=" ";
int len1=arr[i].length();
for(int j=0;j<len1;j++)
{
if(arr[i].charAt(j)!=' ')
{
w+=arr[i].charAt(j);
}
else
{
s=w+" "+s;
w="";
}
}
arr[i]=s;
}
else
{
String w="",s="";
arr[i]+=" ";
int len1=arr[i].length();
for(int j=0;j<len1;j++)
{
char a=arr[i].charAt(j);
if(a!=' ')
{
if(a>='y'||a>='Y')
{
w=w+(char)((int)a-25+1);
}
else
w=w+(char)((int)a+2);
}
else
{
s=s+w+" ";
w="";
}
}
arr[i]=s;
}
}
for(int i=0;i<len;i++)
{
System.out.println(arr[i]);
}}
}
}
ALGORITHM
Define the main Method with an Integer
Parameter n.
The method takes an integer n, representing the
number of strings to be processed.
Check the Validity of n.
If n is between 2 and 9, continue with the program.
If n is outside this range, display "Condition not
satisfied" and exit.
Initialize String Array.
Declare a string array arr of size n to store the
input strings.
Create a Scanner object for reading input from the
user.
Input Strings from the User.
Prompt the user to enter n strings and store each
in the array arr.
Process Each String in the Array.
For odd-indexed strings (index 1, 3, 5, etc.):
o Reverse the order of words in the string.
o Steps:
Append a space to the end of the string to
ensure that the last word is captured.
Initialize two empty strings: w for building
words and s for constructing the reversed
sentence.
For each character in the string:
If the character is not a space, add it
to w to form a word.
If a space is encountered, prepend w
to s and reset w.
Assign s (now containing reversed words)
back to arr[i].
For even-indexed strings (index 0, 2, 4, etc.):
o Shift each character in the string by 2 ASCII
values to encode it.
o Steps:
Append a space to the end of the string
for processing the last word.
Initialize w for constructing shifted words
and s for the final result.
For each character in the string:
If it's not a space, shift the character
by 2 ASCII values and add it to w.
When a space is reached, append w
to s and reset w.
Assign s (with encoded characters) back
to arr[i].
Display the Transformed Array.
Print each transformed string from arr to show the
results.
OUTPUT-
QUESTION 14-
PROGRAM 14-
import java.util.*;
class PrintJob
{
int job[],newJob,Capacity,Front,Rear;
PrintJob()
{
Capacity=20;
Front=-1;
Rear=-1;
newJob=0;
}
void createJob()
{
job=new int[Capacity];
}
void addjob(int n)
{
newJob=n;
if(Rear==Capacity-1)
{
System.out.println("Printjob is full, cannot add
any more");
}
else
{
if(Front==-1)
{
Front=0;Rear=0;
job[Rear]=newJob;
System.out.println("Added job: " + newJob);
}
else
{
job[++Rear]=newJob;
System.out.println("Added job: " + newJob);
}
}
}
void removeJob()
{
if(Rear==-1&&Front==-1||Front>Rear)
{
System.out.println("PrintJob is empty");
}
else
{
if(Rear==Front)
{
Rear=0;
Front=0;
}
else
{
System.out.println("Removed job: " +
job[Front]);
Front++;
}
}
}
void main()
{
Scanner sc=new Scanner(System.in);
PrintJob obj=new PrintJob();
System.out.println("no. of jobs to added(1-20)");
int n=sc.nextInt();
obj.createJob();
System.out.println("input job");
for(int i=0;i<n;i++)
{
obj.addjob(sc.nextInt());
}
System.out.println("No. of jobs to be removed");
int n1=sc.nextInt();
for(int i=1;i<=n;i++)
obj.removeJob();
}
}
ALGORITHM-
Initialize Queue:
Set Capacity to 20.
Initialize Front and Rear to -1 (indicating an empty
queue).
Create an integer array job[] of size Capacity to
store job IDs.
Add Job (addjob(int n)):
Input: n (job ID to be added).
If Rear is equal to Capacity - 1, display "Printjob is
full, cannot add any more".
Otherwise:
o If Front is -1, set Front and Rear to 0 (indicating
the first job being added).
o Set job[Rear] to newJob.
o If Front is already initialized, increment Rear
and add the new job ID at job[Rear].
o Display "Added job: newJob".
Remove Job (removeJob()):
Check if the queue is empty (Rear == -1 && Front
== -1 || Front > Rear):
o If empty, display "PrintJob is empty".
Otherwise:
o Display "Removed job: job[Front]".
o If Rear == Front, reset Front and Rear to 0.
o Otherwise, increment Front to remove the job
at the front of the queue.
Main Function (main()):
Input the number of jobs to be added (n).
Call createJob() to initialize the job array.
For each job:
o Input the job ID and call addjob() to add it to
the queue.
Input the number of jobs to be removed (n1).
For each job removal:
o Call removeJob() to remove it from the queue.
OUTPUT-
QUESTION 15
PROGRAM 15
import java.util.*;
class Recursivepower
{
int power,k,l;
Recursivepower()
{
power=0;
k=0;
l=0;
}
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("input the values of k and l");
k=sc.nextInt();
l=sc.nextInt();
}
int powerof(int k,int l)
{
if(l==0)
return 1;
return k*powerof(k,l-1);
}
void setpower()
{
power=powerof(k,l);
}
void display()
{
System.out.println(power);
}
void main()
{
Recursivepower obj=new Recursivepower();
obj.input();
obj.setpower();
obj.display();
}
}
ALGORITHM
Initialize Variables:
power to store the result of klk^lkl, initially set to
0.
k and l to store the base and exponent,
respectively, both initialized to 0.
Input Function (input()):
Prompt the user to input values for k (base) and l
(exponent).
Store the inputs in the variables k and l.
Recursive Power Calculation (powerof(int k,
int l)):
Base Case: If l (exponent) is 0, return 1 (as any
number raised to the power of 0 is 1).
Recursive Step: Return k * powerof(k, l - 1), which
multiplies k by the result of the function called with
l - 1.
Set Power (setpower()):
Call the powerof() function with k and l to calculate
k^l.
Store the result in the variable power.
Display Result (display()):
Print the value of power.
Main Function (main()):
Create an instance of Recursivepower.
Call the input() method to read values for k and l.
Call setpower() to calculate klk^lkl.
Call display() to show the result.
OUTPUT-
QUESTION 16-
PROGRAM 16-
import java.util.*;
class PrintPrime
{
String str;
int c;
PrintPrime()
{
str="";
c=0;
}
void getdata()
{
Scanner sc=new Scanner(System.in);
System.out.println("input");
str=sc.next();
c=str.length();
}
void printp(int p,int q)
{
if(p>=q)
return;
if(prime(p)==true)
{
System.out.print(str.charAt(p));
}
printp(p+1,q);
}
boolean prime(int n)
{
int a=n,count=0;
for(int i=1;i<=a;i++)
{
if(a%i==0)
{
count++;
}
}
if(count==2)
{
return true;
}
return false;
}
void main()
{
PrintPrime obj=new PrintPrime();
obj.getdata();
obj.printp(0,obj.c);
}
}
ALGORITHM-
Initialize Variables:
str (String) to store the user input.
c (integer) to store the length of the string.
Constructor (PrintPrime()):
Initializes str to an empty string and c to 0.
Input Function (getdata()):
Prompt the user to input a string.
Assign the input to str.
Set c to the length of str.
Recursive Function to Print Characters at Prime
Indices (printp(int p, int q)):
Base Case: If p >= q, terminate the recursion.
Prime Check: If the position p is a prime number
(verified by calling prime(p)), print the character at
index p in str.
Recursive Step: Call printp(p + 1, q) to continue
checking the next position.
Prime Checker (prime(int n)):
Initialize count to 0.
Loop from 1 to n:
o Increment count each time n is divisible by i.
If count equals 2 (indicating that n has exactly two
divisors, 1 and itself), return true (indicating that n
is prime).
Otherwise, return false.
Main Function (main()):
Create an instance of PrintPrime.
Call getdata() to input the string.
Call printp(0, c) to recursively print characters at
prime indices.
OUTPUT-
QUESTION 17-
PROGRAM 17-
import java.util.*;
class Binary
{
int a[],n,l,u;
Binary(int num)
{
n=num;
a=new int[n];
l=0;
u=num-1;
}
void readdata()
{
Scanner sc=new Scanner(System.in);
System.out.println("input");
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
}
int binary_search(int v)
{
if(l>u)
return -1;
int mid=(l+u)/2;
if(a[mid]==v)
return mid;
else if(a[mid]<v)
{
l=mid+1;
return binary_search(v);
}
else if(a[mid]>v)
{
u=mid-1;
return binary_search(v);
}
return -1;
}
void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("input the size of array");
int s=sc.nextInt();
Binary obj=new Binary(s);
obj.readdata();
System.out.println("input the number to be
found");
int b=sc.nextInt();
int find=obj.binary_search(b);
if(find==-1)
{
System.out.println("not found");
}
else
{
System.out.println("found"+find);
}
}
}
ALGORITHM
Initialize Variables:
a[]: An integer array to store the sorted elements.
n: The size of the array.
l: The lower bound for binary search (initialized to
0).
u: The upper bound for binary search (initialized to
n-1).
Constructor (Binary(int num)):
Takes num (size of the array) as input and
initializes n, a, l, and u.
Input Array (readdata()):
Prompt the user to input elements into the array
a[].
Read and store each integer element in a[].
Recursive Binary Search (binary_search(int v)):
Base Case: If l > u, the search space is exhausted,
so return -1 to indicate the element is not found.
Calculate mid as the middle index of the current
range ((l + u) / 2).
Comparison:
o If a[mid] == v, return mid as the index where
v is found.
o If a[mid] < v, set l = mid + 1 and recursively
call binary_search(v) to search in the right half.
o If a[mid] > v, set u = mid - 1 and recursively
call binary_search(v) to search in the left half.
Main Function (main()):
Prompt the user to enter the size of the array and
create an instance of Binary.
Call readdata() to input the array elements.
Prompt the user to input the element v to search
for.
Call binary_search(v) and store the result in find.
Output:
o If find == -1, print "not found."
o Otherwise, print "found" along with the index
find.
OUTPUT-
QUESTION 18-
PROGRAM 18-
import java.util.*;
class Trr
{
int arr[][]=new int[3][3];
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("input");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
arr[i][j]=sc.nextInt();
}
}
}
Trr transpose()
{
Trr n=new Trr();
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
n.arr[i][j]=arr[j][i];
}
}
return n;
}
Trr product(Trr M)
{
Trr nn=new Trr();
int temp=0;
for(int i=0;i<3;i++)
{
for(int k=0;k<3;k++){
temp=0;
for(int j=0;j<3;j++)
{
temp=arr[i][j]*M.arr[j][k];
nn.arr[i][k]+=temp;
}}
}
return nn;
}
void display()
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(arr[i][j]+"\t");
}
System.out.println();
}
}
void main()
{
Trr A=new Trr();
Trr B=new Trr();
Trr c=new Trr();
A.input();
B=A.transpose();
Trr C=A.product(B);
C.display();
}
}
ALGORITHM-
Initialize Matrix (arr[][]):
o arr[][]: A 3x3 integer matrix for storing values.
Input Matrix (input()):
o Prompt the user to input values for the 3x3
matrix arr[][].
o Use nested loops to store each input in the
appropriate position in arr[][].
Matrix Transpose (transpose()):
o Create a new Trr object n to hold the
transposed matrix.
o Use nested loops to set n.arr[i][j] = arr[j][i] for
each element, effectively transposing the
original matrix.
o Return the transposed matrix n.
Matrix Multiplication (product(Trr M)):
o Create a new Trr object nn to store the product
of the two matrices.
o For each element in nn (using i and k for rows
and columns):
Initialize temp to zero.
Use an inner loop to iterate through
elements in the row of the first matrix
(arr[i][j]) and the column of the second
matrix (M.arr[j][k]), calculating the dot
product.
Add the result of temp to nn.arr[i][k].
o Return nn as the product matrix.
Display Matrix (display()):
o Use nested loops to print each element in arr[]
[], formatted as a 3x3 matrix.
Main Function (main()):
o Create three Trr objects, A, B, and C.
o Call A.input() to input values into the matrix A.
o Assign B as the transpose of A by calling
A.transpose().
o Assign C as the product of A and B by calling
A.product(B).
o Display the resulting matrix C by calling
C.display().
OUTPUT-
QUESTION 19-
PROGRAM 19-
import java.util.*;
class prg9
{
void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("input");
String hint=sc.next();
String w="",s="";
int len=hint.length()-1;
for(int i=len;i>=0;i--)
{
char a=hint.charAt(i);
w=w+a;
int b=Integer.parseInt(w);
if(b/1000>9)
{
System.out.println(“invalid”);
S=””;
Break;
}
char c=(char)b;
if(c>='A'&&c<='Z'||(c>='a'&&c<='z')||
c==' '){
s=s+c;w="";}
}
System.out.println(s);
}
}
ALGORITHM-
Initialize Variables:
hint: The input numeric string representing ASCII
codes of characters.
w: An empty string used to build potential ASCII
codes from hint.
s: An empty string to store the decoded characters.
len: The length of hint minus one (to start
processing from the last character).
Input Numeric String (hint):
Prompt the user to enter the numeric string hint.
Decode Process:
Loop from the end of hint to the beginning:
o For each character in hint (starting from the
end), add it to w.
o Convert w to an integer b, which represents a
potential ASCII value.
o Convert b to a character c.
o Check if c is an alphabetic character (A-Z or a-
z) or a space.
If true, add c to s and reset w to an empty
string (indicating a valid ASCII character
was found).
Repeat until the entire string is processed.
Output the Decoded String:
Print the decoded string s, which contains the
characters represented by ASCII codes in hint.
OUTPUT-
QUESTION 20-
PROGRAM 20-
import java.util.*;
class prg10
{
int tomin(String s)
{
int
hr=Integer.parseInt(s.substring(0,s.indexOf(":")));
int
min=Integer.parseInt(s.substring(s.indexOf(":")
+1,s.indexOf(" ")));
if(s.substring(s.indexOf(" ")
+1).equalsIgnoreCase("PM")==true)
{
hr+=12;
}
return hr*60+min;
}
void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("input login time");
String logintime=sc.nextLine();
System.out.println("input logout time");
String logoutime=sc.nextLine();
int loginminute=tomin(logintime);
int logoutminute=tomin(logoutime);
int totalmin=logoutminute-loginminute;
System.out.println(logoutminute);
int thr=(int)(totalmin/60);
int tmin=totalmin%60;
System.out.println("Time Spent-- "+thr+" hr
"+tmin+"min ");
System.out.println("Total amount "+(double)
(totalmin*0.16));
}
}
ALGORITHM-
Method tomin(String s):
Input: A string s representing time in "hh
AM/PM" format.
Output: The total time in minutes.
Steps:
o Extract the hour part by parsing the substring
from the start to the colon (":").
o Extract the minute part by parsing the
substring between the colon and the space.
o Check if the time is in PM:
If it is PM, add 12 to the hour (to convert
to 24-hour format).
o Convert the total time into minutes by
calculating: hr * 60 + min.
o Return the total minutes.
Main Method:
Input: Prompt the user to enter login time and
logout time as strings.
Steps:
o Call the tomin method to convert both login
time and logout time into minutes.
o Calculate the total time spent by subtracting
login minutes from logout minutes.
o Convert the total minutes into hours and
remaining minutes:
Calculate thr (total hours) as totalmin / 60.
Calculate tmin (remaining minutes) as
totalmin % 60.
o Print the total time spent in hours and
minutes.
o Calculate the total amount charged, where the
rate is 0.16 per minute:
Print the total amount as totalmin * 0.16.
OUTPUT-