To find the smallest number divisible by first n numbers

48 Views Asked by At

When I try to run the following code, I am getting a warning as floating point exception(core dumped). Can someone please tell me what mistake I am doing here? The program is to find the smallest number divisible by first n numbers. Ex : if input=4 then output should be equal to 12. I dont want to use the inbuilt gcd function.

#include <iostream>
using namespace std;


int gcd(int n,int m)
{
    for(int i=1;i<=n && i<=m;i++){
        if(n%i==0 && m%i==0)
            return i;
    }
}
    int lcm(int num)
    {
        int sum =1;
        for(int j=0;j<=num;j++){
            sum=(sum*j)/gcd(sum,j);
            return sum;
        }
    }

int main()
{
    int n=4;int m=5;
    cout<<gcd(n,m);
    int num= 4;
    cout<<lcm(num);
    return 0;
}
0

There are 0 best solutions below