Write a program that asks the user for two numbers then look

Write a program that asks the user for two numbers, then looks for the least common

multiple.

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!

Solution

Please follow the code and comments for description :

CODE :

#include <iostream>
using namespace std;
int main() // class to run the code
{
    int a, b, lcm; // required initialisations
    cout << \"Enter a number: \"; // prompt to enter the first number
    cin >> a;
   cout << \"Enter another number: \"; // prompt to enter the second number
   cin >> b;
    for(int i = 2; i <= 100; i++) { // iterating over th enumbers till 100 for the LCM to be found
        if (i % a == 0 && i % b == 0) //Checking for the first number which is divisible by both the numbers
        {
            lcm = i; // if found replacing the result to the number
            cout << i << \"? Yes.!\" << endl; // print the output to console
            break; //exiting from the loop, as we don’t need anymore checking after getting the LCM
        }
        else {
            cout << i << \"? No\" << endl; // else iterating over the numbers printing the result to console
        }
    }  
    return 0;
}

OUTPUT :

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.!

Hope this is helpful.

Write a program that asks the user for two numbers, then looks for the least common multiple. If the program does not find one by 100, stop looking. For example
Write a program that asks the user for two numbers, then looks for the least common multiple. If the program does not find one by 100, stop looking. For example

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site