Assignment 5
Assignment 5
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:
2. Method getdata()
Purpose: Collect user input for numbers, and calculate the HCF and
LCM.
Steps:
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
Steps:
numbers.
4. Method maxChange(int a, int b)
Purpose: Adjust the order of a and b if a is greater than b.
Steps:
Steps:
Steps:
Steps:
Steps:
Class Description
HighFact Represents a class for calculating the highest
common factor (HCF) and least common multiple
(LCM) of a set of numbers.
OUTPUT: