ISC Computer Science Practical 2012
Question 1
A prime palindrome integer is a positive integer (without leading zeros) which is prime as well 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
OUTPUT: The prime palindrome integers are:
101,131,151,181,191,313,351,373,383,727,757,787,797,919,929
Frequency of prime palindrome integers: 15
Example 2:
INPUT:
M=100
N=5000
OUTPUT: Out of range
solution
import java.io.*; public class PrimePalindrome { public static void main(String args[])throws Exception { BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter the lower limit:"); int m=Integer.parseInt(stdin.readLine()); System.out.print("Enter the upper limit:"); int n=Integer.parseInt(stdin.readLine()); int count=0,flag; int i2;//stores the value of i to i2 int r;//stores the reversed number if(m<n)//checks whether lower limit less than upper limit { if((m<3000)&&(n<3000)) { System.out.println("The prime palindrome integers are:"); 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; while(i2>0)//reverses the number { r=(r*10)+(i2%10); i2=i2/10; } if((flag==0)&&(r==i)) { if(count>0)//to avoid comma(,) after the last number 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:
1. Obtain the length of the sentence (measured in words)
2. Arrange the sentence in alphabetical order of the words.
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.*; public class SenAlpha
{ public static void main(String args[])throws Exception { BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter the sentence:"); String s=stdin.readLine(); s=s.toUpperCase();//converts the sentence to uppercase. int l=s.length(); char c=s.charAt(l-1);//extracts the last character 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); String word[]= new String[length]; for(int j=0;j<l;j++) //to store each word in array { ch=s.charAt(j); if(ch==' '||ch==c) { word[w]=s.substring(pos,j); w++; pos=j+1; } } for(int k=0;k<length;k++)//arranges the word in alphabhetical order 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:"); for(int r=0;r<length;r++)//prints the rearranged sentence System.out.print(word[r]+" "); } else System.out.print("Invalid input"); } }
Question 3
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: 1. Display the input matrix 2. Find the maximum and minimum value in the matrix and display them along with their position. 3. Sort the elements of the matrix in ascending order using any standard sorting technique and rearrange them in the matrix. Output the rearranged matrix. Sample input Output INPUT: M=3 N=4 Entered values: 8,7,9,3,-2,0,4,5,1,3,6,-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.*; public class Matrix
{ public static void main(String args[])throws Exception { BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter the number of rows:"); int M=Integer.parseInt(stdin.readLine()); System.out.print("Enter the number of columns:"); 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) { int num[][] = new int[M][N]; System.out.print("Enter the numbers:"); for( i=0;i<M;i++)//to enter the elements 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( i=0;i<M;i++) //to diplay matrix and find largest ad smallest number { 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 num1[]=new int[M*N]; int s=0; for(i=0;i<M;i++)//storing the elements into single dimensional array for(j=0;j<N;j++) { num1[s]=num[i][j]; s++; } for(i=0;i<(M*N);i++) //sorting using bubble sort 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(i=0;i<M;i++)//storing the elements back to double dimensional and printing { 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"); } }