0

This is a program to find the least common multiple of numbers 1 to 20. I dont know why it doesnt work. Somebody please help me out

#include<iostream>
#include<conio.h>
#define A 100
using namespace std;
int i;
bool IsItDiv(int);
int main()
{   int num=21;bool tf;

    tf=IsItDiv(num);cout<<"The number thats divisible 

is";
    if(tf)
    cout<<"The number thats divisible is"<<num;
    else
    {num++;IsItDiv(num);}
    getch();
    
    }
bool IsItDiv(int n)
{
    for(i=1;i<=20;i++)
    {
        if((n%i)==0)
        {
            if(i==20)
            {return true;}
            else 
            {continue;}
            
        }
        else
        {
            return false;
        }
    }
}
3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by ArkM
1

(1) don't use conio.h

(2) do post your questions in the appropriate forum: this is C++ code, not C

1

What's an excited title for help request: I don't know!!...
Next time try to invent more sensible phrase...

It's a well-known ;) fact that

lcm(a,b) = (a/gcd(a,b))*b

where gcd means greatest common divisor.
Euclidian algorithm for computing gcd - see:
http://en.wikipedia.org/wiki/Euclidean_algorithm
It's so easy...

Don't use global variables in this simple (and others) program. For example, declare IsItDiv function with two parameters - n and i. However no need in this function in the recommended solution.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.