Solved ProgramsISCClass 12Computer Science

7 ISC Practical Programs with Solutions: Circular Prime, Prime-Adam and More

Seven ISC Computer Science practical exam programs with Java solutions: circular prime, consecutive number combinations, Prime-Adam numbers, two matrix programs and two sentence-processing programs. Free PDF with a step-by-step dry run.

7 min read
7 ISC Practical Programs with Solutions: Circular Prime, Prime-Adam and More

The ISC Computer Science practical exam gives you a choice of three problems, and you solve one: write the algorithm, code it in Java and test it. The problems follow a small set of patterns: special numbers, matrices and sentence processing. This set of seven practice programs with solutions covers all three.

Programs included

  1. Circular Prime: check whether a number stays prime under every rotation of its digits, and print each rotation.
  2. Consecutive number combinations: print every run of consecutive natural numbers that adds up to N. For example, 15 = 7 + 8 = 4 + 5 + 6 = 1 + 2 + 3 + 4 + 5.
  3. Prime-Adam numbers: find numbers that are both prime and Adam. In an Adam number, the square of the number and the square of its reverse are reverses of each other: 13² = 169 and 31² = 961.
  4. Octal matrix: an M × N matrix where each row holds the digits of an octal number. The rows are converted to decimal, with validation of M, N and the digits 0–7.
  5. Sort words by length: check the sentence terminator, then arrange the words by length, alphabetically when two words have the same length.
  6. Matrix maximum, minimum and sorting: find the largest and smallest elements with their positions, then sort the matrix.
  7. Vowel words: count the words that begin and end with a vowel when words may be separated by several spaces, and rearrange the sentence.

Every program in the PDF comes with a complete Java solution.

Understanding the circular prime rotation

The key step is rotating the digits: the leftmost digit is moved to the end. For a number with d digits, take divisor = 10^(d−1):

int first = n / divisor;          // leftmost digit
int rest  = n % divisor;          // remaining digits
n = rest * 10 + first;            // move first digit to the end

Dry run for N = 197 (d = 3, divisor = 100):

n      first  rest  new n   prime?
197    1      97    971     yes
971    9      71    719     yes
→ 197, 971, 719 are all prime, so 197 IS A CIRCULAR PRIME.

For N = 29: 29 is prime, but its rotation 92 is not, so 29 is not a circular prime.

Checklist for the practical exam

  • Validate the input first. Most ISC problems specify an invalid-input message, such as "INVALID INPUT" for a negative number or a matrix size out of range.
  • Match the output format exactly, including capital letters and the order of the lines.
  • Test with every sample input, then with edge cases such as 0, a single-digit number or a one-word sentence.
  • Write the algorithm in numbered steps before coding. It carries marks of its own.

Revise number logic with our ISC Class 12 recursion programs before you start, and see the ISC Computer Science Practical Paper 2021 for a real exam paper.

Download the paper

ISC Practical Practice Programs with Solutions

PDF · 69 KB

Download PDF
Read the questions as text
1. A Circular Prime is a prime number that remains prime under cyclic shifts of its digits. When the leftmost digit is removed and replaced at the end of the remaining string of digits, the generated number is still prime. The process is repeated until the original number is reached again. A number is said to be prime if it has only two factors 1 and itself. Example: 131 311 113 Hence, 131 is a circular prime. Accept a positive number N and check whether it is a circular prime or not. The new numbers formed after the shifting of the digits should also be displayed. Test your program with the following data and some random data: Example 1 INPUT: N = 197 OUTPUT: 197 971 719 197 IS A CIRCULAR PRIME. Example 2 INPUT: N = 1193 OUTPUT: 1193 1931 9311 3119 1193 IS A CIRCULAR PRIME. Example 3 INPUT: N = 29 OUTPUT: 29 92 29 IS NOT A CIRCULAR PRIME. Solution import java.util.Scanner; public class CircularPrime { public static boolean isPrime(int num) { int c = 0; for (int i = 1; i <= num; i++) { if (num % i == 0) { c++; } } return c == 2; } public static int getDigitCount(int num) { int c = 0; while (num != 0) { c++; num /= 10; } return c; } public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.print("ENTER INTEGER TO CHECK (N): "); int n = in.nextInt(); if (n <= 0) { System.out.println("INVALID INPUT"); return; } boolean isCircularPrime = true; if (isPrime(n)) { System.out.println(n); int digitCount = getDigitCount(n); int divisor = (int)(Math.pow(10, digitCount - 1)); int n2 = n; for (int i = 1; i < digitCount; i++) { int t1 = n2 / divisor; int t2 = n2 % divisor; n2 = t2 * 10 + t1; System.out.println(n2); if (!isPrime(n2)) { isCircularPrime = false; break; } } } else { isCircularPrime = false; } if (isCircularPrime) { System.out.println(n + " IS A CIRCULAR PRIME."); } else { System.out.println(n + " IS NOT A CIRCULAR PRIME."); } } } 2. Write a program which inputs a positive natural number N and prints the possible consecutive number combinations, which when added give N. Test your program for the following data and some random data.. A positive natural number, (for e.g 27) can be represented as follows2+3+4+5+6+7 8+9+10 13+14 Every row represents a combination of consecutive natural numbers, which add up to 27. SAMPLE DATA INPUT N=9 OUTPUT 45 234 INPUT N = 15 OUTPUT 78 12345 456 import java.io.*; class NumberCombination { int no,sum=0,k,j,i; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); public void takeNumber()throws Exception { System.out.println("Enter the number:"); no=Integer.parseInt(br.readLine()); for(i=1;i<=no/2+1;i++) { sum=0; for(j=i;j<=no/2+1;j++) { sum=sum+j; if (sum==no) break; } if(j<=no/2+1) { for(k=i;k<=j;k++) System.out.print(" "+k); System.out.println(); } } } public static void main(String args[])throws Exception { NumberCombination obj=new NumberCombination (); obj.takeNumber(); } } 3. A Prime-Adam integer is a positive integer (without leading zeros) which is a prime as well as an Adam number. Prime number: A number which has only two factors, i.e. 1 and the number itself. Example: 2, 3, 5, 7 ... etc. Adam number: The square of a number and the square of its reverse are reverse to each other. Example: If n = 13 and reverse of 'n' = 31, then, (13)2 = 169 (31)2 = 961 which is reverse of 169 thus 13, is an Adam number. Accept two positive integers m and n, where m is less than n as user input. Display all Prime-Adam integers that are in the range between m and n (both inclusive) and output them along with the frequency, in the format given below: Test your program with the following data and some random data: Example 1 INPUT: m=5 n = 100 OUTPUT: THE PRIME-ADAM INTEGERS ARE: 11 13 31 FREQUENCY OF PRIME-ADAM INTEGERS IS: 3 Example 2 INPUT: m = 100 n = 200 OUTPUT: THE PRIME-ADAM INTEGERS ARE: 101 103 113 FREQUENCY OF PRIME-ADAM INTEGERS IS: 3 Example 3 INPUT: m = 50 n = 70 OUTPUT: THE PRIME-ADAM INTEGERS ARE: NIL FREQUENCY OF PRIME-ADAM INTEGERS IS: 0 Example 4 INPUT: m = 700 n = 450 OUTPUT: INVALID INPUT Solution import java.util.Scanner; public class PrimeAdam { public static int reverse(int num) { int rev = 0; while (num != 0) { int d = num % 10; rev = rev * 10 + d; num /= 10; } return rev; } public static boolean isAdam(int num) { int sqNum = num * num; int revNum = reverse(num); int sqRevNum = revNum * revNum; int rev = reverse(sqNum); return rev == sqRevNum; } public static boolean isPrime(int num) { int c = 0; for (int i = 1; i <= num; i++) { if (num % i == 0) { c++; } } return c == 2; } public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.print("Enter the value of m: "); int m = in.nextInt(); System.out.print("Enter the value of n: "); int n = in.nextInt(); int count = 0; if (m >= n) { System.out.println("INVALID INPUT"); return; } System.out.println("THE PRIME-ADAM INTEGERS ARE:"); for (int i = m; i <= n; i++) { boolean adam = isAdam(i); if (adam) { boolean prime = isPrime(i); if (prime) { System.out.print(i + " "); count++; } } } if (count == 0) { System.out.print("NIL"); } System.out.println(); System.out.println("FREQUENCY OF PRIME-ADAM INTEGERS IS: " + count); } } 4. Write a program to declare a matrix A[][] of order (M x N) where 'M' is the number of rows and 'N' is the number of columns such that the value of 'M' must be greater than 0 and less than 10 and the value of 'N' must be greater than 2 and less than 6. Allow the user to input digits (0 7) only at each location, such that each row represents an octal number. Example: 2 3 1 (decimal equivalent of 1st row = 153 i.e. 2x82 + 3x81 + 1x80) 4 0 5 (decimal equivalent of 2nd row = 261 i.e. 4x82 + 0x81 + 5x80) 1 5 6 (decimal equivalent of 3rd row = 110 i.e. 1x82 + 5x81 + 6x80) Perform the following tasks on the matrix: Display the original matrix. Calculate the decimal equivalent for each row and display as per the format given below. Test your program for the following data and some random data: Example 1: INPUT: M=1 N=3 ENTER ELEMENTS FOR ROW 1: 1 4 4 OUTPUT: FILLED MATRIX 1 4 4 DECIMAL EQUIVALENT 100 Example 2: INPUT: M=3 N=4 ENTER ELEMENTS FOR ROW 1: 1 1 3 7 ENTER ELEMENTS FOR ROW 2: 2 1 0 6 ENTER ELEMENTS FOR ROW 3: 0 2 4 5 OUTPUT: FILLED MATRIX 1 1 3 2 1 0 0 2 4 DECIMAL EQUIVALENT 7 607 6 1094 5 165 Example 3: INPUT: M=3 N=3 ENTER ELEMENTS FOR ROW 1: 2 4 8 OUTPUT: INVALID INPUT Example 4: INPUT: M=4 N=6 OUTPUT: OUT OF RANGE Solution import java.util.Scanner; public class OctalMatrix { public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.print("Enter the number of rows (M): "); int m = in.nextInt(); System.out.print("Enter the number of columns (N): "); int n = in.nextInt(); if (m <= 0 || m >= 10 || n <= 2 || n >= 6) { System.out.println("OUT OF RANGE"); return; } int a[][] = new int[m][n]; for (int i = 0; i < m; i++) { System.out.println("ENTER ELEMENTS FOR ROW " + (i + 1) + ": "); for (int j = 0; j < n; j++) { a[i][j] = in.nextInt(); if (a[i][j] < 0 || a[i][j] > 7) { System.out.println("INVALID INPUT"); return; } } } System.out.println("FILLED MATRIX\tDECIMAL EQUIVALENT"); for (int i = 0; i < m; i++) { int decNum = 0; for (int j = 0; j < n; j++) { decNum += a[i][j] * Math.pow(8, n - j - 1 ); System.out.print(a[i][j] + " "); } System.out.print("\t\t" + decNum); System.out.println(); } } } 5. Write a program to accept a sentence which may be terminated by either '.', '?' or '!' only. The words are to be separated by a single blank space and are in UPPER CASE. Perform the following tasks: Check for the validity of the accepted sentence only for the terminating character. Arrange the words in ascending order of their length. If two or more words have the same length, then sort them alphabetically. Display the original sentence along with the converted sentence. Test your program for the following data and some random data: Example 1: INPUT: AS YOU SOW SO SHALL YOU REAP. OUTPUT: AS YOU SOW SO SHALL YOU REAP. AS SO SOW YOU YOU REAP SHALL Example 2: INPUT: SELF HELP IS THE BEST HELP. OUTPUT: SELF HELP IS THE BEST HELP. IS THE BEST HELP HELP SELF Example 3: INPUT: BE KIND TO OTHERS. OUTPUT: BE KIND TO OTHERS. BE TO KIND OTHERS Example 4: INPUT: NOTHING IS IMPOSSIBLE# OUTPUT: INVALID INPUT Solution import java.util.*; public class StringCheck { public static String sortString(String ipStr) { StringTokenizer st = new StringTokenizer(ipStr); int wordCount = st.countTokens(); String strArr[] = new String[wordCount]; for (int i = 0; i < wordCount; i++) { strArr[i] = st.nextToken(); } for (int i = 0; i < wordCount - 1; i++) { for (int j = 0; j < wordCount - i - 1; j++) { if (strArr[j].length() > strArr[j + 1].length()) { String t = strArr[j]; strArr[j] = strArr[j+1]; strArr[j+1] = t; } if (strArr[j].length() == strArr[j + 1].length() &&(strArr[j].compareTo(strArr[j+1]) > 0)) { String t = strArr[j]; strArr[j] = strArr[j+1]; strArr[j+1] = t; } } } StringBuffer sb = new StringBuffer(); for (int i = 0; i < wordCount; i++) { sb.append(strArr[i]); sb.append(" "); } /* * trim will remove the extra space added * to the end of the string in the loop above */ return sb.toString().trim(); } public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.println("Enter a sentence:"); String str = in.nextLine(); int len = str.length(); System.out.println(); if (str.charAt(len - 1) != '.' && str.charAt(len - 1) != '?' && str.charAt(len - 1) != '!') { System.out.println("INVALID INPUT"); return; } /* * str.substring(0, len - 1) removes the * '.', '?', '!' at the end of the string */ String sortedStr = sortString(str.substring(0, len - 1)); System.out.println(str); 6. 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: Display the input matrix Find the maximum and minimum value in the matrix and display them along with their position. Sort the elements of the matrix in ascending order using any standard sorting technique and rearrange them in the matrix. Output the rearranged matrix. Test your program with the sample data and some random data: Example 1. INPUT: M=3 N=4 Entered values: 8,7,9,3,-2,0,4,5,1,3,6,-4 OUTPUT: Original matrix: 8793 -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 3345 6789 import java.io.*; class Matrix { public void take()throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int arr[][],m,n; int g,h,i,j,max,min,maxr,maxc,minr,minc; while(true) { System.out.println(“\nEnter the number of rows :”); m=Integer.parseInt(br.readLine()); System.out.println(“\nEnter the number of columns:”);n=Integer.parseInt(br.readLine()); if(m<2 || n<2 || m>20 || n>20) System.out.println(“\nEnter the number of rows and columns”); else break; } arr=new int[m][n]; for(i=0;i<m; i++) { for(j=0;j<n;j++) { System.out.println(“\nEnter Value:”); arr[i][j]=Integer.parseInt(br.readLine()); } } max=arr[0][0]; min=arr[0][0]; maxr=0; minr=0; maxc=0; minc=0; for(i=0;i<m;i++) { for(j=0;j<n;j++) { if(arr[i][j]>max) { max=arr[i][j]; maxr=i; maxc=j; } else if(arr[i][j]< min) { minr=i; minc=j; min=arr[i][j]; } } } System.out.println(“\nOriginal Matrix\n”); for(i=0;i<m;i++) { for(j=0;j<n;j++) { System.out.print(arr[i][j]+” “); } System.out.println(); } System.out.println(“\nMaximum Value=”+max); System.out.println(“\nRow=”+maxr); System.out.println(“\nColumn=”+maxc); System.out.println(“\nMinimum Value=”+min); System.out.println(“\nRow=”+minr); System.out.println(“\nColumn=”+minc); for(g=0;g<m;g++) { for(h=0;h<n;h++) { for(i=0;i<m;i++) { for(j=0;j<n;j++) { if(arr[g][h]< arr[i][j]) { min=arr[g][h]; arr[g][h]=arr[i][j]; arr[i][j]=min; } } } } } System.out.println(“\nSorted Array\n”); for(i=0;i<m;i++) { for(j=0;j<n;j++) { System.out.print(arr[i][j]+” “); } System.out.println(); } } public static void main(String args[]) throws Exception { Matrix ob=new Matrix(); ob.take(); } 7. Write a program to accept a sentence which may be terminated by either '.', '?' or '!' only. The words may be separated by more than one blank space and are in UPPER CASE. Perform the following tasks: Find the number of words beginning and ending with a vowel. Place the words which begin and end with a vowel at the beginning, followed by the remaining words as they occur in the sentence. Test your program with the sample data and some random data: Example 1 INPUT: ANAMIKA AND SUSAN ARE NEVER GOING TO QUARREL ANYMORE. OUTPUT: NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 3 ANAMIKA ARE ANYMORE AND SUSAN NEVER GOING TO QUARREL Example 2 INPUT: YOU MUST AIM TO BE A BETTER PERSON TOMORROW THAN YOU ARE TODAY. OUTPUT: NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 2 A ARE YOU MUST AIM TO BE BETTER PERSON TOMORROW THAN YOU TODAY Example 3 INPUT: LOOK BEFORE YOU LEAP. OUTPUT: NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 0 LOOK BEFORE YOU LEAP Example 4 INPUT: HOW ARE YOU@ OUTPUT: INVALID INPUT import java.util.*; public class Sentence_2 { public static void main() { Scanner in=new Scanner(System.in); String s,w[],v="",nv="",str; char f,l; inti,count=0; System.out.println("Enter Your Sentence : "); s=in.nextLine(); if((s.charAt(s.length()-1)=='?')||(s.charAt(s.length()-1)=='.')||(s.charAt(s.length()-1)=='!')) { w=s.split("[.?! ]+"); for(i=0;i<w.length;i++) { f=w[i].charAt(0); l=w[i].charAt(w[i].length()-1); if(((f=='a')||(f=='e')||(f=='i')||(f=='o')||(f=='u')||(f=='A')||(f=='E')||(f=='I')||(f=='O')||(f=='U'))&&((l=='a')||(l =='e')||(l=='i')||(l=='o')||(l=='u')||(l=='A')||(l=='E')||(l=='I')||(l=='O')||(l=='U'))) { count++; v=v+w[i]+" "; } else { nv=nv+w[i]+" "; } } str=v.trim()+" "+nv.trim(); str=str.trim(); System.out.println("NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL : "+count); System.out.println("NEW STRING : \n"+str); } else System.out.println("INVALID INPUT"); } }