Question

Write a C++ class to compute the GCD and LCM. The class should be able to...

Write a C++ class to compute the GCD and LCM.
The class should be able to store the two numbers to compute their GCD and LCM.
The class should have the following methods:
- void setNumbers(int, int);
- int getGCD(void);
- int getLCM(void);

You may use a constructor instead of setNumbers method.

Also write a simple C++ program that will implement the class above.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
using namespace std;
class gcdlcm{
private:
int a,b;
// Default Constructor
public:
gcdlcm(int a1,int b1)
{
//assign values using Constructor
a=a1;
b=b1;
}
int getGCD()
{
int gcd;
//swap if b is greater
if (b>a) {   
int temp =b;
b=a;
a=temp;
}
//if both are divisible by number it is gcd
for(int i=1;i<=b;i++) {
if (a%i==0&& b%i==0) {
gcd=i;
}
  
}
return gcd;
}
int getLCM()
{
//formula for LCM
return (a*b)/getGCD();
}
};
int main()
{
gcdlcm g(4,6);
cout<<"The GCD of 4 and 6 is "<<g.getGCD()<<endl;
cout<<"The LCM of 4 and 6 is "<<g.getLCM()<<endl;
return 1;
}
The GCD of 4 and 6 is 2 The LCM of 4 and 6 is 12

Add a comment
Know the answer?
Add Answer to:
Write a C++ class to compute the GCD and LCM. The class should be able to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please help me with this code. Thank you Implement the following Java class: Vehicle Class should...

    Please help me with this code. Thank you Implement the following Java class: Vehicle Class should contain next instance variables: Integer numberOfWheels; Double engineCapacity; Boolean isElectric, String manufacturer; Array of integers productionYears; Supply your class with: Default constructor (which sets all variables to their respective default values) Constructor which accepts all the variables All the appropriate getters and setters (you may skip comments for this methods. Also, make sure that for engineCapacity setter method you check first if the vehicle...

  • The least common multiple (lcm) of two positive integers u and v is the smallest positive...

    The least common multiple (lcm) of two positive integers u and v is the smallest positive integer that is evenly divisible by both u and v. Thus, the lcm of 15 and 10, written lcm (15,10), is 30 because 30 is the smallest integer divisible by both 15 and 10. Write a function lcm() that takes two integer arguments and returns their lcm. The lcm() functon should calculate the least common multiple by calling the gcd() function from program 7.6...

  • In C++ Write a program that contains a BankAccount class. The BankAccount class should store the...

    In C++ Write a program that contains a BankAccount class. The BankAccount class should store the following attributes: account name account balance Make sure you use the correct access modifiers for the variables. Write get/set methods for all attributes. Write a constructor that takes two parameters. Write a default constructor. Write a method called Deposit(double) that adds the passed in amount to the balance. Write a method called Withdraw(double) that subtracts the passed in amount from the balance. Do not...

  • Write a Java class (name it TwoNumbers) that will manage two numbers. The class should have...

    Write a Java class (name it TwoNumbers) that will manage two numbers. The class should have two integer variables. The class should have the following constructors: - TwoNumbers(); - TwoNumbers(3, 4); The first constructor should initialize the class variables to zero. The second constructor should initialize the class variables to the passed variables. The class should have the following methods: - setNumbers(3, 4); - setFirstNum(3); - setSecondNum(4); - getSum(); - getDiff(); - getProd(); The setNumbers(3, 4) method should initialize the...

  • . Q7. Write a C++ class called Point2D that describes a 2-dimensional (x,y) point. It should...

    . Q7. Write a C++ class called Point2D that describes a 2-dimensional (x,y) point. It should have the following features: • A constructor that accepts two double values (x,y) to create a point object. A default constructor. • A void print() method that prints out the points coordinates. • A void set(Point2D p) method that allows you to set the point using the given object of Point2D. • A Point2D midPoint(Point2D p1) method that returns a Point2D object that is...

  • its about in C++ You will implement a simple calendar application The implementation should include a class named Calendar, an abstract class named Event and two concrete classes named Task an...

    its about in C++ You will implement a simple calendar application The implementation should include a class named Calendar, an abstract class named Event and two concrete classes named Task and Appointment which inherit from the Event class The Event Class This will be an abstract class. It will have four private integer members called year, month, day and hour which designate the time of the event It should also have an integer member called id which should be a...

  • Q1. Write a C++ class called Time24h that describes a time of the day in hours,...

    Q1. Write a C++ class called Time24h that describes a time of the day in hours, minutes, and seconds. It should contain the following methods: • Three overloaded constructors (one of which must include a seconds value). Each constructor should test that the values passed to the constructor are valid; otherwise, it should set the time to be midnight. A display() method to display the time in the format hh:mm:ss. (Note: the time can be displayed as 17:4:34, i.e. leading...

  • Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance...

    Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance and interface behaviors . The link to the PDF of the diagram is below MotorVehical.pdf Minimize File Preview User Define Object Assignment: Create a Intellij Project. The Intellij project will contain three user defined classes. The project will test two of the User Define Classes by using the invoking each of their methods and printing the results. You are required to create three UML...

  • C# Language The ColourControl Class The class should be public. The class defines a read-only or...

    C# Language The ColourControl Class The class should be public. The class defines a read-only or private set public property named Value of type int. The class defines a public event named ValueChanged whose delegate type is void with two parameters, object sender and EventArgs e. The public constructor for the class is a default constructor (i.e., takes no parameters). The constructor sets the Value property to 128. The class defines a public void method named IncreaseValue. The method increases...

  • Binary Tree Template Write your own version of a class template that will create a binary...

    Binary Tree Template Write your own version of a class template that will create a binary tree that can hold values of any data type. Demonstrate the class with a driver program. Place your binary tree template in it's own header file, Btree.h. Include methods for the following: inserting new values into the tree removing nodes from the tree searching the tree returning the number of nodes in the tree displaying the contents of the tree using preorder traversal Your...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT