C Programs Tutorials | IT Developer
IT Developer

Java Programs - Solved 2023 Theory Paper ISC Computer Science



Share with a Friend

Solved 2023 Thory Paper ISC Computer Science

Class 12 - ISC Computer Science Solved Theory Papers

Pronic Number in Java using Recursion - ISC 2023 Theory

Design a class Pronic to check if a given number is a pronic number or not. A number is said to be pronic if the product of two consecutive numbers is equal to the number.

 

Example:
0 = 0 × 1
2 = 1 × 2
6 = 2 × 3
12 = 3 × 4


Thus, 0, 2, 6, 12 are pronic numbers.

 

Some of the members of the class are given below:

Class name: Pronic

Data members/instance variables:
num: to store a positive integer number

 

Methods/Member functions:
Pronic(): default constructor to initialize the data member with legal initial value
void acceptNum(): to accept a positive integer number
boolean isPronic(int v): returns true if the number ‘num’ is a pronic number, otherwise returns false using recursive technique
void check(): checks whether the given number is a pronic number by invoking the function isPronic() and displays the result with an appropriate message

 

Specify the class Pronic giving details of the constructor, void acceptNum(), boolean isPronic() and void check(). Define a main() function to create an object and call the functions accordingly to enable the task.

import java.util.Scanner; class Pronic{ int num; public Pronic(){ num = 0; } public void acceptNum(){ Scanner in = new Scanner(System.in); System.out.print("Enter the number: "); num = Math.abs(Integer.parseInt(in.nextLine())); } public boolean isPronic(int v){ if(v * (v - 1) == num) return true; if(v > 1) return isPronic(v - 1); return false; } public void check(){ if(isPronic(num)) System.out.println(num + " is a pronic number"); else System.out.println(num + " is not a pronic number"); } public static void main(String args[]){ Pronic obj = new Pronic(); obj.acceptNum(); obj.check(); } }

Output

 
 
 OUTPUT 1: 
Enter the number: 2
2 is a pronic number

 OUTPUT 2: 
Enter the number: 4
4 is not a pronic number

 OUTPUT 3: 
Enter the number: 12
12 is a pronic number