Example

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Question:

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 I and itself.
Example:
131
311
113
Hence, 131 is a circular prime.
Test your program with the sample 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
Programming Code:
1 /**
2 * The class CircularPrime_Q1_ISC2016 inputs a number and
* checks whether it is a Circular Prime or not
3 * @author : www.guideforschool.com
4 * @Program Type : BlueJ Program - Java
5 * @Question Year : ISC Practical 2016 Question 1
6 */
7
8 import java.util.*;
class CircularPrime_Q1_ISC2016
9 {
10 boolean isPrime(int n) // Function for checking whether a number is prime or not
11 {
12 int c = 0;
for(int i = 1; i<=n; i++)
13 {
14 if(n%i == 0)
15 c++;
16 }
17 if(c == 2)
18 return true;
else
19 return false;
20 }
21
22 int circulate(int n) //Function for circulating the digits to form new number
23 {
String s = Integer.toString(n);
24 String p = s.substring(1)+s.charAt(0);
25 int a = Integer.parseInt(p);
26 return a;
27 }
28
29 void isCircularPrime(int n) //Function to check for circular prime
{
30 int f = 0,a = n;
31 do
32 {
33 System.out.println(a);
34 if(isPrime(a)==false)
{
35 f = 1;
36 break;
37 }
38 a = circulate(a);
}while(a!=n);
39
40
if(f==1)
41 System.out.println(n+" IS NOT A CIRCULAR PRIME");
42 else
43 System.out.println(n+" IS A CIRCULAR PRIME");
44 }
45
public static void main(String args[])
46 {
47 CircularPrime_Q1_ISC2016 ob = new CircularPrime_Q1_ISC2016();
48 Scanner sc = new Scanner(System.in);
49 System.out.print("Enter a number : ");
50 int n = sc.nextInt();
ob.isCircularPrime(n);
51 }
52 }
53
54
55
56
57
58
59
60
61
62
Question:
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.
<

p style=”text-align: justify;”>Test your program with the sample data and some random data:

Example 1
INPUT :M = 4
9 2 1 5
8 13 8 4
15 6 3 11
7 12 23 8
OUTPUT:
ORIGINAL MATRIX
9 2 1 5
8 13 8 4
15 6 3 11
7 12 23 8
REARRANGED MATRIX
9 2 1 5
8 3 6 4
15 8 13 11
7 12 23 8
DIAGONAL ELEMENTS
9 5
3 6
8 13
7 8
SUM OF THE DIAGONAL ELEMENTS = 59

Programming Code:
1 /**
2 * The class SortNonBoundary_ISC2016 inputs a square matrix and
* sorts the non-boundary elements in ascending order
3 * @author : www.guideforschool.com
4 * @Program Type : BlueJ Program - Java
5 * @Question Year : ISC Practical 2016 Question 2
6 */
7
8 import java.util.*;
class SortNonBoundary_ISC2016
9 {
10 int A[][],B[],m,n;
11
12 void input() //Function for taking all the necessary inputs
13 {
14 Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the square matrix : ");
15 m=sc.nextInt();
16 if(m<4 || m>10)
17 {
18 System.out.println("Invalid Range");
System.exit(0);
19 }
20 else
21 {
22 A = new int[m][m];
23 n = (m-2)*(m-2);
B = new int[n]; //Array to store Non-Boundary Elements
24
25 System.out.println("Enter the elements of the Matrix : ");
26 for(int i=0;i<m;i++)
27 {
28 for(int j=0;j<m;j++)
29 {
System.out.print("Enter a value : ");
30 A[i][j]=sc.nextInt();
31 }
32 }
33 }
}
34
35 /* The below function stores Non-Boundary elements
36 * from array A[][] to array B[] if s = 1
37 * else stores the Non-Boundary elements in array A[][] from array B[]
38 */
39 void convert(int s)
{
40 int x=0;
41 for(int i=0;i<m;i++)
42 {
43 for(int j=0;j<m;j++)
{
44 if(i != 0 && j != 0 && i != m-1 && j != m-1)
45 {
46 if(s==1)
47 B[x] = A[i][j];
48 else
A[i][j] = B[x];
49 x++;
50 }
51 }
52 }
}
53
54
void sortArray() //Function for sorting Non-Boundary elements stored in array B[]
55 {
56 int c = 0;
57 for(int i=0; i<n-1; i++)
58 {
for(int j=i+1; j<n; j++)
59 {
60 if(B[i]>B[j])
{
61 c = B[i];
62 B[i] = B[j];
63 B[j] = c;
64 }
}
65 }
66 }
67
68 void printArray() //Function for printing the array A[][]
69 {
70 for(int i=0;i<m;i++)
{
71 for(int j=0;j<m;j++)
72 {
73 System.out.print(A[i][j]+"\t");
74 }
System.out.println();
75 }
76 }
77
78 void printDiagonal() //Function for printing the diagonal elements and their sum
79 {
80 int sum = 0;
for(int i=0;i<m;i++)
81 {
82 for(int j=0;j<m;j++)
83 {
84 if(i==j || (i+j)==m-1)
85 {
System.out.print(A[i][j]+"\t");
86 sum = sum + A[i][j];
87 }
88 else
89 System.out.print("\t");
}
90 System.out.println();
91 }
92 System.out.println("Sum of the Diagonal Elements : "+sum);
93 }
94
95 public static void main(String args[])
{
96 SortNonBoundary_ISC2016 ob = new SortNonBoundary_ISC2016();
97 ob.input();
98 System.out.println("*********************");
99 System.out.println("The original matrix:");
System.out.println("*********************");
100 ob.printArray(); //Printing the original array
101 ob.convert(1); //Storing Non-Boundary elements to a 1-D array
102 ob.sortArray(); //Sorting the 1-D array (i.e. Non-Diagonal Elements)
103 ob.convert(2); //Storing the sorted Non-Boundary elements back to original 2
104
System.out.println("*********************");
105 System.out.println("The Rearranged matrix:");
106 System.out.println("*********************");
ob.printArray(); //Printing the rearranged array
107 System.out.println("*********************");
108 System.out.println("The Diagonal Elements:");
109 System.out.println("*********************");
110 ob.printDiagonal(); //Printing the diagonal elements and their sum
}
111 }
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
Question:
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:

(a) Find the number of words beginning and ending with a vowel.

(b) 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
Programming Code:
/**
1 * The class ISC2016_Q3 inputs a sentence, and prints and counts the words
2 * beginning and ending with a vowel, before other words
3 * @author : www.guideforschool.com
4 * @Program Type : BlueJ Program - Java
* @Question Year : ISC Practical 2016 Question 3
5
*/
6
7 import java.util.*;
8 class ISC2016_Q3
9 {
10 boolean isVowel(String w) // Function to check if a word begins and ends with a vo
{
11 int l = w.length();
12 char ch1 = w.charAt(0); // Storing the first character
13 char ch2 = w.charAt(l-1); // Storing the last character
14 if((ch1=='A' || ch1=='E' || ch1=='I' || ch1=='O' || ch1=='U') &&
15 (ch2=='A' || ch2=='E' || ch2=='I' || ch2=='O' || ch2=='U'))
{
16 return true;
17 }
18 else
{
19 return false;
20 }
21 }
22
23 public static void main(String args[])
{
24 ISC2016_Q3 ob = new ISC2016_Q3();
25 Scanner sc = new Scanner(System.in);
26
27 System.out.print("Enter a sentence : ");
28 String s = sc.nextLine();
29 s = s.toUpperCase();
int l = s.length();
30 char last = s.charAt(l-1); // Extracting the last character
31
32 /* Checking whether the sentence ends with '.' or '?' or not */
33 if(last != '.' && last != '?' && last != '!')
34 {
System.out.println("Invalid Input. End a sentence with either '.', '?' or
35 }
36 else
37 {
38 StringTokenizer str = new StringTokenizer(s," .?!");
39 int x = str.countTokens();
int c = 0;
40 String w = "", a = "", b = "";
41
42 for(int i=1; i<=x; i++)
43 {
44 w = str.nextToken(); // Extracting words and saving them in w
45
46 if(ob.isVowel(w))
{
47 c++; // Counting all words beginning and ending with a vowel
48 a = a + w + " "; // Saving all words beginning and ending with a
49 }
50 else
b = b + w + " "; // Saving all other words in variable 'b'
51 }
52 System.out.println("OUTPUT : \nNUMBER OF WORDS BEGINNING AND ENDING WITH
53 System.out.println(a+b);
54
55 }
56 }
}
57
58
59
60
61
62
63
64
65
66
67
68

You might also like