0% found this document useful (0 votes)
18 views

Java With BlueJ 12 BOARD PROJECTT

ISC GRADE 12 COMPUTER SCIENCE

Uploaded by

shahprithvi01
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)
18 views

Java With BlueJ 12 BOARD PROJECTT

ISC GRADE 12 COMPUTER SCIENCE

Uploaded by

shahprithvi01
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
You are on page 1/ 8

Java

With
BlueJ
INDEX
Question 1
Design a class PrimePalinGen to generate prime palindrome numbers.
[A number is said to be prime palindrome of the number is a prime as well as
palindrome number]
[Prime number : A number which is divisible by 1 and itself.]
[Palindrome number : A number which is same as its reverse]
Example: 11 (where 11 is a prime number and a palindrome number) Some of the
members of the class are given below:
Class name : PrimePalinGen
Data members/instance variables:
start : to store the start of range
end : to store the end of range
Methods/member functions:
PrimePalinGen(int a, int b) : parametrized constructor to inilialise the
data members.
int isPrime(int i) : returns 1 if the number is prime otherwise
return 0. int isPalin(int i) : returns 1 if the number is prime otherwise return 0.
void generate() : generates all prime palindrome numbers
between start and end by invoking the functions isPrime() and isPalin().
Specify the class PrimePalinGen giving details of the constructor(), int
isPrime(int), int isPalin(int) and void generate(). Define a main() function to create
an object and call the functions accordingly to enable the task.

Solution:
mport java.util.*;

class PrimePalinGen //methods to check if a number is prime or palindrome and to


generate prime palindrome numbers.
{
int start, end;

PrimePalinGen(int a, int b)
{
start = a;
end = b;
}

int isPrime(int num) //checks if a number is prime by iterating up to the square


root of the number.
{
if (num <= 1)
return 0;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0)
return 0;
}
return 1;
}

int isPalin(int num) //checks if a number is a palindrome by reversing it and


comparing it with the original.
{
int original = num;
int reverse = 0;
while (num != 0) {
int remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
if (original == reverse)
return 1;
else
return 0;
}
void generate() //generates prime palindrome numbers within the specified
range
{
System.out.println("Prime palindrome numbers between " + start + " and " +
end + " are:");
for (int i = start; i <= end; i++) {
if (isPrime(i) == 1 && isPalin(i) == 1)
System.out.print(i + " ");
}
System.out.println();
}
}
public class Main //user input is taken for the range, and the generate method is
called to display prime palindrome numbers within that range.
{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the range (start and end):");
int start = scanner.nextInt();
int end = scanner.nextInt();
PrimePalinGen obj = new PrimePalinGen(start, end);
obj.generate();
scanner.close();
}
}
Variable Description:
1. start: Starting point of the range.
2. end: Ending point of the range.
3. num: Number being checked for prime or palindrome.
4. original: Original number being checked for palindrome.
5. reverse: Reversed number for palindrome comparison.
6. i: Loop variable for iteration.
7. obj: Object of the PrimePalinGen class.

Algorithm:
1. Define a class PrimePalinGen with instance variables start and end.
2. Implement a parameterized constructor to initialize start and end.
3. Implement methods isPrime and isPalin to check if a number is prime and
palindrome, respectively.
4. Define a method generate to generate all prime palindrome numbers between start and
end.
5. In the main function, create an object of the class PrimePalinGen, take input for range
from the user, and call the generate method.

Question 2
A Prime-Adam integer is a positive integer (without leading zeros) which is a prirne 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.
Exarnple: 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:
Solution:
import java.util.Scanner;

public class PrimeAdamNumbers {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.print("Enter the value of m: ");


int m = scanner.nextInt();
System.out.print("Enter the value of n: ");
int n = scanner.nextInt();

int count = 0;

System.out.println("Prime-Adam numbers between " + m + " and " + n + ":");

for (int i = m; i <= n; i++) {


if (isPrime(i) && isAdamNumber(i)) {
System.out.println(i);
count++;
}
}

System.out.println("Frequency of Prime-Adam numbers: " + count);


}

// Method to check if a number is prime


public static boolean isPrime(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
// Method to check if a number is an Adam number
public static boolean isAdamNumber(int num) {
int reversedNum = reverseNumber(num);
int squareOriginal = num * num;
int squareReversed = reversedNum * reversedNum;
return squareOriginal == reverseNumber(squareReversed);
}

// Method to reverse a number


public static int reverseNumber(int num) {
int reversed = 0;
while (num != 0) {
reversed = reversed * 10 + num % 10;
num /= 10;
}
return reversed;
}
}
Algorithm:
 Input Handling: The program starts by taking two integers, m and n, as input from the
user.
 Main Logic: It iterates through all the numbers in the range [m, n].
 Prime Check: For each number, it checks if the number is prime using the isPrime method.
 Adam Number Check: If the number is prime, it then checks if the number is an Adam
number using the isAdamNumber method.
 Reversing Numbers: The isAdamNumber method uses the reverseNumber method to reverse
the digits of a number.
 Output: The program prints all the Prime-Adam numbers and finally prints their
frequency.

Variable Description:

You might also like