Posts

Showing posts with the label recursion

Reverse Number ISC Computer Science 2022 Semester 2 Theory

Design a class Revno which reverses an integer number. Example : 94765 becomes 56749 on reversing the digits of the number. Some of the members of the class are given below: Class name : Revno Data members/instance variables : num: to store the integer number Member functions/methods : Revno(): default constructor void inputNum(): to accept the number int reverse(int n): returns the reverse of a number by using recursive technique void display(): displays the original number along with its reverse by invoking the method reverse() Specify the class Revno, giving details of the constructor, void inputNum(), int reverse(int) and void display(). Define the main() function to create an object and call the functions accordingly to enable the task. import java.util.Scanner; class Revno{     int num;     public Revno(){         num = 0;     }     public void inputNum(){         Scanner in = new Scanner(System.in)...

Calculate Series ISC Computer Science 2022 Semester 2 Theory

A class CalSeries has been defined to calculate the sum of the series: sum = 1 + x + x 2 + x 3 + ... + x n Some of the members of the class are given below: Class name : CalSeries Data members/instance variables : x: integer to store the value of x n: integer to store the value of n sum: integer to store the sum of the series Methods/Member functions : CalSeries(): default constructor void input(): to accept the values of x and n int power(int p, int q): return the power of p raised to q (p q ) using recursive technique void cal(): calculates the sum of the series by invoking the method power() and displays the result with an appropriate message Specify the class CalSeries, giving details of the constructor, void input(), int power(int, int) and void cal(). Define the main() function to create an object and call the member functions accordingly to enable the task. import java.util.Scanner; class CalSeries{     int x;     int n;     int sum;   ...

Binary Search ISC Computer Science 2020 Theory

Design a class BinSearch to search for a particular value in an array. Some of the members of the class are given below: Class name : BinSearch Data members/instance variables : arr[]: to store integer elements n: integer to store the size of the array Member functions/methods : BinSearch(int num): parameterized constructor to initialize n = num void fillArray(): to enter elements in the array void sort(): sorts the array elements in ascending order using any standard sorting technique int binSearch(int l, int u, int v): searches for the value 'v' using binary search and recursive technique and returns its location if found otherwise returns -1. Define the class BinSearch giving details of the constructor, void fillArray(), void sort(), and binSearch(int, int, int). Define the main() function to create an object and call the functions accordingly to enable the task. import java.util.Scanner; class BinSearch{     int arr[];     int n;     public BinSearc...

Decimal to Octal ISC Computer Science 2011 Theory

A class DeciOct has been defined to convert a decimal number into its equivalent octal number. Some of the members of the class are given below: Class name : DeciOct Data members/instance variables : n: stores the decimal number. oct: stores the octal equivalent number. Member functions : DeciOct(): constructor to initialize the data members n = 0, oct = 0. void getNum(int num): assign num to n. void deciOct(): calculates the octal equivalent of 'n' and stores it in 'oct' using the recursive technique. void show(): displays the decimal number 'n', calls the function deciOct() and displays its octal equivalent. (a) Specify the class, giving details of all the functions, including main() to create an object and call the functions accordingly to enable the task. (b) State any two disadvantages of using recursion. import java.util.Scanner; class DeciOct{     int n;     int oct;     public DeciOct(){         n = 0;        ...

Emirp Number ISC Computer Science 2013 Theory

An  emirp number  is a number which is prime backwards and forwards. Example: 13 and 31 are both prime numbers. Thus, 13 is an emirp number. Design a class Emirp to check if a given number is emirp number or not. Some of the members of the class are given below: Class name : Emirp Data members/instance variables : n: stores the number. rev: stores the reverse of the number. f: stores the divisor. Member functions : Emirp(int num): to assign n = num, rev = 0, and f = 2. int isPrime(int x): check if the number is prime using the recursive technique and return 1 if prime otherwise return 0. void isEmirp(): reverse the given number and check if both the original number and the reverse number are prime, by invoking the function isPrime(int) and display the result with an appropriate message. Specify the class Emirp, giving details of the functions. Define main() function to create an object and call the methods to check for Emirp number. import java.util.Scanner; class Emirp{ ...

Perfect Number ISC Computer Science 2018 Theory

Design a class  Perfect  to check if a given number is a  perfect number  or not. A number is said to be perfect if sum of the factors of the number excluding itself is equal to the original number. Example : 6 = 1 + 2 + 3 (where 1, 2 and 3 are factors of 6, excluding itself) Some of the members of the class are given below: Class name : Perfect Data members/instance variables : num: to store the number Methods/Member functions : Perfect(int n): parameterized constructor to initialize the data member num = n int sumOfFactors(int i): returns the sum of the factors of the number num, excluding itself, using recursive technique void check(): checks whether the given number is perfect by invoking the function sumOfFactors() and displays the result with an appropriate message Specify the class Perfect giving details of the constructor, int sumOfFactors(int) and void check(). Define a main() function to create an object and call the functions accordingly to enable the task...

Palindrome Number ISC Computer Science 2017 Theory

A class  Palin  has been defined to check whether a positive number is a  Palindrome number  or not. The number 'N' is palindrome if the original number and its reverse are same. Some of the members of the class are given below: Class name : Palin Data members/instance variables : num: integer to store the number revNum: integer to store the reverse of the number Methods/Member functions : Palin(): constructor to initialize data members with legal initial values void accept(): to accept the number int reverse(int y): reverses the parameterized argument 'y' and stores it in 'revNum' using recursive technique void check(): checks whether the number is a palindrome by invoking the function reverse() and display the result with an appropriate message. Specify the class Palin giving the details of the constructor, void accept(), int reverse(int) and void check(). Define the main() function to create an object and call the functions accordingly to enable the task. imp...