Pronic Number ISC Specimen 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();
    }
}

Comments

Popular posts from this blog

Encrypt Program ISC Specimen 2023 Theory

No Repeat Program ISC Computer Science 2022 Semester 2 Theory

Bank Inheritance Program ISC Specimen 2023 Theory