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

Assignment 5

Uploaded by

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

Assignment 5

Uploaded by

Aryan Jaiswal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

ASSIGNMENT 5: (23.04.

2024)
A class Highfact has been defined to find HCF and LCM of two no.s using
recursive technique. Find the HCF and LCM of n no.s. Some of the
members of Class are given below:
Class Name : HighFact
Data Members : int hcf, int lcm
Member Functions :
HighFact() : To assign initial values of the data members
void getdata() : To input n no.s
void minChange(int a,int b) : To swap a and b if a<b
void maxChange(int a,int b) : To swap a and b if a>b
int recHCF(int i) : To find the HCF using recursive technique
int recLCM(int a,int b,int i) : To find the LCM using recursive technique
void result() : To display the final HCF and LCM of n number
Algorithm:
1. Class Definition and Setup
 Define a class named HighFact with the following properties:

o Data Members:

 hcf and lcm: integers to store the highest common factor

(HCF) and least common multiple (LCM).


o Constructor HighFact(): Initialize hcf and lcm to 0.

2. Method getdata()
 Purpose: Collect user input for numbers, and calculate the HCF and

LCM.
 Steps:

1. Create a Scanner object sc to handle input.


2. Prompt the user to enter the number of elements n.
 If n is less than or equal to 0, print "INVALID INPUT!!" and

exit.
3. Create an integer array arr of size n to store the numbers.
4. Prompt the user to input each number and store them in the arr
array.
5. Initialize hcf and lcm with the first number (arr[0]).
6. For each remaining number arr[i] (from index 1 to n-1):
 Call minChange(hcf, arr[i]) to adjust the order of hcf and

arr[i] if needed.
 Call recHCF(arr[i]) to recursively calculate the HCF and store

it in hcf.
 Call recLCM(lcm, arr[i], 1) to recursively calculate the LCM

and store it in lcm.


3. Method minChange(int a, int b)
 Purpose: Adjust the order of two numbers a and b if a is less than b.

 Steps:

1. If a is less than b, swap a and b using a temporary variable temp.


 This ensures that a will always be the greater of the two

numbers.
4. Method maxChange(int a, int b)
 Purpose: Adjust the order of a and b if a is greater than b.

 Steps:

1. If a is greater than b, swap a and b using a temporary variable


temp.
2. Store the larger value in hcf.
5. Method recHCF(int i)
 Purpose: Recursively calculate the HCF between hcf and i.

 Steps:

1. If hcf equals i, return hcf as the HCF.


2. If hcf is greater than i, call maxChange(i, hcf - i) to adjust their
values.
 Repeat the recursive call with the updated values.

3. If hcf is less than i, decrement i by hcf and call recHCF(i) again.


4. Continue until hcf and i are equal, at which point hcf is returned
as the final HCF.
6. Method recLCM(int a, int b, int i)
 Purpose: Recursively find the LCM of a and b.

 Steps:

1. If i is divisible by both a and b, return i as the LCM.


2. Otherwise, increment i by 1 and recursively call recLCM(a, b, i).
7. Method result()
 Purpose: Display the final values of HCF and LCM.

 Steps:

1. Print the value of hcf.


2. Print the value of lcm.
8. Main Method
 Purpose: Execute the program by calling the necessary methods.

 Steps:

1. Create an instance obj of the HighFact class.


2. Call obj.getdata() to gather input and calculate HCF and LCM.
3. Call obj.result() to display the results.
Variable Listing Table:

Variable Type Scope Purpose


hcf int Instance (HighFact) Stores the Highest
Common Factor (HCF)
of the given numbers.
Initialized to 0 in the
constructor.
lcm int Instance (HighFact) Stores the Least
Common Multiple
(LCM) of the given
numbers. Initialized
to 0 in the
constructor.
sc Scanner Local (getdata Facilitates input
method) reading from the
console.
n int Local (getdata Stores the number of
method) elements (numbers)
provided by the user.
arr int[] Local (getdata Array to store the
method) numbers input by the
user.
i int Local (for loops) Used as a loop
counter in for loops
within methods.
temp int Local (minChange, Temporary variable
maxChange used for swapping
methods) two integers.
Method Listing Table:

Method Prototype Purpose

HighFact() HighFact() Constructor: Initializes hcf and


lcm to 0.
getdata() void getdata() Gets input from the user:
number of elements and the
numbers themselves. Initializes
hcf and lcm with the first
number, calculates HCF and
LCM for the rest.
minChange(int void minChange(int a, Swaps two integers if the first is
a, int b) int b) less than the second.

maxChange(int void maxChange(int a, Swaps two integers if the first is


a, int b) int b) greater than the second and
updates hcf.

recHCF(int i) int recHCF(int i) Recursively calculates the


Highest Common Factor (HCF).
recLCM(int a, int recLCM(int a, int b, Recursively calculates the Least
int b, int i) int i) Common Multiple (LCM).

result() void result() Displays the final values of HCF


and LCM calculated.

main(String[] public static void Entry point of the program.


args) main(String[] args) Creates an instance of HighFact,
gets data from the user,
calculates HCF and LCM, and
displays the result.
Class Listing Table:

Class Description
HighFact Represents a class for calculating the highest
common factor (HCF) and least common multiple
(LCM) of a set of numbers.
OUTPUT:

You might also like