0% found this document useful (0 votes)
21 views15 pages

Circular Prime Checker Program

AI

Uploaded by

xagopen600
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views15 pages

Circular Prime Checker Program

AI

Uploaded by

xagopen600
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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 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.

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.
Program
import [Link];
class ISC16Q1 {
int n;
String s;
boolean f;
public ISC16Q1(int num) {
n = num;
s = "";
f = true;
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
ISC16Q1 obj = new ISC16Q1(num);
[Link]();
[Link]();
}
boolean isPrime(int a) {
int c = 0;
for (int i = 1; i <= a; i++) {
if (a % i == 0) {
c++;
}
}
return c == 2;
}
int circulate(int a) {
String str = [Link](a);
String p = [Link](1) + [Link](0);
return [Link](p);
}
void process() {
int a = n;
do {
s += a + "\n";
if (!isPrime(a)) {
f = false;
}
a = circulate(a);
} while (a != n);
}
void display() {
[Link](s);
if (f) {
[Link](n + " IS A CIRCULAR PRIME.");
} else {
[Link](n + " IS NOT A CIRCULAR PRIME.");
}
}
}
Algorithm
Step 1: START
Step 2: INITIALIZE a Scanner object.
Step 3: PROMPT the user to enter a number.
Step 4: READ the number into a variable 'num'.
Step 5: CREATE an object of class ISC16Q1, passing 'num' to the constructor.
Step 6: CALL the process method to perform the circular prime check.
Step 7: In the process method, INITIALIZE an integer 'a' with the original number 'n'.
Step 8: START a DO-WHILE loop.
Step 9: APPEND the current value of 'a' to a string 's'.
Step 10: CALL the isPrime method with 'a' as an argument.
Step 11: In the isPrime method, COUNT the factors of the number.
Step 12: IF the factor count is 2, RETURN true.
Step 13: ELSE, RETURN false.
Step 14: IF isPrime returns false, SET the flag 'f' to false.
Step 15: CALL the circulate method with 'a' to get the next rotation.
Step 16: In the circulate method, CONVERT the number to a string.
Step 17: CREATE a new string by moving the first character to the end.
Step 18: CONVERT the new string back to an integer and RETURN it.
Step 19: UPDATE 'a' with the new circulated number.
Step 20: CONTINUE the loop UNTIL 'a' is equal to the original number 'n'.
Step 21: CALL the display method to show the result.
Step 22: In the display method, PRINT the generated numbers stored in 's'.
Step 23: IF the flag 'f' is true, DISPLAY that the number is a circular prime.
Step 24: ELSE, DISPLAY that the number is not a circular prime.
Step 25: STOP
Variable Description
Data Type Variable Name Description
int n Stores the number entered by the user.
String s Stores the sequence of generated numbers for display.
boolean f A flag to check if all rotations are prime.
int num Temporary variable in main to hold user input.
ISC16Q1 obj Object of the class.
int a Loop variable to hold the current number being checked.
int c Counter for factors in isPrime method.
int i Loop control variable in isPrime method.
String str Temporary string to hold number in circulate method.
String p Temporary string to hold the rotated number string.

Input/Output
Test 1
INPUT:
N = 197
OUTPUT:
197
971
719
197 IS A CIRCULAR PRIME.
Test 2
INPUT:
N = 1193
OUTPUT:
1193
1931
9311
3119
1193 IS A CIRCULAR PRIME.
Test 3
INPUT:
N = 29
OUTPUT:
29
92
29 IS NOT A CIRCULAR PRIME.
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:

1. Sort the non-boundary elements in ascending order using any standard sorting technique and
rearrange them in the matrix.

2. Calculate the sum of both the diagonals.

3. Display the original matrix, rearranged matrix and only the diagonal elements of the rearranged
matrix with their sum.

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
Example 2
INPUT:
M=5
7 4 1 9 5
8 2 6 10 19
13 1 3 5 1
10 0 5 12 16
1 8 17 6 8
OUTPUT:
ORIGINAL MATRIX
7 4 1 9 5
8 2 6 10 19
13 1 3 5 1
10 0 5 12 16
1 8 17 6 8
REARRANGED MATRIX
7 4 1 9 5
8 0 1 2 19
13 3 5 5 1
10 6 10 12 16
1 8 17 6 8

DIAGONAL ELEMENTS
7 5
0 2
5
6 12
1 8
SUM OF THE DIAGONAL ELEMENTS = 46
Example 3
INPUT:
M=3
OUTPUT:
THE MATRIX SIZE IS OUT OF RANGE.
Program
import [Link];
class ISC16Q2 {
int m;
int[][] a;
String o, r, d;
int s;
public ISC16Q2(int size) {
m = size;
a = new int[m][m];
o = "ORIGINAL MATRIX\n";
r = "REARRANGED MATRIX\n";
d = "DIAGONAL ELEMENTS\n";
s = 0;
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the size of the square matrix: ");
int size = [Link]();

if (size <= 3 || size >= 10) {


[Link]("THE MATRIX SIZE IS OUT OF RANGE.");
return;
}

ISC16Q2 obj = new ISC16Q2(size);


[Link](sc);
[Link]();
[Link]();
}
void input(Scanner sc) {
[Link]("Enter the elements of the Matrix:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = [Link]();
o += a[i][j] + "\t";
}
o += "\n";
}
}
void sortArray(int[] b) {
int n = [Link];
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (b[i] > b[j]) {
int temp = b[i];
b[i] = b[j];
b[j] = temp;
}
}
}
}
void process() {
int n = (m - 2) * (m - 2);
int[] b = new int[n];
int k = 0;

for (int i = 1; i < m - 1; i++) {


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

sortArray(b);
k = 0;

for (int i = 1; i < m - 1; i++) {


for (int j = 1; j < m - 1; j++) {
a[i][j] = b[k++];
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
r += a[i][j] + "\t";
if (i == j || (i + j) == m - 1) {
s += a[i][j];
d += a[i][j] + "\t";
} else {
d += "\t";
}
}
r += "\n";
d += "\n";
}
}
void display() {
[Link](o);
[Link](r);
[Link](d);
[Link]("SUM OF THE DIAGONAL ELEMENTS = " + s);
}
}
Algorithm
Step 1: START
Step 2: INITIALIZE a Scanner object.
Step 3: PROMPT user for the matrix size 'M'.
Step 4: READ 'M'.
Step 5: IF 'M' is not between 4 and 9 inclusive, DISPLAY error and STOP.
Step 6: CREATE an object of class ISC16Q2, passing 'M' to constructor.
Step 7: CALL the input method to fill the matrix.
Step 8: In the input method, PROMPT for matrix elements.
Step 9: USE nested FOR loops from i=0 to M-1 and j=0 to M-1.
Step 10: READ element into A[i][j].
Step 11: APPEND original matrix to a string 'o'.
Step 12: CALL the process method.
Step 13: In the process method, CALCULATE size of non-boundary array 'n'.
Step 14: CREATE a 1D array 'B' of size 'n'.
Step 15: USE nested FOR loops to TRAVERSE non-boundary elements of 'A'.
Step 16: COPY non-boundary elements from 'A' to 'B'.
Step 17: CALL sortArray method to sort array 'B' in ascending order.
Step 18: USE nested FOR loops to TRAVERSE non-boundary elements of 'A' again.
Step 19: COPY sorted elements from 'B' back to the non-boundary positions in 'A'.
Step 20: USE nested FOR loops to build the rearranged matrix string 'r' and diagonal elements string 'd'.
Step 21: CALCULATE the sum of diagonal elements and store in 's'.
Step 22: CALL the display method.
Step 23: In the display method, DISPLAY the original matrix string 'o'.
Step 24: DISPLAY the rearranged matrix string 'r'.
Step 25: DISPLAY the diagonal elements string 'd'.
Step 26: DISPLAY the sum of diagonals 's'.
Step 27: STOP
Variable Description
Data Type Variable Name Description
int m Stores the size of the square matrix.
int[][] a Stores the matrix elements.
String o Stores the original matrix for display.
String r Stores the rearranged matrix for display.
String d Stores the diagonal elements for display.
int s Stores the sum of the diagonal elements.
Scanner sc Scanner object for input.
int size Variable in main to hold user input for size.
int[] b 1D array to store and sort non-boundary elements.
int n Size of the 1D array 'b'.
int k Index for the 1D array 'b'.
int i, j Loop control variables.
int temp Temporary variable for sorting.

Input/Output
Test 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
Test 2
INPUT:
M=5
7 4 1 9 5
8 2 6 10 19
13 1 3 5 1
10 0 5 12 16
1 8 17 6 8
OUTPUT:
ORIGINAL MATRIX
7 4 1 9 5
8 2 6 10 19
13 1 3 5 1
10 0 5 12 16
1 8 17 6 8
REARRANGED MATRIX
7 4 1 9 5
8 0 1 2 19
13 3 5 5 1
10 6 10 12 16
1 8 17 6 8

DIAGONAL ELEMENTS
7 5
0 2
5
6 12
1 8
SUM OF THE DIAGONAL ELEMENTS = 46
Test 3
INPUT:
M=3
OUTPUT:
THE MATRIX SIZE IS OUT OF RANGE.
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:


(i) Find the number of words beginning and ending with a vowel.
(ii) Place the words which begin and end with a vowel at the beginning, followed by the remaining
words as they occur in the sentence.

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
Program
import [Link].*;
class ISC16Q3 {
String s, a, b;
int c;
boolean v;
public ISC16Q3(String sentence) {
s = sentence;
a = "";
b = "";
c = 0;
v = true;
}
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader([Link]);
BufferedReader br = new BufferedReader(isr);
[Link]("Enter a sentence: ");
String sentence = [Link]();
ISC16Q3 obj = new ISC16Q3(sentence);
[Link]();
[Link]();
}
boolean isVowel(char ch) {
return "AEIOU".indexOf(ch) != -1;
}
void process() {
s = [Link]();
char l = [Link]([Link]() - 1);

if (l != '.' && l != '?' && l != '!') {


v = false;
return;
}

s = [Link](0, [Link]() - 1).trim();


String[] words = [Link]("\\s+");

for (String w : words) {


if ([Link]() > 0 && isVowel([Link](0)) && isVowel([Link]([Link]() - 1))) {
a += w + " ";
c++;
} else {
b += w + " ";
}
}
}
void display() {
if (!v) {
[Link]("INVALID INPUT");
} else {
[Link]("NUMBER OF WORDS BEGINNING AND ENDING WITH A
VOWEL = " + c);
[Link]((a + b).trim());
}
}
}
Algorithm
Step 1: START
Step 2: INITIALIZE BufferedReader for input.
Step 3: PROMPT the user to enter a sentence.
Step 4: READ the full line into a string 'sentence'.
Step 5: CREATE an object of class ISC16Q3, passing 'sentence' to the constructor.
Step 6: CALL the process method.
Step 7: In the process method, CONVERT sentence 's' to upper case.
Step 8: GET the last character of the sentence.
Step 9: IF the last character is not '.', '?' or '!', SET validation flag 'v' to false and RETURN.
Step 10: REMOVE the last character and trim spaces from the sentence.
Step 11: SPLIT the sentence into words based on one or more spaces.
Step 12: FOR each word 'w' in the resulting array:
Step 13: IF the word is not empty:
Step 14: GET the first and last characters of the word.
Step 15: CALL isVowel for the first character AND the last character.
Step 16: In isVowel, CHECK if the character is 'A', 'E', 'I', 'O', or 'U'. RETURN true if it is, else false.
Step 17: IF both calls return true, APPEND word to string 'a' and INCREMENT counter 'c'.
Step 18: ELSE, APPEND word to string 'b'.
Step 19: ENDFOR
Step 20: CALL the display method.
Step 21: In the display method, IF validation flag 'v' is false, DISPLAY 'INVALID INPUT'.
Step 22: ELSE:
Step 23: DISPLAY the count of vowel-words 'c'.
Step 24: COMBINE strings 'a' and 'b', trim extra space, and DISPLAY the result.
Step 25: ENDIF
Step 26: STOP
Variable Description
Data Type Variable Name Description
String s Stores the input sentence.
String a Stores words that begin and end with a vowel.
String b Stores the remaining words.
int c Counts the number of words beginning and ending with a vowel.
boolean v Flag for input validation based on sentence terminator.
BufferedRe br Object for reading input from the console.
ader
String sentence Variable in main to hold user input.
char l Stores the last character of the sentence for validation.
String[] words Array to store the words of the sentence.
String w Represents a single word during iteration.
char ch Character being checked in the isVowel method.

Input/Output
Test 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
Test 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
Test 3
INPUT:
LOOK BEFORE YOU LEAP.
OUTPUT:
NUMBER OF WORDS BEGINNING AND ENDING WITH A VOWEL = 0
LOOK BEFORE YOU LEAP
Test 4
INPUT:
HOW ARE YOU@
OUTPUT:
INVALID INPUT

You might also like