C Help needed I will need it to output the exact information

C++ Help needed

I will need it to output the exact information in the below 2 examples. Nothing can be hard coded. I am in a basic C++ course so it shouldnt be more difficult then using loops. I included my current program below the problem statement. I don\'t seem to be getting the LCM correct. When I run with 3 and 4 I get 12, which is correct, but when I run with 2 and 4, I get 8, which is incorrect, as the answer should be 4. I also am unsure of how to get all the numbers to output as shown below as well.

Write a program that asks the user for two numbers, then looks for the least common multiple.For a refresher on what a least common multiple is, see:

http://www.math.com/school/subject1/lessons/S1U3L3GL.html

If the program does not find one by 100, stop looking.

For example:

Enter a number: 3

Enter another number: 4

2? no. 3? no. 4? no. 5? no. 6? no. 7? no. 8? no. 9? no. 10? no.

11? no. 12? yes!

You do not have to worry about how the answer wraps on the screen.

Another example:

Enter a number: 97

Enter another number: 51

2? no. 3? no. 4? no. 5? no. 6? no. 7? no. 8? no. 9? no. 10? no. 11? no.

12? no. 13? no. 14? no. 15? no. 16? no. 17? no. 18? no. 19? no. 20? no.

21? no. 22? no. 23? no. 24? no. 25? no. 26? no. 27? no. 28? no. 29? no.

30? no. 31? no.32? no. 33? no. 34? no. 35? no. 36? no. 37? no. 38? no.

39? no. 40? no. 41? no. 42? no. 43? no. 44? no. 45? no. 46? no. 47? no.

48? no. 49? no. 50? no. 51? no. 52? no. 53? no. 54? no. 55? no. 56? no.

57? no. 58? no. 59? no. 60? no. 61? no. 62? no. 63? no.

64? no. 65? no. 66? no. 67? no. 68? no. 69? no. 70? no. 71? no. 72? no. 73? no. 74? no.

75? no. 76? no. 77? no. 78? no. 79? no. 80? no. 81? no. 82? no. 83? no.

84? no. 85? no. 86? no. 87? no. 88? no. 89? no. 90? no. 91? no. 92? no.

93? no. 94? no. 95? no. 96? no. 97? no. 98? no. 99? no. 100? no. Gave up!

This is my current program:

#include <iostream>
#include <string>
using namespace std;

int main() {
int userNum1 = 0;
int userNum2 = 0;
int leastCM = 0;
int i = 0;

cout << \"Enter a number: \";
cin >> userNum1;
cout << userNum1 << endl;

cout << \"Enter another number: \";
cin >> userNum2;
cout << userNum2 << endl;

leastCM = userNum1 * userNum2;

for (i=1; i<=leastCM;i++){
   if ((i%leastCM==0) && (i%userNum1==0) && (i%userNum2==0)){
       cout << i;
}
}
   return 0;
}

this is my output from my current program:

Solution

// C++ code to find LCM of two numbers

#include <iostream>
#include <string>
using namespace std;

int main()
{
int userNum1 = 0;
int userNum2 = 0;
int leastCM = 0;
  
cout << \"Enter a number: \";
cin >> userNum1;

cout << \"Enter another number: \";
cin >> userNum2;


if(userNum1 > userNum2)
leastCM = userNum1;
else
leastCM = userNum2;

do
{
if (leastCM % userNum1 == 0 && leastCM % userNum2 == 0)
{
cout << \"LCM = \" << leastCM << endl;
break;
}
else
++leastCM;

// If the program does not find one by 100, stop looking.
if(leastCM > 100)
{
cout << \"No LCM found till 100. Gave up\ \";
break;
}

} while (true);

return 0;
}

/*
output:

Enter a number: 49
Enter another number: 56
No LCM found till 100. Gave up


Enter a number: 12
Enter another number: 20
LCM = 60


*/

C++ Help needed I will need it to output the exact information in the below 2 examples. Nothing can be hard coded. I am in a basic C++ course so it shouldnt be
C++ Help needed I will need it to output the exact information in the below 2 examples. Nothing can be hard coded. I am in a basic C++ course so it shouldnt be

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site