Question

C++ Program to find Greatest common Factor (GCF) and Least Common Multiple (LCM) of a number...

C++ Program to find Greatest common Factor (GCF) and Least Common Multiple (LCM) of a number

c++

Homework Answers

Answer #1

#include<iostream>

using namespace std;

int main()
{
int p, q, n1, n2, t, gcf, lcm;
cout<<"Enter the two numbers : ";
cin >> n1 >> n2; // read the two numbers
p = n1;
q = n2;
while (q != 0) // calculate gcf
{
t = q;
q = p % q;
p = t;
}
gcf = p;
lcm = (n1 * n2) / gcf; // calculate lcm
cout << "GCF of two numbers " << n1 <<" and " << n2 << " is : " << gcf << endl; // print gcf
cout << "LCM of two numbers "<< n1 <<" and " << n2 << " is : " << lcm; // print lcm

return 0;
}

OUTPUT :

Know the answer?
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
Similar Questions
Write a C++ program to find least common multiple (LCM) of two, three and four integer...
Write a C++ program to find least common multiple (LCM) of two, three and four integer values. The integer values are entered from the keyboard and the outputs are printed to the console. The LCM of two, three and four integer values are computed using Prime factorization method. You have to use arrays to hold input values and use functions/methods to get data from the keyboard, display output to the console, calculate LCM using Prime factorization method. Your program should...
Find the greatest common factor applying Euclidean Algorithm: 29448, 59220
Find the greatest common factor applying Euclidean Algorithm: 29448, 59220
C++ The problem is as below: The Greatest and Least of These Write a program with...
C++ The problem is as below: The Greatest and Least of These Write a program with a loop that lets the user enter a series of integers. The user should enter -99 to signal the end of the series. After all the numbers have been entered, the program should display the largest and smallest numbers entered.
Write Java program Lab42.java which takes as input two positive integers m and n and computes...
Write Java program Lab42.java which takes as input two positive integers m and n and computes their least common multiple by calling method lcm(m,n), which in turn calls recursive method gcd(m,n) computing the greatest common divisor of m and n. Recall, that lcm(m,n) can be defined as quotient of m * n and gcd(m,n).
C CODE PLZ! All instructions are in sections of code #include <stdio.h> /* TODO: Define 3...
C CODE PLZ! All instructions are in sections of code #include <stdio.h> /* TODO: Define 3 functions input, gcd and lcm in such a way that the main function below compiles correctly and has the correct behavior. The input function prompts the user to enter a non-negative integer. If the user enters a negative integer, the function prints a "sorry" statement and prompts the user again. It keeps on prompting until the user enters a non-negative number. The input function...
Find the least common multiple of these two expressions. 6x5v3u2 and 9v8u4    This is the...
Find the least common multiple of these two expressions. 6x5v3u2 and 9v8u4    This is the way the question is posted. I did more research. I believe the answer is 18x5v8u4
This question is about the C program(NOT C++ program) Please write the C program to find...
This question is about the C program(NOT C++ program) Please write the C program to find out the largest and the smallest number in 32bits For example ,the smallest number in 32 bits is -2147483648
The greatest common divisor c, of a and b, denoted as c = gcd(a, b), is...
The greatest common divisor c, of a and b, denoted as c = gcd(a, b), is the largest number that divides both a and b. One way to write c is as a linear combination of a and b. Then c is the smallest natural number such that c = ax+by for x, y ∈ N. We say that a and b are relatively prime iff gcd(a, b) = 1. Prove that a and n are relatively prime if and...
Prove that any two nonzero elements of a UFD have a least common multiple, and describe...
Prove that any two nonzero elements of a UFD have a least common multiple, and describe the least common multiple in terms of the prime factorizations of the two elements.
IN PYTHON: Please answer as simply as possible in Python The highest Common Factor of a...
IN PYTHON: Please answer as simply as possible in Python The highest Common Factor of a pair of numbers is the largest number than will divide evenly into both members of the pair. Write a program to read in two numbers and compute the Highest Common Factor. The output will look like this: Enter the first number: 4 Enter the second number: 20 The highest common factor: 4
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT