You are on page 1of 118

Question 1:

Write a program to input a 2-D array and print it in E form.(5x5)

Algorithm :
Step 1: Start
Step 2: Input a number from the user.
Step 3: Pass the number to the function isFascinating.
Step 4: Multiply the number by 1, 2 and 3 and store them in three different variables.
Step 5: Concatenate the variables in a String variable.
Step 6: Loop Starts.
Step 7: The "i" loop takes the number at index number 'i' and stores in variable 'c'. The
"j" loop starts from index number 'i+1' and checks whether the variable c is repeated in
the remaining string or not.
Step 8: If the variable 'c' becomes equal to any other character in the remaing string
then the counter variable 'count' increments by 1.
Step 9: If count >1 then the 'i loop' breaks and the compiler comes out of the i loop.
Step 10: Then the compiler checks whter if i==num.length() or i<num.length().
Step 11: If i==num.length() then it means that there was no repeated words and the
number becomes a "Fascinating Number".
Step 12: End

Programming Code:
import java.util.*;
class E-form

{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int a[][]=new int[5][5];
System.out.pritln(Enter Array Containing 25 elements);
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println(Original Matrix);
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
System.out.print(a[i][j]+" ");
System.out.println( );
}
System.out.println( );

System.out.println(Array in E form is :);


for(i=0;i<5;i++)
{
For(j=0;j<5;j++)

{
If(i%2==0)
System.out.print(a[i][j]);
}
System.out.println();
}
}
}

Question 20:
Write a program to accept a string and print the string in reverse order.
Sample Input: wood
Sample Output: doow

Algorithm:
Step 1: Start
Step 2: Input a string by the user.
Step 3: Append a blank character at the end of the string.
Step 4: Calculate the length of the string. i.e. x=st1.length()
Step 5: Start a reverse loop from the length of the string to 0. i.e. for(y=--x;y>=0;y--)
Step 6: Find the character at each yth position inside the loop and concatenate it in a
variable initialized to null initally.
Step 7: When the loop ends the concatenated variable will represent the reverse string.
Step 8: Print the reverse string.
Step 9: End

Programming Code:
import java.io.*;
public class StringReverse
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);

System.out.println("Enter a String");
s=in.readLine();
String w="",maxw="";int max=0,le=0,co=0;
for(i=0;i<s.length();i++)
{
char c=s.charAt(i);
if(c!=' ')
{
w+=c;
}
else
{
co=0;
for(j=0;j<w.length();j++)
{
char ch=w.charAt(j);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
{
co++;
}
}
if(co>max)
{
maxw=w;
max=co;
}
}
}
System.out.println("Word with maximum number of vowels is "+maxw);
}
}

Question 17:

Write a program to input m words in a S.D.A and print only those words that either start
with a vowel or end with a.

Algorithm:
Step 1: Start
Step 2: Input any string by the user.
Step 3: Convert the input string into lower case using toLowerCase() method.
s=s.toLowerCase();

i.e.

Step 4: Calculate the length of the string


.
Step 5: Start a loop from character a to z. i.e. for(char i='a'; i<='z'; i++)
Step 6: Initialize a counter variable count=0.
Step 7: Start a loop from 0 to length of the input string. i.e. for(int j=0; j<l; j++)
Step 8: Find character at each position of the string using charAt() method. i.e.
ch=s.charAt(j)
Step 9: If the character extracted in step 8 is same that one in the outer loop then
increment the counter variable by 1. i.e. if(ch==i) count++;
Step 10: If the count is not equal to 0 then print the character and the count.
Step 11: At the end of the loop all characters in the string and their frequency will be
printed.
Step 12: End

Programming Code:
import java.io.*;

class AlphabetFreq
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter any string: ");
String s = br.readLine();
int l=s.length();
String w="";
for(i=0;i<m;i++)
{
String s=a[i];
char c=s.charAt(0);
char ch=s.charAt(s.length()-1);
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||ch=='a')
w=w+s+" ";
}
System.out.println("All words either starting from vowels or ending with 'a' are ");
System.out.println(w);
}
}

Question 12:
Write a program to declare a square matrix A[][] of order (M x M) where M must be greater
than 3 and less than 10. Allow the user to input positive integers into this matrix. Perform
the following tasks on the matrix:
(a) Sort the non-boundary elements in ascending order using any standard sorting
technique
and
rearrange
them
in
the
matrix.
(b)
Calculate
the
sum
of
both
the
diagonals.
(c) Display the original matrix, rearranged matrix and only the diagonal elements of the
rearranged matrix with their sum.
Algorithm :

Programming Code :
import java.util.*;
class NonBoundrySorting
{
int A[][],B[],m,n;

void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the square matrix : ");
m=sc.nextInt();
if(m<4 || m>10)
System.out.println("Invalid Range");
else
{
A = new int[m][m];
n = (m-2)*(m-2);
B = new int[n];

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


for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print("Enter a value : ");
A[i][j]=sc.nextInt();

}
}
}
}

void convert(int s)
{
int x=0;
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
if(i != 0 && j != 0 && i != m-1 && j != m-1)
{
if(s==1)
B[x] = A[i][j];
else
A[i][j] = B[x];
x++;
}
}
}
}

void sortArray()
{

int c = 0;
for(int i=0; i<n-1; i++)
{
for(int j=i+1; j<n; j++)
{
if(B[i]>B[j])
{
c = B[i];
B[i] = B[j];
B[j] = c;
}
}
}
}

void printArray()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
}

void printDiagonal()
{
int sum = 0;
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
if(i==j || (i+j)==m-1)
{
System.out.print(A[i][j]+"\t");
sum = sum + A[i][j];
}
else
System.out.print("\t");
}
System.out.println();
}
System.out.println("Sum of the Diagonal Elements : "+sum);
}

public static void main(String args[])


{
NonBoundrySorting ob = new NonBoundrySorting();
ob.input();
System.out.println("*********************");

System.out.println("The original matrix:");


System.out.println("*********************");
ob.printArray();
ob.convert(1);
ob.sortArray();
ob.convert(2);

System.out.println("*********************");
System.out.println("The Rearranged matrix:");
System.out.println("*********************");
ob.printArray();
System.out.println("*********************");
System.out.println("The Diagonal Elements:");
System.out.println("*********************");
ob.printDiagonal();
}
}
import java.util.*;
class VowelChar
{
public static void main(String arg[])
{
char a[ ][ ];
int i, j, m;
Scanner ob=new Scanner(System.in);
System.out.println("Enter size of an Array from 2 to 20 ");

m=ob.nextInt( );
if(m>1 && m<=20)
{
a=new char[m][m];
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{

a[i][j]=ob.nextLine().charAt(0 );
}
}
System.out.println("Original Matrix is ");
System.out.println( );
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
System.out.print(a[i][j]+" ");
System.out.println( );
}
System.out.println( );
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
char c=a[i][j];

if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
{
a[i][j]='*';
}
}
}
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}

Question 11:
Write a program to generate first 20 terms of the Fibonacci series in descending order in a
S.D.A of size 20. Store the numbers in another array which are primes.

Algorithm :
Step 1: Start

Step 2: Input a number from the user in the variable num.


Step 3: Copy the value of num into a variable z.
Step 4: Pass the value of num to a function reverse().
Step 5: Loop starts.
Step 6: Variable q extracts every last digit of variable q.
Step 7: Variable sum stores the digits in reverse order.
Step 8: The function returns the value of sum.
Step 9: In main method the compiler checks whether or not the value of sum is equal to the original
or not.
Step 10: If the condition is true then the number is Palindrome Number otherwise not.
Step 11: End.

Programming Code:
import java.util.*;
class Fibonacci
{
public static void main(String arg[])
{
int a[20];
int i, j, m;
Scanner ob=new Scanner(System.in);

for(j=0;j<m;j++)
System.out.print(a[i][j]+" ");
System.out.println( );
}
System.out.println( );

a[19]=1;a[18]=1;
int b[]=new int[20];
for(i=19;i>=2;i--)
{
a[i-2]=a[i]+a[i-1];
}
System.out.println("Fibonacci Series in descending order is :");
for(i=0;i<19;i++)
System.out.print(a[i]+" ");
for(i=19;i>=0;i--)
{
int t=a[i];
for(j=2;j<t;j++)
{
if(t%j==0)
{
co++;
}
}
if(co==0)
{
b[k]=a[i];
k++;
}
}
for(i=0;i<k;i++)

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

}
}

write a program to input a 2d


matrix and print it in T form
Programming Code:
import java.util.*;
class T-form
{
public static void main(String arg[])
{
int a[ ][ ];
int i, j, m;
Scanner ob=new Scanner(System.in);
System.out.println("Enter size of an Array from 2 to 20 that are odd ");
m=ob.nextInt( );
if(m>1 && m<=20)
{
a=new int[m][m];
for(i=0;i<m;i++)

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

a[i][j]=ob.nextInt( );
}
}
System.out.println("Original Matrix is ");
System.out.println( );
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
System.out.print(a[i][j]+" ");
System.out.println( );
}
System.out.println( );
System.out.println(Array in T form is :);
for(i=0;i<n;i++)
{
System.out.print(a[0][n]);
}
for(i=1;i<n;i++)
{
for(j=0;j<n;j++)
{
if(j==(n-1)/2)

System.out.println(a[i][j]);
}
}

Question 15:
Algorithm :
mahendra singh dhoni
dhoni mahendra sing

Programming Code:
import java.io.*;
class ThreeWords
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter any word : ");
String s = br.readLine();
int l = s.length();
int la=s.lastIndexOf(" ");
s1=s.substring(la);
s2=s.substring(0,la);
s3=s3+s1+" "+s2;
}
}

Question 21:
Write a program to input a Sentence from the user and print the number of 2 letters 3
letters and 4 letters words.

Algorithm :

Programming Code:
import java.util.*;
public class Replace
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter A Sentence ");
String s=sc.nextLine();

String w="",maxw="";int max=0,le=0,l1=0,l2=0,l3=0;


for(i=0;i<s.length();i++)
{
char c=s.charAt(i);

if(c!=' ')
{
w+=c;
}
else
{
int le=w.length();
if(le==2)
l1++;
if(le==3)
l2++;
if(le==4)
l3++;
}
{
maxw=w;
max=le;
}
}
}

System.out.println("Number of 2 letter words are "+l1);


System.out.println("Number of 3 letter words are "+l2);
System.out.println("Number of 4 letter words are "+l3);
}
}

Program 4:- Write a program to print the magic matrix.


Algorithm-

Step1 :- Start
Step2 :- input a limit n and a 2-d array a[][] of dimension n*n.
Step3 :- initialize r, c, x ,c1, r1 with 0, n/2, 1 respectively.
Step4 :- take a while loop of x(= 1) till it is less than equals to n*n.
Step5 :- a[r][c]=x++ , r1= r , c1= c,r-- , c++.
Step6 :- if (r<0) then r=n-1.
Step7 :- if(c==n) then c=0.
Step8 :- if (a[r][c]!=0) then r=r1 , c=c1, r++.

Step9 :- print array a[][] which is now the required magical matrix.
Step10 :- STOP
class magic
{
public static void main(int n)
{
int i,j,c=0,r=0,c1,r1;
int a[][]=new int[n][n];
c=n/2;
int d=1;
int s=n*n;
while(d!=(s+1))
{
a[r][c]=d;
c1=c;
r1=r;
if(r==0)
r=n;
if(c==0)
c=n;
r--;
c--;
d++;
if(a[r][c]!=0)
{
r=r1;

c=c1;
r++;
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
System.out.print(a[i][j]+"\t");
System.out.println();
}
}
}

Input:N=3

Output:-

Program 5-Write a program to display Pascal's Triangle .

Algorithm :
Step 1 - start
step 2 - pas[0] = 1
step 3 - if i=0 then goto step 4
step 4 - if j=0 then goto step 5
Step 5 - print pas[j]+" "
Step 6 - i++& if i<=i goto step 5
Step 8 - if j=i+1 then goto step 7
step 9 - pas[j]=pas[j]+pas[j-1]
Step 10 - j--& if j>0 goto step 9
Step 11 end

import java.util.*;
class Pascal
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
int [ ] pas = new int [n+1];
pas[0] = 1;
for (int i=0; i<n; i++)
{
for (int j=0; j<=i; ++j)
System.out.print(pas[j]+" ");
System.out.println( );
for (int j=i+1; j>0; j--)
pas[j]=pas[j]+pas[j-1];
}
}
}

Program 6-Write a program to input a 2-D array and find


the sum of each column.
Algorithm :
Step 1 - start
Step 2 - input a[]
Step 3 - from x =0 to x<4 repeat step 4
Step 4 - from y =0 to y<4 repeat step 5
Step 5 - print (a[x][y]+" "
Step 6 - from x =0 to x<4 repeat step 7 , step 9 and step 10
Step 7 - from y =0 to y<4 repeat step 8
Step 8 - sum=sum+a[x][y] ,
Step 9 - print sum

Step 10 - sum = 0
Step11 end
import java.util.*;
class ColoumnSum
{
public static void main(String args[])
{
int a[][]=new int[4][4];
Scanner sc=new Scanner(System.in);
int x,y,z,Sum=0;
System.out.println("Enter the array");
for(x=0;x<4;x++)
{
for(y=0;y<4;y++)
{
a[x][y]=sc.nextInt();
}
}
System.out.println("Array");
for(x=0;x<4;x++)
{
for(y=0;y<4;y++)
{
System.out.print(a[x][y]+" ");
}
System.out.println();

}
for(y=0;y<4;y++)
{
for(x=0;x<4;x++)
{
Sum=Sum+a[x][y];
}
System.out.println("Sum of Column "+(y+1)+" is "+Sum);
Sum=0;
}
}
}

Program 7-Write a program to input a 2-D array and find


the sum of diagonal.
Algorithm :
Step 1- start
Step 2- input a[]
Step 3- from x =0 to x<4 repeat step 4
Step 4- from y =0 to y<4 repeat step 5
Step 5- print (a[x][y]+" "
Step 6- from x =0 to x<4 repeat step 7
Step 7 - sum=sum+a[x][y] , y=y+1
Step 8 - from x =0 to x<4 repeat step 9

Step 9 - from y =0 to y<4 repeat step 10


Step 10 - check if((x+y)==3)
Step 11 - sum+=a[x][y]
Step 9- print sum and sum
Step 10 - sum = 0
Step11- end
import java.util.*;
class DiagonalSum
{
public static void main(String args[])
{
int a[][]=new int[4][4];
Scanner sc=new Scanner(System.in);
int x,y,z,Sum=0,sum=0;
System.out.println("Enter the array");
for(x=0;x<4;x++)
{
for(y=0;y<4;y++)
{
a[x][y]=sc.nextInt();
}
}
System.out.println("Array -");
for(x=0;x<4;x++)
{
for(y=0;y<4;y++)

{
System.out.print(a[x][y]+" ");
}
System.out.println();
}
y=0;
for(x=0;x<4;x++)
{
Sum=Sum+a[x][y];
y=y+1;
}
for(x=0;x<4;x++)
{
for(y=0;y<4;y++)
{
if((x+y)==3)
{
sum+=a[x][y];
}
}
}
System.out.println("Sum of Right diagonal is "+Sum);
System.out.println("Sum of Left diagonal is "+sum);
Sum=0;
}
}

Program 8-Write a program to declare a matrix A[ ][ ] of


order (MxN) where M is the number of rows and N is
the number of columns such that both M and N must be
greater than 2 and less than 20. Allow the user to input
integers into this matrix. Perform the following tasks on
the matrix:
a) Display the input matrix.
b) Find the maximum and minimum value in the matrix and
display them along with their positions.
c) Sort the elements of the matrix in ascending order
using any standard sorting technique and rearrange
them in the matrix.
d) Output the rearranged matrix.
Algorithm :
Step 1 - accept the dimension of the array and check if it is in the range.
Step 2 - if dimension satisfies the range, create an array of size mxn and accept the
elements.
Step 3 - add the elements of the two dimensional array to a single dimensional
array.
Step 4 - sort the elements of the 1-d array in ascending order using bubble sort
technique.
Step 5 - find the first element of the sorted array and find its position in the 2-d
array. This is our smallest element.
Step 6 - find the last element of the sorted array and find its position in the 2-d
array. This is our largest element.
Step 7 - print the sorted array in a 2x2 matrix
import java.util.*;
class Matrix_Sort
{
public static void main(String args[])
{

Scanner sc=new Scanner(System.in);


System.out.println("Enter the dimensions of the matrix");
int M=sc.nextInt();
int N=sc.nextInt();
if(M<=2||M>=20||N<=2||M>=20)
System.out.println("SIZE OUT OF RANGE");
else
{
int A[][]=new int[M][N];
System.out.println("Enter the elements of matrix");
int i,j;
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
A[i][j]=sc.nextInt();
}
}
System.out.println("ORIGINAL MATRIX:");
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
int a[]=new int[M*N];
int k=0,tmp;
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
a[k]=A[i][j];
k++;
}
}
for(i=0;i<k;i++)
{
for(j=0;j<k-1-i;j++)
{
if(a[j]>a[j+1])

{
tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
}
}
}
int max=a[k-1], min=a[0];
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
if(A[i][j]==max)
{
System.out.println("LARGEST NUMBER\t:"+max);
System.out.println("\t\tROW= "+i+"\n\t\tCOLUMN= "+j);
}
if(A[i][j]==min)
{
System.out.println("SMALLEST NUMBER\t:"+A[0][0]);
System.out.println("\t\tROW= "+i+"\n\t\tCOLUMN= "+j);
}
}
}
System.out.println("REARRANGED MATRIX");
k=0;
for(i=0;i<M;i++)
{
for(j=0;j<N;j++)
{
System.out.print(a[k]+"\t");
k++;
}
System.out.println();
}
}
}

Program 9-Write a program to create a m x n matrix and


print the prime elements in it along with the row and
column in which they are present.
Algorithm :
Step-1: input the number of rows and columns as m and n
Step-2: create a m x n matrix
Step-3: enter m x n elements in the matrix
Step-4: run a loop for the rows m
Step-5: run a loop for the columns n
Step-6: print original matrix and run a loop to find the number of factors of the
matrix element
Step-7: if the number of factors is 2 print the element, its row index and column
index
Step-8: continue process for all the elements
Step-9: end
import java.util.*;
public class searchPrime
{
public static void main(String args[])throws InputMismatchException
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of rows and columns of a matrix: ");
int m=sc.nextInt();
int n=sc.nextInt();
int a[][]=new int[m][n];
int i,j,k,s=0;
System.out.println("Enter "+(m*n)+" elements:");

for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("Original Matrix");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
System.out.println("Prime Element\t Row\t Column");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
int c=0;
for(k=1;k<=a[i][j];k++)
{
if(a[i][j]%k==0)
{

c++;
}
}
if(c==2){
System.out.println(a[i][j]+"\t\t"+i+"\t\t"+j);
}
}
}
}
}

Program 10-Write a program to sort the border elements of a n x


n matrix in ascending order.
Algorithm :
Step-1: input the number of rows for a square matrix
Step-2: create an n x n matrix and enter elements in it
Step-3: run two loops to access the matrix
Step-4: check for border elements
Step-5: if found, store them in a single dimension array, say b[]
Step-6: continue the process till all the border elements are stored in b[]
Step-7: now, sort the array b[] in ascending order using any sorting technique
Step-8: to store the sorted border elements in the matrix, run a loop for the
First row and store elements from b[] in the matrix
Step-9: run a loop for last column and do the same
Step-10: run a loop for last row and then first column to store the elements from b[]
to matrix
Step-11: finally, print the matrix that now has border elements sorted in ascending
order.
Step-12: end
import java.util.*;
public class BoundrySorting
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of rows for a sqaure matrix: ");
int n=sc.nextInt();

int a[][]=new int[n][n];


int i,j,s=0;
System.out.println("Enter "+(n*n)+" elements:");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("Original Matrix");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
int b[]=new int[n*n];
int x=0;

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

if(i==0 || j==0 || i==n-1 || j==n-1)


{
b[x++]=a[i][j];
}
}
}

for(i=0;i<x;i++)
{
for(j=i+1;j<x;j++)
{
if(b[i]>b[j])
{
int t=b[i];
b[i]=b[j];
b[j]=t;
}
}
}

int c=0;
for(i=0;i<n;i++)
a[0][i]=b[c++];
for(i=1;i<n;i++)
a[i][n-1]=b[c++];
for(i=n-2;i>=0;i--)

a[n-1][i]=b[c++];
for(i=n-2;i>0;i--)
a[i][0]=b[c++];
System.out.println("Sorted Array");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.print(a[i][j]+" ") ;
}
System.out.println();
}
}
}

Program 13-Write a program to input a 2-D array and find


whether it is a Lower Triangular Matrix or not.
Lower Triangular matrix is a square matrix in which all the entries
above the main diagonal () are zero. The entries below or on the main
diagonal themselves may or may not be zero.
Algorithm :
Step 1 - start
Step 2 - enter size of array in variable m
Step 3 - create array a[m][m]
Step 4 - input elements in array
Step 5 - print array
Step 6 - set p = 0
Step 7 - start loop from i = 0 , to i < m , repeat step 8
Step 8 - start loop from j = i+1 , to j < m , repeat step 9
Step 9 - check if ( a[i][j] !=0)
if true
set p = 1
break
Step 10 - check if ( p==0)
if true print " the matrix is lower triangular"
else print " the matrix is not lower triangular"
Step 11 - end
import java.util.*;
class LowerTriangularMatrix
{
public static void main(String args[])throws Exception
{

Scanner sc=new Scanner(System.in);


System.out.print("Enter the size of the matrix : ");
int m=sc.nextInt();
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();
}
}
System.out.println("The 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();
}
int p=0;
for(int i=0;i<m;i++)
{
for(int j=i+1;j<m;j++)
{

if(A[i][j]!=0)
{
p=1;
break;
}
}
}

if(p==0)
System.out.println("The matrix is Lower Triangular");
else
System.out.println("The matrix is not Lower Triangular");
}
}

Program 16-Write a program to store numbers in a m x n


matrix . Find the sum of elements in each row and each
column . Display the sum of each row and column
corresponding to the rows and column.
Algorithm :
Step 1 - start
Step 2 - enter size of array in variables m & n
Step 3 - create an array a[m][n]
Step 4 - input elements in array
Step 5 - print array
Step 6 - start loop from i = 0 , to i < m repeat step 7
Step 7 - set s = 0
start loop from j = 0 , to j < n repeat step 8
Step 8 - set s = s + a[i][j]
Step 9 - set a[m][i] = s
step 10 - start loop from i = 0 , to i < m repeat step 11
Step 11 - set s = 0
start loop from j = 0 , to j < n repeat step 12
Step 12 - set s = s + a[i][j]
Step 13 - set a[i][n] = s
Step 14 - print new array containing sum.
Step 15 - end.
import java.util.*;
public class sum
{

public static void main(String args[])


{
Scanner sc = new Scanner(System.in);
int n,m,i,j,s;
System.out.println("Enter the rows and column of the matrix");
m=sc.nextInt();
n=sc.nextInt();
int a[][]=new int[m+1][n+1];
System.out.println("Enter the Array");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
a[i][j]=sc.nextInt();
}
}
System.out.println("Array ");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
for(i=0;i<m;i++)

{
s=0;
for(j=0;j<n;j++)
s+=a[i][j];
a[m][i]=s;
}
for(i=0;i<m;i++)
{
s=0;
for(j=0;j<n;j++)
s+=a[i][j];
a[i][n]=s;
}
System.out.println(" Final Matrix with the sum of each row and column is below
:");
for(i=0;i<=m;i++)
{
for(j=0;j<=m;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
}

Program 17-Write a program to create a 4x4 matrix .


Now swap the elements of 1 st row and 4th row. Display
the result i.e., interchange the elements of the 1 st row
with 4th row.
Algorithm :
Step 1 - start
Step 2 - enter size of array in variables m & n
Step 3 - create an array a[m][n]
Step 4 - input elements in array
Step 5 - print array
Step 6 - start loop from j = 0 , to j < 4 , repeat step 7
Step 7 - set t = a[0][j]
set a[0][j] = a[3][j]
set a[3][j] = a[0][j]
Step 8 - print swapped matrix
import java.util.*;
public class Swapping
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a[][]=new int[4][4];
int i,j,t;
System.out.println("Enter Array");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)

{
a[i][j]=sc.nextInt();
}
}
System.out.println("Original Array");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
for(j=0;j<4;j++)
{
t=a[0][j];
a[0][j]=a[3][j];
a[3][j]=t;
}
System.out.println("New Array after Swapping ");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++) System.out.print(a[i][j]+" ");
System.out.println();
}
}

Program 18-Write a program to store numbers in a 4x4


matrix. Arrange the numbers of each row in ascending
order and display the result.
Algorithm :
Step 1 - start
Step 2 - create an array a[m][n]
Step 3 - input elements in array
Step 4 - print array
Step 5 - start loop from i = 0 , to i < 4 , repeat step6
Step 6 - start loop from j = 0 , to j < 4 , repeat step 7
Step 7 - start loop from k = 0 , to k < 3 - j repeat step 8
Step 8 - check if ( m[i][k] > m[i][k+1] )
if true
set t = m[i][k]
set m[i][k] = m[i][k+1]
set m[i][k+1] = t
Step 9 - print sorted array
Step 10 - end.

import java.util.*;
public class Ascending_Row
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int m[][]=new int[4][4];
int i,j,k,t;

System.out.println("Enter Array");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
m[i][j]=sc.nextInt();
}
}
System.out.println("Original MAtrix ");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print(m[i][j]+" ");
}
System.out.println();
}
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3-j;k++)
{
if(m[i][k]>m[i][k+1])
{
t=m[i][k];

m[i][k]=m[i][k+1];
m[i][k+1]=t;
}
}
}
}
System.out.println("The Array after Sorting each row ");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print(m[i][j]+" ");
}
System.out.println();
}
}
}

Program 24-Write a program to enter a sentence and a word to be


searched. The program displays the frequency of the searched
letter.
Sample Input : The quick brown fox jumped over the wall.
Enter Word to be Searched : the
Sample Output : 2 times

Algoriththm:
Step 1: Start
Step 2: Input a sentence from the user.
Step 3: Input the word to be searched from the user.
Step 4: Calculate the length of the sentence.
Step 5: Start a loop from 0 to length of the sentence.
Step 6: Find each word of the sentence using indexOf and substring methods.
IndexOf method will calculate the position of the space i.e. int n=s.indexOf(' ',i) and
substring will get the word. i.e. String w=s.substring(i,n)
Step 7: Compare the word ignoring case to be searched with each word found in
step 6.
Step 8: If the word is equal to the word to be searched then increment a counter
that was initially initialized to 0.
Step 9: At the end of the loop the counter value will give the count of the occurance
of the word in the sentence.
Step 10: If the counter value is 0, then that means the word is not present in the
sentence.
Step 11: End.

import java.util.*;
public class Word_Frequency
{ public static void main(String args[]) {
int c=0;String wd=new String();
Scanner sc=new Scanner(System.in);
System.out.println("Enter A Sentence");
String s=sc.nextLine()+" ";
System.out.println();
System.out.println("Enter The Word To Be Searched");
wd=sc.next();
int len=s.length();
for(int i=0;i<len;i++)
{
int n=s.indexOf(' ',i);
String w=s.substring(i,n);
if(wd.equalsIgnoreCase(w)==true)
c++;
i=n;
}
if(c!=0)
System.out.println("The Word "+wd+" occurs in the Sentence "+c+" times");
else
System.out.println("Word Not Found");}}

Program 28-Write a program to input two numbers and display the


sum of first digit of the first number and the last digit of the second
number. Also find the sum of the central digit of both the numbers.
Algorithm
Step 1 Declare variables like n1,n2,s1,l1=0,l2=0,
Step 2 Declare two arrays a1[] and a2[].
Step 3 Input two numbers in n1,n2.
Step 4 Begin while loop till n1>0.

Step 5 Perform n1%10 and store it in a1[l1] and add 1 to l1.


Step 6 Begin while loop n2>o.
Step 7 Repeat step5 taking n2 array a2[]..
Step 8 Initialize s1 as s1=a1[0]+a2[l2-1] and print the sum of Ist digit of Ist no and
last digit of second no.
Step 9 Check if l1%2==0..
Step 10 If step9 is true then perform s2=0.5d*(a1[(l1/2)]+a1[(l1/2)-1]) else perform
s2=a1[l1/2];
Step 11 Repeat step10 with l2.
Step12 End.
import java.io.*;
public class num1
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n1,n2,l1=0,l2=0,s1;
int a1[]=new int[5],a2[]=new int[5];
double s2;
System.out.println("Enter two numbers:");
n1=Integer.parseInt(br.readLine());
n2=Integer.parseInt(br.readLine());
while(n1>0)
{
a1[l1]=(n1%10);
n1/=10;
l1++;

}
while(n2>0)
{

a2[l2]=(n2%10);
n2/=10;

l2++;
}
s1=a1[0]+a2[l2-1];
if(l1%2==0)
s2=0.5d*(a1[(l1/2)]+a1[(l1/2)-1]);
else
s2=a1[l1/2];
if(l2%2==0)
s2=s2+0.5d*(a2[(l2/2)]+a2[(l2/2)-1]);
else
s2=s2+a2[l2/2];
System.out.println("The sum of the digit of the first number and the last digit of the
second number="+s1);
System.out.println("The sum of the central digit of both the numbers="+s2);
}
}

Program 30-Write a program to input three binary


numbers and display the sum of them.

For eg-

1101+1001+1100=100010.

Algorithm.

Step1.Initialize array arr [] with 100.


Step 2.Declare a,b,c,i,s,s1,s2,d,d1,d2,c,c0,c1,c2,sum,k.
Step 3.Initilize s,s1,s2,d,d1,d2,sum,k with 0 and c0,c1,c2 with 1;
Step 4.Input three binary number from user.
Step 5.Begin loop for i from a to1 and repeat step 6 to 8.
Step 6.Assign value of i%10 to s.
Step 7.d=d+s*c0.
Step 8.Assign value of c0*2 to c0.
Step 9.Begin loop for i from b to1 and repeat step 10 to 12.
Step 10.Assign value of i%10 to s1.
Step 11.d1=d1+s1*c1.
Step 12.Assign value of c1*2 to c1.
Step 13.Begin loop for i from c to1and repeat step 14 to 16.
Step 14.Assign value of i%10 to s2.
Step 15.d2=d2+s2*c2.
Step 16.Assign value of c2*2 to c2.
Step 17.Add d,d1,d2 and store the value to sum.
Step 18Begin while loop for sum upto 0 and repeat step 19 to 21.
Step 19.Assign value of sum%2 to sum.
Step 20.Assign value of sum/2 to sum.
Step 21.k++.
import java.io.*;

class sumbinary
{
public static void main(String args[])throws Exception
{
BufferedReader num=new BufferedReader(new InputStreamReader(System.in));
int c,i,n1,n2,n3,sum1=0,sum2=0,sum3=0,p1=0,p2=0,p3=0,p,k=0,sum4;
int a[]=new int[10];

System.out.println("Enter three numbers");


n1=Integer.parseInt(num.readLine());
n2=Integer.parseInt(num.readLine());
n3=Integer.parseInt(num.readLine());

for(i=n1;i>0;i/=10)
{
c=i%10;
sum1=sum1+(int)Math.pow(2,p1)*c;
p1++;
}
for(i=n2;i>0;i/=10)
{
c=i%10;
sum2=sum2+(int)(Math.pow(2,p2)*c);
p2++;
}
for(i=n3;i>0;i/=10)

{
c=i%10;
sum3=sum3+(int)(Math.pow(2,p3)*c);
p3++;
}
sum4=sum1+sum2+sum3;
System.out.print(sum4);
while(sum4>0)
{
p=sum4%2;
a[k]=p;
k++;
sum4=sum4/2;
}
System.out.println("the added binary number is=");
for(i=k-1;i>=0;i--)
{
System.out.print(a[i]);}
}
}
}

Program 31-Write a program to find the no of days


between two dates.

Algorithm

Step 1 Declare variables like d1,d2,m1,m2,y1,y2,ny2=365,k,d=0.


Step 2 Enter number of days in each month in array date1[].
Step 3 Enter number of days in each month in array date2[].
Step 4 Input first date in d1,m1,y1.
Step 5 Input second date in d2,m2,y2.
Step 6 Check if y1%4=0 then initialize 29 to date1[2].
Step 7 Check if y2%4=0 then initialize 29 to date2[2] and 366 to ny2.
Step 8 Check if y1=y2 then repeat steps 9 to 13.
Step 9 Begin a loop for k from 1 to m1 by k++.
Step 10 Add d1 as d1=d1+date1[k].
Step 11 Begin a loop for k from 1 to m2 by k++.
Step 12 Add d1 as d2=d2+date2[k].
Step 13 Initialize d=d2-d1.
Step 14 If y1!=y2 then repeat steps 15 to 19.
Step 15 Begin a loop for k from 1 to m1 by k++.
Step 16 Add d1 as d1=d1+date1[k].
Step 17 Begin a loop for k from 1 to m2 by k++.
Step 18 Add d1 as d2=d2+date2[k].
Step 19 Initialize d2=d2+ny2.
Step 20 Initialize d=d2-d1.
Step 21 Print the difference of two dates as d.
Step 22 End of process.
import java.io.*;
class daysdates

{
public static void main(String args[])throws Exception
{
BufferedReader num=new BufferedReader(new InputStreamReader(System.in));

int dd1,mm1,yy1,dd2,mm2,yy2,p,i,sum=0,k,sum1=0,sum2=0,p1=0,k3,k2,c,k1;
int a[]={31,28,31,30,31,30,31,31,30,31,30,31};

System.out.println("enter the first date");


dd1=Integer.parseInt(num.readLine());
mm1=Integer.parseInt(num.readLine());
yy1=Integer.parseInt(num.readLine());
System.out.println("enter the second date");
dd2=Integer.parseInt(num.readLine());
mm2=Integer.parseInt(num.readLine());
yy2=Integer.parseInt(num.readLine());

if(yy1==yy2)
{
if(mm1!=mm2)
{
p=(a[mm1-1]-dd1)+dd2;

for(i=mm1;i<=mm2-2;i++)
{

sum=sum+a[i];

}
if(mm1<=2&&mm2>2&&yy1%4==0)
k=sum+p+1;
else
k=sum+p;
System.out.println("the no of days between the two dates="+k);
}
else
{

k=dd2-dd1;;

System.out.println("the no of days between two dates="+k);


}
}
if(yy1!=yy2)
{
for(i=mm1;i<12;i++)
{

p1=p1+a[i];

c=a[mm1-1]-dd1;
if(mm1<=2&&yy1%4==0)
k1=p1+c+1;
else
k1=p1+c;
for(i=yy1+1;i<=yy2-1;i++)
{
if(i%4!=0)
sum1=sum1+365;
else
sum1=sum1+366;

for(i=0;i<=mm2-2;i++)
{

sum2=sum2+a[i];

}
if(yy2%4==0&&yy2>2)
{

k2=sum2+dd2+1;
}

else
{
k2=sum2+dd2;
}
k3=k2+k1+sum1;
System.out.println("the no of days between two dates="+k3);
}
}

Program 33-Write a program to input n numbers in a array


and arrange in given manner without using another array.
Shift the largest number at the middle position (i.e N/2
index number) second largest before the middle and so
on.

Algorithm
Step1.Declare variables n,i,j,k,k1,t.
Step 2.Input size of array in n from user.
Step 3.Declare array a[] of size (n*2).
Step 4.Assign (n+n)-4 to k and (n+n)-2 to k1.
Step 5.Input n numbers from user.
Step 6.Begin loop for i from 0 to n-1 and repeat step 7 to 12.
Step 7.Begin loop for j from i+1 to n-1 and repeat step 8 to12.
Step 8.if(a[i]<a[j])
Step 9.Assign value of a[i] to t.
Step 10.Assign value of a[j] to a[i].
Step 11.Assign value of t to a[j].
Step 12.Assign 0 to t.
Step 13.Assign a[0] to a[(n+n)-3].
Step 14.Begin loop for i from 1 to n-1 and repeat step 15 to 20.
Step 15.if(i%2!=0)
Step 16.Assign a[i] to a[k].
Step 17.Assign k-1 to k.
Step 18.else if(i%2= =0)
Step 19.Assign a[i] to a[k1].
Step 20.Assign k1+1 to k1.
Step 21.Print elements of array a[] from n to (n+n)-1.
import java.io.*;
class arraypat
{

public static void main(String args[])throws Exception


{
BufferedReader num=new BufferedReader(new InputStreamReader(System.in));
int a[]=new int[20];
int n,x,y,z,p,temp;
System.out.println("enter the number of elements");
n=Integer.parseInt(num.readLine());
for(x=0;x<n;x++)
{
System.out.println("enter the element");
a[x]=Integer.parseInt(num.readLine());
}
p=(n/2);
n--;
for(x=0;x<p;x++)
{
for(y=x;y<=(n-x);y++)
{
for(z=y;z<=(n-x);z++)
{
if(a[y]>a[z])
{
temp=a[y];
a[y]=a[z];
a[z]=temp;
}

}
}
for(y=x+1;y<=(n-x);y++)
{
for(z=y;z<=(n-x);z++)
{
if(a[y]<a[z])
{
temp=a[y];
a[y]=a[z];
a[z]=temp;
}
}
}
}
if(n%2==0)
{
for(x=0;x<p;x++)
{
temp=a[x];
a[x]=a[n-x];
a[n-x]=temp;
}
}
System.out.println("the final array is");
for(x=0;x<=n;x++)

System.out.println(a[x]);}}}

Program 36-Write a program to generate a magical


matrix.A magical matrix is such that the sum of each
row,each column and each diagonal is always same.

Algorithm
Step1

Start.

Step2
Declare suitable variables like n,r=0,I,r1,c1,c and declare an 2-d
array of limit 10.
Step3

Take an odd limit in n and perform c=n/2.

Step4

Start a loop from i=1 to i<=n*n.

Step5

Strore a[r][c] in I and then put r1=r,c1=c.

Step6

Perform r-- and c++.

Step7

Check if r==-1 then put r=n-1.

Step8

Check if r>n-1 then put r=0.

Step9
Step10

Check if c>n-1 then put c=0.


Check if a[r][c]!=0,if so then put r=r1,c=c1,r++.

Step11

Now print the elements of 2-d array.

Step12

End.

import java.io.*;
class magic
{
public static void main(String args[])throws Exception
{
BufferedReader num=new BufferedReader(new InputStreamReader(System.in));
int a[][]=new int[10][10];

int n,r=0,i,r1,c1,c;
System.out.println("Enter your odd limit");
n=Integer.parseInt(num.readLine());
c=n/2;

for(i=1;i<=n*n;i++)
{
a[r][c]=i;

r1=r;
c1=c;
r--;
c++;
if(r==-1)
{
r=n-1;
}
if(r>n-1)
{
r=0;
}
if(c>n-1)
{
c=0;
}
if(a[r][c]!=0)
{
r=r1;
c=c1;
r++;
}}
for(r=0;r<n;r++)
{
for(c=0;c<n;c++)
{

System.out.print(a[r][c]);
}
System.out.println();}}}

Program 37-A square of square matrix can be generated


by taking any square matrix of even dimensions (>=2) and
fill the innermost number n is circular order. Then fill its
surrounding square one by one.
Eg N=4 size=6

2
0
3
9
3
8
3
7
3
6

2
1
8

2
2
9

1
9
1
8
1
7

2
3
1
0
5

1
5

2
4
1
1
1
2
1
3
1
4

2
5
2
6
2
7
2
8
2
9

1
6

35

34

33

32

31

30

Step1

Start.

Step2
Declare suitable variables like p,c,x,n,r1,c1,c2,c3,k,n,r,i,j and
declare a 2-d array of limit
Step3

Take an limit in N and perform p=N and c=n/2-1.

Step4
Start a loop from r=n/2-1 to r<=n/2 and perform a[c][r]=p++ and
after executing the loop do c++;
Step5

Start a loop from r=n/2 to r>=n/2-1,rand perform a[c][r]=p++.

Step6

Perform c++.

Step7
circular order.
Step8
Step9

Start the loops with j,c1,c2,c3 and i to store the numbers in


Now print the elements of 2-d array.
End.

import java.io.*;
class circular
{
public static void main(String args[])throws Exception
{
BufferedReader num=new BufferedReader(new InputStreamReader(System.in));
int a[][]=new int[10][10];
int p,c,x,n,r1,c1,c2,c3,k,N,r,i,j;

System.out.println("Enter the value of N");


N=Integer.parseInt(num.readLine());
p=N;
System.out.println("enter the dimensions");
n=Integer.parseInt(num.readLine());
c=n/2-1;

for(r=n/2-1;r<=n/2;r++)
{
a[c][r]=p++;
}
c++;
for(r=n/2;r>=n/2-1;r--)
{
a[c][r]=p++;
}
r=n/2-2;
r1=n/2+1;
x=n/2-2;
for(j=1;j<=n/2-1;j++)
{
for(c1=r;c1<=r1;c1++)
{
a[x][c1]=p++;
}
for(c2=x+1;c2<=r1;c2++)
{
a[c2][r1]=p++;
}
for(c3=r1-1;c3>=r;c3--)
{
a[r1][c3]=p++;
}

k=r1-1;
for(i=r1;i>r+1;i--)
{
a[k][c3+1]=p++;
k--;
}
r--;
r1=r1+1;
x--;
}
for(r=0;r<n;r++)
{
for(c=0;c<n;c++)
{
System.out.print(a[r][c]+""+"");
}
System.out.println();
}
}
}

Program 39-Write a program which takes number of lines


from the user and input that no of sentences,join those
sentences and print them in reverse order after removing
all the punctuation marks.
For eg
If the input is
No of lines=2
Emotions,controlled and directed to work,is character.

By swami Vivekananda.
Then the output is
Vivekananda swami by characters is work to directed and
controlled emotions

Algorithm

Step1

Start.

Step2
Declare variables like n,I,j,z=0 and declare two string of arraysen[],word[] of limit 20 and 200 respectively.
Step3

Find out the length of the string in n.

Step4

Start a loop from i=0 to i<n.

Step5

Start another loop from j=0 to j<sen[i].length().

Step6
Extract the jth character of sen[i]th string and check if they are
equal to any of the punctuation marks
Step7
Step8
str1= ;
Step9

If step6 is true then continue.


If step6 not true,and if ch= ,then perform word[z++]=str1 and
If step8 is also not true then perform str1=str1+ch.

Step10 Start a loop from i=z-1 till i>=0 and then print the contents of
string array word[i].
Step11

End.

import java.io.*;
class ISC2007STRING
{
public static void main(String args[])throws Exception

{
BufferedReader num=new BufferedReader(new InputStreamReader(System.in));

String sen[]=new String[20];

String word[]=new String[200];


String str1="";
int n,i,j,z=0;

System.out.println("Enter your number of lines");


n=Integer.parseInt(num.readLine());

for(i=0;i<n;i++)
{
sen[i]=num.readLine();
sen[i]=sen[i]+"";
}
for(i=0;i<n;i++)
{
for(j=0;j<sen[i].length();j++)
{
char ch=sen[i].charAt(j);
if(ch==','||ch=='.'||ch==';'||ch==':')
{
continue;
}

if(ch==' ')
{
word[z++]=str1;
str1="";
}
else
{
str1=str1+ch;
}
}
}
for(i=z-1;i>=0;i--)
{
System.out.print(word[i]+"");
}
}
}

Program 40-Write a program to input a string from the


user and a word and reverse that word in the string
wherever it is present.
For eg-Input-bear is unbearable animal
Outputraeb is unraebable animal.

Algorithm

Step1

Start.

Step2
Declare variables like i,j,n and declare two empty strings str1 and
str2 and two strings str andstr3.
Step3

Find out the length of the string in n.

Step4

Start a loop from i=0 to i<n and extract 0 th character of the str3 in

ch2.
Step5
Step7
Step8

Extract the ith character of str string and check if it is equal to ch2.
If step6 is true then perform str1=str1substring(i,i+n).
If str1=str3 then perform i=i+n-1.

Step9
Start a loop from j=0 till j<str1.length() and Extract jth character
of str1 in ch1 and perform str2=ch+str2.
Step10 Print str2.
Step11

End.

import java.io.*;
class bear
{ public static void main(String args[])throws Exception
{
BufferedReader num=new BufferedReader(new InputStreamReader(System.in));
String str,str1="",str2="",str3;
int i,j,n;

System.out.println("Enter your string");


str=num.readLine();
System.out.println("Enter your word");
str3=num.readLine();
char ch2=str3.charAt(0);
n=str3.length();
for(i=0;i<str.length();i++)

{
char ch=str.charAt(i);
if(ch==ch2)
{
str1=str.substring(i,i+n);

if(str1.equals(str3))
{

i=i+n-1;
for(j=0;j<str1.length();j++){
char ch1=str1.charAt(j);
str2=ch1+str2;
}
System.out.print(str2);
}
else
{
System.out.print(ch);
}}

else
{
System.out.print(ch);

}
str2="";
}
}
}

Program 41-.Write a program to input numbers in two


array and print the common numbers between them.

Algorithm

Step1
Step2
of limit 5.
Step3

Start.
Declare suitable variables like i,j,k=0and declare an array a[],b[],c[]
Start a loop from i=0 to i<5.

Step4
Start a loop from j=0 to j>5 and then perform c[k]=a[i] if a[i]=b[j]
and after that do k++.
Step5
Step6

Now print the common numbers of the array.


End.

import java.io.*;
class arraycommon
{
public static void main(String args[])throws Exception
{
BufferedReader num=new BufferedReader(new InputStreamReader(System.in));
int a[]=new int[5];
int b[]=new int[5];
int c[]=new int[5];
int i,k=0,j;
for(i=0;i<5;i++)
{
System.out.println("Enter your first array number");
a[i]=Integer.parseInt(num.readLine());
}
for(i=0;i<5;i++)
{
System.out.println("enter your second array number");
b[i]=Integer.parseInt(num.readLine());
}

for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(a[i]==b[j])

{
c[k]=a[i];
k++;
}
}
}
System.out.println("the common numbers in the array are");
for(i=0;i<k;i++)
{
System.out.println(c[i]);}}}

Program 42-Write a program to sort the elements in a two


dimensional array.
For eg

Input--

31

54

87

47

27

30

28

6
4

58

3
4

21

Output

87

47

64

58

54

34

31

30

28

27

21

Algorithm
Step 1 Declare and initialize variables like r1,c1,temp,r,c and a double dimensional
array a[][].
Step 2 Store the numbers from the user in 2-d array.
Step 3 Begin a loop from r1=0 till r<4.
Step 4 Begin a loop from c1=0 till c1<4.
Step 5 Begin a loop from r=0 till r<4.
Step 6 Begin a loop from c=0 till c<4.
Step 7 Check if a[r1][c1]>a[r][c].
Step 8 If step7 is true then perform the swapping of elements.
Step 9 Print the final 2-d array at the end.
Step 10 End of process.
import java.io.*;
class twodsorting
{
public static void main(String args[])throws Exception
{
BufferedReader num=new BufferedReader(new InputStreamReader(System.in));
int a[][]=new int[6][5];
int r,c,temp,r1,c1;
for(r=0;r<4;r++)

{
for(c=0;c<4;c++)
{
System.out.println("Enter your number");
a[r][c]=Integer.parseInt(num.readLine());
}
}
for(r1=0;r1<4;r1++)
{
for(c1=0;c1<4;c1++)
{
for(r=0;r<4;r++)
{
for(c=0;c<4;c++)
{

if(a[r1][c1]>a[r][c])
{
temp=a[r1][c1];
a[r1][c1]=a[r][c];
a[r][c]=temp;
}
}
}
}
}

System.out.println("The sorted array is");


for(r=0;r<4;r++)
{
for(c=0;c<4;c++)
{
System.out.print(a[r][c]+" ");
}
System.out.println();
}

}
}

Program 43-Write a program to sort the elements of rows


in a 2d array.

Algorithm
Step 1 Declare and initialize variables like r1,c1,temp,r,c,z and a double dimensional
array a[][].
Step 2 Store the numbers from the user in 2-d array.
Step 3 Begin a loop from r=0 till r<4.
Step 4 Begin a loop from c=0 till c<4.
Step 5 Begin a loop from z=0 till z<4.
Step 6 Check if a[r][c]>a[r][z].
Step 8 If step7 is true then perform the swapping of elements.
Step 9 Print the final 2-d array at the end.
Step 10 End of process.
import java.io.*;
class rowsorting
{
public static void main(String args[])throws Exception
{
BufferedReader num=new BufferedReader(new InputStreamReader(System.in));
int a[][]=new int[6][5];
int r,c,temp,r1,c1,z;
for(r=0;r<3;r++)
{
for(c=0;c<4;c++)

{
System.out.println("Enter your number");
a[r][c]=Integer.parseInt(num.readLine());
}
}
for(r=0;r<3;r++)
{
for(c=0;c<4;c++)
{
for(z=0;z<4;z++)
{
if(a[r][c]>a[r][z])
{
temp=a[r][c];
a[r][c]=a[r][z];
a[r][z]=temp;
}
}
}
}
for(r=0;r<3;r++)
{
for(c=0;c<4;c++)
{
System.out.print(a[r][c]+" ");
}

System.out.println();
}}}

Program 47-WAP to subtract two dates.


AlgorithmSTEP1:Start.
STEP2:Create a blank array.
STEP3:First of all input the details about date1 with the warning that it should be
greater.
STEP4:Store it in the array created in step 2.
STEP5:Now input the details about date 2.
STEP6:Repeat step 4.
STEP7Now, check the validity of the date, if valid, continue, else display error
message.
STEP8:Subtract the two dates by using array.

STEP9:Display the result


STEP10:END
import java.io.*;

class subdates
{
static
BufferedReader
InputStreamReader(System.in));

in

new

BufferedReader(new

public static void main(String args[])throws Exception

int a[] = new int[3],b[] = new int[3];

int mon[] =
{31,31,28,31,30,31,30,31,31,30,31,30,31,31};

//inputting details about date 1

System.out.println("WARNING : DATE 1 SHOULD BE GREATER\n");

System.out.println("Enter about date 1 : ");

System.out.print("Enter date : ");

a[0] = Integer.parseInt(in.readLine());

System.out.print("Enter month : ");

a[1] = Integer.parseInt(in.readLine());

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

a[2] = Integer.parseInt(in.readLine());

//inputting details about date 2

System.out.println("Enter about date 2 : ");


System.out.print("Enter date : ")
b[0] = Integer.parseInt(in.readLine());

System.out.print("Enter month : ");

b[1] = Integer.parseInt(in.readLine());

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

b[2] = Integer.parseInt(in.readLine());

//checking whether date is valid

if(valid(a[0],a[1],a[2])==false || valid(b[0],b[1],b[2])==false)

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

System.exit(0);

//subtracting dates

else
{
if(b[2]%400==0 || b[2]%4==0 && b[2]%100!=0)
b[2] = 29;
b[0] = a[0]-b[0];
while(b[0]<0)
{
b[0] = b[0]+mon[b[1]];
a[1]--;
}
if(a[1]<0)
{
a[1] = 12;
a[2]--;

}
b[1] = a[1]-b[1];
if(b[1]<0)
{
b[1] = b[1]+12;
a[2]--;
}
b[2] = a[2]-b[2];
}
//displaying result
System.out.println("Date is : "+b[0]+" days "+b[1]+" month "+b[2]+"
year") ;
}
//returning whether date is valid or not
public static boolean valid(int dd,int mm,int yy)
{
int mon[] = {31,31,28,31,30,31,30,31,31,30,31,30,31};
if(yy%400==0 && yy%100==0 || yy%4==0)
mon[2]=29;
if(mm>12)
{
System.out.print("Date is Invalid");
System.exit(0);
}
if(dd>0 && dd<=mon[mm] && mm>0 && mm<13 && yy>0)
return true;
else

return false;
}
}

Output:

Program 48-WAP to add two dates.

STEP1:Start
STEP2:Input details about date 1.

STEP3:Create an array and store very entry in the array location, i.e, date, month
and year.
STEP4:Input details about date 2.
STEP5:Repeat step 3 by creating a different array.
STEP6:Check for the validity of the date.
STEP7:If valid, continue, else display error message.
STEP8:Add the dates.
STEP9:Keep on checking that the date lies in the limit of a specific month.
STEP10:Display the date.
STEP11:END.
import java.io.*;
class adddates
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
public static void main(String args[])throws Exception
{
int a[] = new int[3],b[] = new int[3];
int mon[] = {31,31,28,31,30,31,30,31,31,30,31,30,31,31};
//inputting details of date 1
System.out.println("Enter about Date 1 : ");
System.out.print("Enter date : ");
a[0] = Integer.parseInt(in.readLine());
System.out.print("Enter month : ");
a[1] = Integer.parseInt(in.readLine());
System.out.print("Enter year : ");
a[2] = Integer.parseInt(in.readLine());
//inputting details of date 2

System.out.println("Enter about Date 2 : ");


System.out.print("Enter date : ");
b[0] = Integer.parseInt(in.readLine());
System.out.print("Enter month : ");
b[1] = Integer.parseInt(in.readLine())
System.out.print("Enter year : ");
b[2] = Integer.parseInt(in.readLine());
//checking for validity
if(valid(a[0],a[1],a[2])==false || valid(b[0],b[1],b[2])==false)
{
System.out.println("Invalid Date");
System.exit(0);
}
//adding dates
else
{
if(b[2]%400==0 || b[2]%100!=0 && b[2]%4==0)
mon[2] = 29;
b[0] = a[0]+b[0];
if(b[0]>mon[b[1]+1])
{
b[0] = b[0]-mon[b[1]+1];
b[1]++;
}
b[1] = a[1]+b[1];
if(b[1]>12)

{
b[1] = b[1]%12;
b[2]++;}
b[2] = b[2]+a[2];
}
//displaying result
System.out.println("Date Is : "+b[0]+":"+b[1]+":"+b[2]);
}
public static boolean valid(int dd,int mm,int yy)
{
int mon[] = {31,31,28,31,30,31,30,31,31,30,31,30,31};
if(yy%400==0 || yy%4==0 && yy%100!=0)
mon[2] = 29;
if(mm>12)
{
System.out.print("Date is Invalid");
System.exit(0);
}
if(dd>0 && dd<=mon[mm] && mm>0 && mm<13 && yy>0)
return true;
else
return false;
}
}

Output :
.

You might also like