Professional Documents
Culture Documents
Solution
Solution
as a palindrome. Given two positive integers m and n, where m < n, write a program to
determine how many prime-palindrome integers are there in the range between m and n (both
inclusive) and output them.
The input contains two positive integers m and n where m < 3000 and n < 3000. Display the
number of prime palindrome integers in the specified range along with their values in the
format specified below:
Test your program with the sample data and some random data:
Example 1:
INPUT: m=100
N=1000
Example 2:
INPUT:
M=100
N=5000
import java.io.*;
{
BufferedReader stdin=new BufferedReader(new
InputStreamReader(System.in));
int m=Integer.parseInt(stdin.readLine());
int n=Integer.parseInt(stdin.readLine());
int count=0,flag;
if((m<3000)&&(n<3000))
for(int i=m;i<=n;i++)
flag=0;
for(int j=2;j<i;j++)//checks whether the
number is prime
if(i%j==0)
flag=1;
break;
i2=i;r=0;
r=(r*10)+(i2%10);
i2=i2/10;
if((flag==0)&&(r==i))
System.out.print(",");
System.out.print(i);
count++;
System.out.println();
System.out.println("Frequency of prime
palindrome integers:"+count);
else
System.out.println("Out of range");
Question 2
Write a program to accept a sentence as input. The words in the string are to be separated by a
blank. Each word must be in upper case. The sentence is terminated by either '.','!' or '?'.
Perform the following tasks:
Test your program with the sample data and some random data:
Example 1:
INPUT: NECESSITY IS THE MOTHER OF INVENTION.
OUTPUT:
Length: 6
Rearranged Sentence:
INVENTION IS MOTHER NECESSITY OF THE
Example 2:
INPUT: BE GOOD TO OTHERS.
OUTPUT:
Length: 4
Rearranged Sentence: BE GOOD OTHERS TO
solution
import java.io.*;
String s=stdin.readLine();
int l=s.length();
char ch;
int pos=0,length=0,w=0;
String temp;
if(c=='.'||c=='!'||c=='?')
{
for(int i=0;i<l;i++) //computes the length of the
sentence(in words)
ch=s.charAt(i);
if(ch==' '||ch==c)
length++;
System.out.println("Length:"+length);
ch=s.charAt(j);
if(ch==' '||ch==c)
word[w]=s.substring(pos,j);
w++;
pos=j+1;
for(int m=0;m<(length-k-1);m++)
if(word[m].compareTo(word[m+1])>0)
{
temp=word[m];
word[m]=word[m+1];
word[m+1]=temp;
System.out.print("Rearranged Sentence:");
System.out.print(word[r]+" ");
else
System.out.print("Invalid input");
Question 3
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:
2. Find the maximum and minimum value in the matrix and display
them along with their position.
INPUT:
M=3
N=4
Original matrix:
8 7 9 3
-2 0 4 5
1 3 6 -4
Largest Number: 9
Row: 0
Column: 2
Smallest Number: -4
Row=2
Column=3
Rearranged matrix:
-4 -2 0 1
3 3 4 5
6 7 8 9
solution
import java.io.*;
int M=Integer.parseInt(stdin.readLine());
int N=Integer.parseInt(stdin.readLine());
int large,small;
int lrow=0,srow=0;
int lcol=0,scol=0;
int i=0,j=0,temp;
if(2<M&&M<20&&2<N&&N<20)
for( j=0;j<N;j++)
num[i][j]=Integer.parseInt(stdin.readLine());
System.out.println("Original matrix:");
large=num[0][0];
small=num[0][0];
{
for(j=0;j<N;j++)
System.out.print(num[i][j]+" ");
if(num[i][j]>large)
large=num[i][j];
lrow=i;
lcol=j;
if(num[i][j]<small)
small=num[i][j];
srow=i;
scol=j;
System.out.println();
System.out.println("Largest number:"+large);
System.out.println("row="+lrow);
System.out.println("column="+lcol);
System.out.println("Smallest number:"+small);
System.out.println("row="+srow);
System.out.println("column="+scol);
int s=0;
for(j=0;j<N;j++)
num1[s]=num[i][j];
s++;
for(j=0;j<((M*N)-i-1);j++)
if(num1[j]>num1[j+1])
temp=num1[j];
num1[j]=num1[j+1];
num1[j+1]=temp;
System.out.println("Rearraged matrix:");
s=0;
{
for(j=0;j<N;j++)
num[i][j]=num1[s];
System.out.print(num[i][j]+" ");
s++;
System.out.println();
else
System.out.println("Out of range");