You are on page 1of 64

Name: Srilanka

Class: 12
Subject: Computer
Submitted by: Srilanka
Submitted to: Asil ismail

Question 1

Write a program to input upper and lower


limit. Display all the prime-palindrome no.
Lying in the limit.
Algorithm-

import java.util.*;
class PrimePalin
{
public static void main()
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter lower limit");
int n=sc.nextInt();

System.out.println("Enter upper limit");


int m= sc.nextInt();
System.out.println("Prime-Palindrome Nos.");
while(n<=m)
{
int k=n;int d=0;
while(k!=0)
{d=d*10+(k%10); k=k/10;}
if(d==n)
{int c=0;
for(int i=1;i<=n;i++)
if(n%i==0)
c++;
if(c==2)
System.out.println(n+" ");
}
n++;
}
}
}

Question 2

Write a program to input a no. and form a


new no. by arranging all digits in ascending
order.
Algorithm-

import java.util.*;
class DigiRange
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the no.");
int n= sc.nextInt();
int k=n;int s;int r=n;
while(k!=0)
{ n=r;r=0;s=n%10;n=n/10;
while(n!=0)

{int b=n%10;
if(b<s){
r=r*10+s;s=b;}
else if(b>s)
r=r*10+b;
n=n/10;
}
System.out.print(s);
k=k/10;
}

}
}

Question 3

Write a program to display black cats of a


number.
Algorithmimport java.util.*;
class SplitNo

{public static void main()


{ Scanner sc= new Scanner(System.in);
System.out.println("Enter a no.");
int n= sc.nextInt();
for(int i=0;i<n;i++)
for(int j=i;j<n;j++)
{ int s=0;
for(int k=i;k<j;k++)
s=s+k;
if(s==n)
{for(int l=i;l<j;l++)
System.out.print(l+" ");
System.out.println(); } } } }

Question 4

Write a program to declare a square matrix


of order mxm. Store nos. In the matrix.
Display the original matrix. Sort the nonboundary elements of matrix in ascending

order. Display the diagonal elements of the


matrix along with their sum.
Algorithm1. Start
2. Input the value of m
3. Store nos. In the matrix
4. Display the original matrix
5. Sort the non-boundary elements
6. Display the new rearranged matrix
7. Display the diagonal elements
8. Calculate and display the sum of diagonal elements
9. Stop

import java.util.*;
class Matrix_Pract2
{public static void main()
{ Scanner sc=new Scanner(System.in);
System.out.println("Enter value of m");
int m= sc.nextInt();
if(m<20&&m>3)
{ int a[][]=new int[m][m];
System.out.println("Enter no. in marix");
for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
a[i][j]=sc.nextInt();
int b[]=new int[((m-2)*(m-2))];int c=0;
for(int i=1;i<m-1;i++)
for(int j=1;j<m-1;j++)
{b[c]=a[i][j]; c++;}

int t=0;
for(int i=0;i<c;i++)
for(int j=0;j<(c-1-i);j++)
{ if(b[j]>b[j+1])
{ t=b[j];
b[j]=b[j+1];
b[j+1]=t; } }
System.out.println("ORIGINAL MATRIX : ");
for(int i=0;i<m;i++)
{for(int j=0;j<m;j++)
System.out.print(a[i][j]+" ");
System.out.println();}
for(int d=0, i=1;i<m-1;i++)
for(int j=1;j<m-1;j++)
{a[i][j]= b[d];d++;}
System.out.println("REARRANGED MATRIX :");
for(int i=0;i<m;i++)
{for(int j=0;j<m;j++)
System.out.print(a[i][j]+" ");
System.out.println();}
System.out.println("DIAGONAL ELEMENTS :");
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]+" ");
else
System.out.print(" ");
System.out.println();}
int e=0;

for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
if((i==j)||((i+j)==m-1))
e=e+a[i][j];
System.out.println("SUM OF DIAGONAL ELEMENTS = "+e); }
else
System.out.println("THE MATRIX SIZE IS OUT OF RANGE !!!"); } }

Question 5

Write a program to enter nos. In 4x4 matrix.


Display the original matrix. Sort the rows of

matrix in ascending order. Display the new


sorted matrix.
Algorithm1. Start
2. Store nos. In matrix
3. Display the original matrix
4. Sort each row of matrix using any sorting technique
5. Display the sorted array
6. Stop

import java.util.*;
class Surprise
{public static void main()
{
Scanner sc= new Scanner(System.in);
int A[][] =new int[4][4];
System.out.println("Enter 16 nos.");
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
A[i][j]=sc.nextInt();
System.out.println("Original array:");
for(int i=0;i<4;i++){
for(int j=0;j<4;j++)
System.out.print(A[i][j]+" ");
System.out.println();}
int t=0;
for(int i=0;i<4;i++)
{for(int j=0;j<4;j++)
for(int k=0;k<(3-j);k++)

{if(A[i][k]>A[i][k+1])
{t=A[i][k];
A[i][k]=A[i][k+1];
A[i][k+1]=t;
}}
}
System.out.println("the resultant array :" );
for(int i=0;i<4;i++)
{for(int j=0;j<4;j++)
System.out.print(A[i][j]+" ");
System.out.println();
}} }

Question 6

Write a program to input nos. In matrix of


order 4x4. Display the original matrix.
Display the elements beyond left diagonal.

Algorithm1. Start
2. Input nos. In matrix
3. Display the original matrix
4. Print the elements beyond left diagonal
5. Stop

import java.util.*;
class Diagonal
{
public static void main()
{
Scanner sc = new Scanner(System.in);
int A[][]=new int[4][4];
System.out.println("Please enter no. in array");
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
A[i][j]=sc.nextInt();
for(int i=0;i<4;i++)
{for(int j=0;j<4;j++)
System.out.print(A[i][j]);
System.out.println();}
for(int i=0;i<4;i++)
{
for(int j=1;j<=(4-i);j++)
System.out.print(" ");
for(int k=(4-i);k<4;k++)
System.out.print(A[i][k]);

System.out.println();
}
}
}

Question 7

Write a program to input nos. In a matrix of


order mxn.
Display the original matrix. Find and display
the max term and min term of matrix. Also
sort the matrix in ascending order.
Algorithm1.Start
2.Enter the values of m and n
3.Input nos. In matrix
4.Display the original matrix
5.Find the largest no. Present in the matrix
6.Find the smallest no. In the matrix
7.Sort the matrix using any sorting technique
8.Display the new sorted array along with max &min term

9.Stop

import java.util.*;
class Arrange
{public static void main()
{Scanner sc=new Scanner(System.in);
System.out.println("enter m");
int m= sc.nextInt();
System.out.println("enter n");
int n=sc.nextInt();
if((m<20&&m>2)&&(n>2&&n<20))
{int a[][]=new int[m][n];
System.out.println("Enter nos. in matrix");
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
a[i][j]=sc.nextInt();
System.out.println("entered matrix");
for(int i=0;i<m;i++)
{for(int j=0;j<n;j++)
System.out.print(a[i][j]);
System.out.println();}
int max=0;int min=a[0][0];
int p1=0,p2=0,p3=0,p4=0;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{if(a[i][j]>max)
{max=a[i][j];p1=i;p2=j;}
if(a[i][j]<min)
{min=a[i][j];p3=i;p4=j;}

}
System.out.println("max term= "+max+" row= "+p1+" column=
"+p2);
System.out.println("min term= "+min+" row= "+p3+" column=
"+p4);
int t=0,p=0,q=0;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{p=i;q=j;
for(int k=i;k<m;k++)
for(int l=j;l<n;l++)
{if(a[k][l]<a[p][q])
{p=k;q=l;}
}
t=a[i][j];
a[i][j]=a[p][q];
a[p][q]=t;
}
System.out.println("new matrix:");
for(int i=0;i<m;i++)
{for(int j=0;j<n;j++)
System.out.print(a[i][j]);
System.out.println();}
}
}
}

Question 8
Write a program to enter nos. in 4x4 matrix.
Display the orginal matrix. Replace the elements
of left diagonal and right diagonal with 0. Also
display the sum of first and fourth column.
Algorithm1. Start
2. Input nos. In 4x4 matrix
3. Replace the elements of right diagonal with 0
4. Replace the elements of left diagonal with 0
5. Calculate the sum of elements of first and fourth column
6. Display the original matrix
7. Display the rearranged matrix
8. Display the sum of elements
9. Stop

import java.util.*;
class LeftRight
{public static void main()

{Scanner sc=new Scanner(System.in);


System.out.println("Enter nos. in matrix");
int A[][]=new int[4][4];
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
A[i][j]=sc.nextInt();
System.out.println("the original matrix :");
for(int i=0;i<4;i++)
{ for(int j=0;j<4;j++)
System.out.print(A[i][j]);
System.out.println();}
for(int i=0;i<4;i++) for(int j=0;j<4;j++)
{if(i==j)
A[i][j]=0;
if((i+j)==3)
A[i][j]=0; }
System.out.println("New matrix :");
for(int i=0;i<4;i++)
{for(int j=0;j<4;j++)
System.out.print(A[i][j]);
System.out.println(); }
int s1=0,s4=0;
for(int i=0;i<4;i++)
{s1=s1+A[i][0];
s4=s4+A[i][3];}
System.out.println("Sum of first column = "+s1);
System.out.println("Sum of fourth column = "+s4); } }

Question 9

Write a program to input a square matrix of


order mxm.
m should be less than 20 and greater than 2.
Display the original matrix and mirrored matrix.
Algorithm1. Start
2. Input value of m
3. If m>2&&m<20
4. Store the nos. In matrix
5. Display the original matrix
6. Interchange the coumns
7. Display the new matrix
8. Stop
import java.util.*;

class Mirror
{public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the value of m");
int m=sc.nextInt();
if(m<20&&m>2)
{
int a[][]=new int[m][m];
System.out.println("enter the nos. in matrix");
for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
a[i][j]=sc.nextInt();
System.out.println("the original matrix : ");
for(int i=0;i<m;i++)
{for(int j=0;j<m;j++)
System.out.print(a[i][j]);
System.out.println();
}
int n=m-1;int t=0;
for(int i=0;i<(m/2);i++)
{for(int j=0;j<m;j++)
{t=a[j][i];
a[j][i]=a[j][n];
a[j][n]=t;
} n--;}
System.out.println("Mirrored matrix :");
for(int i=0;i<m;i++)
{for(int j=0;j<m;j++)

System.out.print(a[i][j]);
System.out.println(); }
else{ System.out.println("Sorry!!! out of range ");}
} }

Question 10

Write a program to input alphabets in a 4x4


matrix. Arrange all the vowels first and then the
remaining ones. Print the original and
rearranged matrix.
Algorithm1.Start
2.Input alphabets in matrix
3.Create a new matrix of order 4x4
4.Display the original matrix
5.Insert the vowels from original into new matrix
6.Then insert the remaining alphabets
7.Print the new matrix
8.Stop

import java.util.*;
import java.io.*;
class Vow
{public static void main()throws IOException
{Scanner sc=new Scanner(System.in);
System.out.println("enter alphabets ");
char a[][]=new char[4][4];
for(int i=0;i<4;i++) for(int j=0;j<4;j++)
{a[i][j]=(char)System.in.read();}
System.out.println("original array");
for(int i=0;i<4;i++) {for(int j=0;j<4;j++)
System.out.print(a[i][j]+" ");
System.out.println();}
char b[][]=new char[4][4]; int x=0,y=0;
for(int i=0;i<4;i++) {for(int j=0;j<4;j++)
if(a[i][j]=='A'||a[i][j]=='E'||a[i][j]=='I'||a[i][j]=='O'|| a[i][j]=='U')
{b[x][y]=a[i][j];
y++;a[i][j]=' ';if(y==4){y=0;x++;} }}
for(int i=0;i<4;i++) for(int j=0;j<4;j++)
if(a[i][j]!=' ')
{ b[x][y]=a[i][j];
y++;if(y==4){y=0;x++;} }
System.out.println("new array");
for(int i=0;i<4;i++) {for(int j=0;j<4;j++)
System.out.print(b[i][j]+" ");
System.out.println();} } }

Question 11

Write a program to accept a number(<1000) and


display in words.
Sample input 25
Sample output- twenty five
Algorithm1- start
2- Store required number names in an array.
3- Put three condition as given in program0<a<=20; 20<a<=100; 100<a<1000
4- In first condition the number matches with the index number of its
number name.
5- In second condition p is used to store last part of number which
matches with the index number in array of its number name and d
stores first part of number.
6- Similarly third condition is performed.

7- Result is displayed after every condition.


8- stop

import java.io.*;
class numbernames
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
int p;int d;int v;int b;int n;int c;
System.out.println("Enter any number between 1 to 1000");
int a= Integer.parseInt(in.readLine());
Stringm[]={"","one","two","three","four","five","six","seven","eight","nine
","ten","eleven","twelve"thirteen","fourteen","fifteen","sixteen","seventee
n","eighteen","nineteen","twenty",
"thirty","forty","fifty","sixty","seventy","eighty","ninety","hundred","thous
and"};
if(a>0&&a<=20)
{
System.out.println(m[a]);
}
if(a>20&&a<=100)
{
p=a%10;
d=a/10;
System.out.println(m[20+(d-2)]+" "+m[p]); }
if(a>100&&a<1000)
{ n=a%100;
b=n%10;
v=n/10;

c=a/100;
System.out.println(m[c]+" hundred "+m[20+(v-2)]+" "+m[b]);
} } }

Question 12

Write a program to accept a string and convert it


in alphabetical order and display the new string.
Algorithm1. Accept a string from the user.
2. l=str.length();

// to find the length of the string

3. Loop executesfor(j=65;j<=90;j++)
{for(i=0;i<l;i++)
{ch=str.charAt(i);

// first loop runs from ASCII code of A to Z


// second loop runs till length of the string
// to find the characters of the string

if(ch==(char)j||ch==(char)(j+32))
System.out.print(ch); }}
4. Stop.

import java.io.*;
class alphaorder
{

// if above condition is true then display it

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


{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
int l;char ch;int i;String st;int j;
System.out.println("Enter string");
String str=in.readLine();
l=str.length();
for(j=65;j<=90;j++)
{
for(i=0;i<l;i++)
{
ch=str.charAt(i);
if(ch==(char)j||ch==(char)(j+32))
System.out.print(ch);
}}}}

Question 13

Write a program to accept two words and frame


a new word by taking one by one character from

each word. Assuming both words have same


length.
Algorithm1. Accept two string from the user having same length.
2. Find length of any of the string.
3. Loop executesfor(i=0;i<l;i++)
{ s=s+str.charAt(i)+st.charAt(i); // one character from each string and
adds them to s
} System.out.println(s);

// displays the final string

4. Stop.

import java.io.*;
class combine
{public static void main(String args[])throws IOException
{InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
int l;int i;String s;s="";
System.out.println("Enter first string");
String str=in.readLine();
System.out.println("Enter second string");
String st=in.readLine();
l=str.length();
for(i=0;i<l;i++)
{s=s+str.charAt(i)+st.charAt(i); }
System.out.println(s);
}}

Question 14

Write a program to enter encoding factor and


string to be encoded. Display the encoded string.
Algorithm1. Start
2. Enter string and encoding factor
3. Interchange each character with encoded character
4. Cocatenate to form encoded string
5. Display encoded string
6. Stop

import java.util.*;
class Enco
{public static void main()
{Scanner sc= new Scanner(System.in);
System.out.println("Enter Sring ");
String s=sc.nextLine();
System.out.println("Enter encoding factor");
int n=sc.nextInt();
String st="";int k=65+32;
for(int j=0;j<s.length();j++)

{char ch=s.charAt(j);
if(((int)ch)<(91+32-n))

st=st+(char)(((int)ch)+n);
else if(((int)ch)>=(91+32-n))
st=st+(char)(k+((int)ch-(123-n))); }
System.out.println("ecoded string= "+st);
//abcdefghijklmnopqrstuvwxyz
} }

Question 15

Write a program to enter a text and then count


the frequency of words and and an . Display
the original text along with frequencies of an
and and.
Agorithm1. Start
2. Enter the string
3. Calculate the frequency of an and and
4. Use separate functions to make the task easier

5. Display original string along with the frequencies


6. Stop

import java.util.*;
class Frequency
{ String text;
int countan,countand,len;
public Frequency()
{ text="";
len=0;
countan=0;
countand=0; }
public void accept(String N)
{ text=N;
len=text.length();
}
public void checkfreqan()
{ int x=0;
for(int i=0;i<len;i++)
{ char ch=text.charAt(i);
if(ch==' '||ch=='.'||ch==',')
{String st=text.substring(x,i);
x=i+1;
if(st.equals("an"))
countan++;
} } }
public void checkfreqand()
{ int x=0;
for(int i=0;i<len;i++)

{ char ch=text.charAt(i);
if(ch==' '||ch=='.'||ch==',')
{String st=text.substring(x,i);
x=i+1;
if(st.equals("and"))
countand++;
} } }
public void display()
{ System.out.println("Original String = "+text);
System.out.println("Frequency of an = "+countan);
System.out.println("Frequency of and = "+countand); }
public static void main()
{ Scanner sc= new Scanner(System.in);
System.out.println("Enter Text......");
String str= sc.nextLine();
Frequency ob=new Frequency();
ob.accept(str);
ob.checkfreqan();
ob.checkfreqand();
ob.display();
} }

Question 16

Write a program that enters 2 strings. Check


whether one of the strings is substring of the
other or not.
Algorithm1. Start
2. Enter two strings
3. Compare two strings to check if 1 is substring of other
4. Else return false
5. Above task can be easily done by using function
6. Display the result
7. Stop

import java.util.*;
class Substring
{ public static boolean isSubstring(String s,String t)
{ int m=s.length();
int n=t.length();
int i=0;
for(int j=0;j<n;j++)
{if(s.charAt(i)==t.charAt(j)) i++;
if(i==m) return true; }
return false;
}
public static void main()
{ Scanner sc=new Scanner(System.in);
System.out.println("Enter string sequence be searched for");
String str=sc.nextLine();
System.out.println("Enter String in which to search for");
String st=sc.nextLine();

if(isSubstring(str,st))
System.out.println(str+" is substring of "+st);
else
System.out.println(str+" is not a substring of "+st);
}}

Question 17

Write a program to input a string. Reverse each


pair of adjacent letters of string. If string has
odd no. of letters last letter is unchaged.
Algorithm1.Start
2.Input a string
3.Find the length of string
4.Create a new object of stringbuffer class
5.With the loop i=0 to last
6.Last=len-1 if length is even else len-2
7.Interchange the adjacent characters
8.Display the new string

9.Stop

import java.util.*;
class Strings
{public static void main()
{Scanner sc=new Scanner(System.in);
System.out.println("Enter a string/line of text ");
String origstr=sc.nextLine();
String outstr=swapPairs(origstr);
System.out.println("New string after swapping characters:");
System.out.println(outstr); }
public static String swapPairs(String str)
{int len=str.length();
int last=0;
if(len%2==0)
last=len-1;
else
last=len-2;
StringBuffer sb=new StringBuffer(str);
char ch1,ch2;
for(int i=0;i<last;i+=2){
ch1=sb.charAt(i);
ch2=sb.charAt(i+1);
sb.setCharAt(i,ch2);
sb.setCharAt(i+1,ch1); }
return sb.toString();
}}

Question 18

Write a program to accept as input. The words in


string are separated by blank space, each letter
must be in uppercase and sentence terminated
by .,,,? . Display the length of sentence in
words and arrange the words in alphabetical
order.
Algorithm1. Start
2. Input a sentence from user
3. Count the no. Of words in sentence
4. Extract each word and arrange them in alphabetical order
5. Display word length and new string
6. Stop

import java.util.*;
class Pract
{public static void main()
{Scanner sc= new Scanner(System.in);
System.out.println("Enter a string");
String st=sc.nextLine();

int l=st.length();
int x=0; String A[]=new String[l];
int c=0;
for(int i=0;i<l;i++)
{char ch=st.charAt(i);
if(ch==' '||ch=='.'||ch==',')
{String s= st.substring(x,i); A[c]=s;
x=i+1; c++;} }
System.out.println("No. of words = "+c);
String t="";
for(int i=0;i<=c;i++)
for(int j=0;j<c-1;j++)
if(A[j].compareTo(A[j+1])>0){
t=A[j]; A[j]=A[j+1];
A[j+1]=t;}
System.out.println("New String:");
for(int i=0;i<c;i++)
System.out.print(A[i]+" ");
}}

Question 19

Write a program to enter to enter the month first day and print the whole
calendar?
import java.util.*;
class CalendarProgram
{
//Function to match the given month and return its maximum days
int findMaxDay(String mname, int y)
{
String months[] = {"","January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"};
int D[]={0,31,28,31,30,31,30,31,31,30,31,30,31};

if((y%400==0) || ((y%100!=0)&&(y%4==0)))
{
D[2]=29;
}
int max = 0;
for(int i=1; i<=12; i++)
{
if(mname.equalsIgnoreCase(months[i]))
{
max = D[i]; //Saving maximum day of given month
}
}
return max;
}

//Function to match the given weekday name and return its weekday no.
int findDayNo(String wname)
{

String days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",


"Saturday"};
int f = 0;
for(int i=0; i<7; i++)
{
if(wname.equalsIgnoreCase(days[i]))
{
f = i; //Saving week day no. given day (e.g. '0' for Sunday)
}
}
return f;
}

//Function for creating the calendar


void fillCalendar(int max, int f, String mname, int y)
{
int A[][] = new int[6][7];
int x = 1, z = f;

for(int i=0;i<6;i++)
{
for(int j=f; j<7; j++)
{
if(x<=max)
{
A[i][j] = x;
x++;
}
}

f = 0;
}

for(int j=0; j<z; j++) //Adjustment to bring last (6th) row elements to first row
{
A[0][j]=A[5][j];
}

printCalendar(A, mname, y); //Calling function to print the calendar


}

//Function for printing the calendar


void printCalendar(int A[][], String mname, int y)
{
System.out.println("\n\t----------------------------------------------------");
System.out.println("\t\t\t "+mname+" "+y);
System.out.println("\t----------------------------------------------------");
System.out.println("\tSUN\tMON\tTUE\tWED\tTHU\tFRI\tSAT");
System.out.println("\t----------------------------------------------------");

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


{
for(int j = 0; j < 7; j++)
{
if(A[i][j]!=0)
System.out.print("\t "+A[i][j]);
else
System.out.print("\t ");
}

System.out.println("\n\t----------------------------------------------------");
}
}

public static void main(String args[])


{
CalendarProgram ob = new CalendarProgram();
Scanner sc = new Scanner(System.in);
System.out.print("Enter the year : ");
int y = sc.nextInt();
System.out.print("Enter the month name (e.g. January) : ");
String mname = sc.next();
System.out.print("Enter the week day name (e.g. Sunday) of 1st day of "+mname+" : ");
String wname = sc.next();

int max = ob.findMaxDay(mname,y);


int f = ob.findDayNo(wname);
ob.fillCalendar(max,f,mname,y);
}
}

Question 20
Write a program to input a double dimension matrix and print the pyramid out of it.

import java.util.*;
class Diagonal
{
public static void main()
{
Scanner sc = new Scanner(System.in);
int A[][]=new int[5][5];
System.out.println("Please enter no. in array");
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
A[i][j]=sc.nextInt();
for(int i=0;i<5;i++)
{
for(int j=1;j<=(5-i);j++)
System.out.print(" ");

for(int k=(5-i);k<=4;k++)
System.out.print(A[i][k]+" ");
System.out.println(" ");
}
}
}
Question 21
Write a program to enter a matrix and print it. Then rearrange the matrix
in ascending order. Print the rearranged matrix and find the sum of
diagonal elements.
Answer
import java.util.*;
class Matrix_Pract2
{public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value of m");
int m= sc.nextInt();
if(m<20&&m>3)
{
int a[][]=new int[m][m];
System.out.println("Enter no. in marix");
for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
a[i][j]=sc.nextInt();
int b[]=new int[((m-2)*(m-2))];int c=0;
for(int i=1;i<m-1;i++)
for(int j=1;j<m-1;j++)
{b[c]=a[i][j]; c++;}
int t=0;

for(int i=0;i<c;i++)
for(int j=0;j<(c-1-i);j++)
{ if(b[j]>b[j+1])
{
t=b[j];
b[j]=b[j+1];
b[j+1]=t;
}
}
System.out.println("ORIGINAL MATRIX : ");
for(int i=0;i<m;i++)
{for(int j=0;j<m;j++)
System.out.print(a[i][j]+" ");
System.out.println();}

for(int d=0, i=1;i<m-1;i++)


for(int j=1;j<m-1;j++)
{a[i][j]= b[d];d++;}

System.out.println("REARRANGED MATRIX :");


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

System.out.println("DIAGONAL ELEMENTS :");


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]+" ");
else
System.out.print(" ");
System.out.println();}
int e=0;
for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
if((i==j)||((i+j)==m-1))
e=e+a[i][j];

System.out.println("SUM OF DIAGONAL ELEMENTS = "+e);


}
else
System.out.println("THE MATRIX SIZE IS OUT OF RANGE !!!");
}
}
Output

Question 22
Write a program to enter a binary number and convert it to decimal
number?
import java.util.*;
class Binary2deci
{

public static void main()


{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the binary no.");
int n=sc.nextInt();
int x=0; int d=0; int k=n;
while(n!=0)
{
int a=n%10;
d =d+a*(int)Math.pow(2,x);
x++; n=n/10;
}
System.out.println("Decimal equivalent of "+k+" = "+d);
}
}
Output

Question 23
Write a program to enter the number of working of an employee and find
his weekly pay.
Answer
import java.util.*;
class Payment
{

public static void main()


{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the weekly working hours");
int n=sc.nextInt();
double r=0.0;
if(n<48)
r=n*1000;
else if(n>48&&n<=56)
r=(48*1000)+(n-48)*1250;
else
r=(48*1000)+(8*1250)+(n-56)*1500;
System.out.println("Working Hours = "+n);
System.out.println("Weekly pay = "+r);
}
}
Output

Question 24
Write a program to enter a number and find whether it is magic or not.
Answer
import java.util.*;
class Magic

{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
int s=n;int c=0;
while(s>9)
{ n=s;c=0;
while(n!=0)
{c=c+(n%10);System.out.println(c);
n=n/10;}
s=c;
}
if(s==1)
System.out.println("Magic no.");
else
System.out.println("Not a Magic no.");
}
}
Output

Question 25
Write a program to enter a number and find its digit range and print the
same.
Answer
import java.util.*;
class DigiRange
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the no.");
int n= sc.nextInt();
int k=n;int s;int r=n;
System.out.println("the digi range is ");
while(k!=0)
{ n=r;r=0;s=n%10;n=n/10;
while(n!=0)
{int b=n%10;
if(b<s){

r=r*10+s;s=b;}
else if(b>s)
r=r*10+b;
n=n/10;
}

System.out.print(s+" ");
k=k/10;
}

}
}
Output

Question 26

Write a program to enter a number and split the number.


ANSWER
import java.util.*;
class SplitNo
{
public static void main()
{

Scanner sc= new Scanner(System.in);


System.out.println("Enter a no.");
int n= sc.nextInt();
for(int i=0;i<n;i++)
for(int j=i;j<n;j++)
{ int s=0;
for(int k=i;k<j;k++)
s=s+k;
if(s==n)
{for(int l=i;l<j;l++)
System.out.print(l+" ");
System.out.println();}
}

}
}
OUTPUT

QUESTION 27
Write a program to enter the lower and upper limit and find the
primepalin number between them and print the same.
Answer

import java.util.*;
class PrimePalin
{
public static void main()
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter lower limit");
int n=sc.nextInt();
System.out.println("Enter upper limit");
int m= sc.nextInt();
System.out.println("Prime-Palindrome Nos.");
while(n<=m)
{
int k=n;int d=0;
while(k!=0)
{d=d*10+(k%10); k=k/10;}
if(d==n)
{int c=0;
for(int i=1;i<=n;i++)
if(n%i==0)
c++;
if(c==2)
System.out.println(n+" ");
}
n++;
}
}
}
Output

Question 28
Write a program to enter a string and find the frequency of an and and in
the given string and print the same.
Answer
import java.util.*;
class FrequencyOfAlpha
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter string");
String s=sc.nextLine();
int a=0;
for(int i=65;i<91;i++)

{
a=0;
for(int j=0;j<s.length();j++)
{
char ch=s.charAt(j);
if(ch==(char)i||ch==(char)(i+32))
a++;

}
if(a!=0)
System.out.println("Frequency of "+(char)i+"or"+(char)(i+32)+" = "+a);

}
}
}
Output

Question 29
Write a program to enter a double dimension matrix and find the mirror
image of it. Display both the matrix.
Answer
import java.util.*;
class Mirror

{public static void main()


{
Scanner sc=new Scanner(System.in);
System.out.println("enter the value of m");
int m=sc.nextInt();
if(m<20&&m>2)
{
int a[][]=new int[m][m];
System.out.println("enter the nos. in matrix");
for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
a[i][j]=sc.nextInt();
System.out.println("the original matrix : ");
for(int i=0;i<m;i++)
{for(int j=0;j<m;j++)
System.out.print(a[i][j]+" ");
System.out.println(" ");
}
int n=m-1;int t=0;
for(int i=0;i<(m/2);i++)
{for(int j=0;j<m;j++)
{t=a[j][i];
a[j][i]=a[j][n];
a[j][n]=t;
} n--;}
System.out.println("Mirrored matrix :");
for(int i=0;i<m;i++)
{for(int j=0;j<m;j++)
System.out.print(a[i][j]+" ");

System.out.println(" ");}

}
else{
System.out.println("Sorry!!! out of range ");}
}
}
Output

Question 30

Write a program to enter a matrix. Fill zero in diagonal elements then find
the sum of first and fourth column.
Answer
import java.util.*;
class LeftRight
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter nos. in matrix");
int A[][]=new int[4][4];
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
A[i][j]=sc.nextInt();
System.out.println("the original matrix :");
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
System.out.print(A[i][j]+" ");
System.out.println(" ");}
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{if(i==j)
A[i][j]=0;
if((i+j)==3)
A[i][j]=0;
}
System.out.println("New matrix :");
for(int i=0;i<4;i++)

{for(int j=0;j<4;j++)
System.out.print(A[i][j]+" ");
System.out.println(" ");
}
int s1=0,s4=0;
for(int i=0;i<4;i++)
{s1=s1+A[i][0];
s4=s4+A[i][3];}
System.out.println("Sum of first column = "+s1);
System.out.println("Sum of fourth column = "+s4);
}
}
Output

Question 31
Write a program to enter a matrix and find the maximum and minimum
term accordingly find its corresponding row or column. And rearrange the
matrix in ascending order.
Answer
import java.util.*;
class Arrange
{public static void main()
{Scanner sc=new Scanner(System.in);
System.out.println("enter m");

int m= sc.nextInt();
System.out.println("enter n");
int n=sc.nextInt();
if((m<20&&m>2)&&(n>2&&n<20))
{int a[][]=new int[m][n];
System.out.println("Enter nos. in matrix");
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
a[i][j]=sc.nextInt();
System.out.println("entered matrix");
for(int i=0;i<m;i++)
{for(int j=0;j<n;j++)
System.out.print(a[i][j]+" ");
System.out.println(" ");}
int max=0;int min=a[0][0];
int p1=0,p2=0,p3=0,p4=0;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{if(a[i][j]>max)
{max=a[i][j];p1=i;p2=j;}
if(a[i][j]<min)
{min=a[i][j];p3=i;p4=j;}
}
System.out.println("max term= "+max+" row= "+p1+" column= "+p2);
System.out.println("min term= "+min+" row= "+p3+" column= "+p4);
int t=0,p=0,q=0;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{p=i;q=j;

for(int k=i;k<m;k++)
for(int l=j;l<n;l++)
{if(a[k][l]<a[p][q])
{p=k;q=l;}
}
t=a[i][j];
a[i][j]=a[p][q];
a[p][q]=t;
}
System.out.println("new matrix:");
for(int i=0;i<m;i++)
{for(int j=0;j<n;j++)
System.out.print(a[i][j]+" ");
System.out.println(" ");}
}
}
}

Output

Question 32
Write a program enter a string the rearrange string In ascending order.
Answer
import java.util.*;
class Alphabeti
{public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter string");
String st=sc.nextLine();

String s=st.toLowerCase();
int l=s.length();
String str="";
for(int i=0;i<l;i++)
{char ch= s.charAt(i);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
{}else str=str+ch;

}
s=Character.toUpperCase(str.charAt(0))+str.substring(1);
System.out.println("Original String = "+st);
System.out.println("New String = "+s);str="";
for(int i=65;i<91;i++)
for(int j=0;j<s.length();j++)
{char ch=s.charAt(j);
if((ch==(char)i)||(ch==(char)(i+32)))
str=str+ch;
}
System.out.println("Final String = "+str);
}
}

Output

Question 33
Write a program to enter a number and find whether it is composite
magic or not and between a limit.
Answer
import java.util.*;
class Comagic
{public static void main()
{Scanner sc=new Scanner(System.in);
System.out.println("Enter lower limit");
int m=sc.nextInt();
System.out.println("Enter upper limit");
int n=sc.nextInt();
for(int i=m;i<=n;i++)
{
if(chk(i))
System.out.print(i+" ");}

}
public static boolean chk(int n)
{
int c=0;boolean a=false,b=false;

for(int i=1;i<n;i++)
if(n%i==0)
c++;
if(c!=2)
a=true;
int k=n;int s=0;
while(k>9)
{s=k;int d=0;
while(s!=0)
{d=d+s%10;
s=s/10;
}
k=d;
}
if(k==1)
b=true;
if((a==true)&&(b==true))
return true;
else return false;

}
Output

Question 34

Write a pro

You might also like