0

I want to calulate the lcm of 1 to 20. This code works perfectly for 1 to
10, but when I use the same code for 1 to 20 the output is 232792237. The correct output is 232792560. I'm stuck.

C program to determine the lcm of 1 to 20

#include<stdio.h>
#include<math.h>
#define MAX 1000
int main()
{
   int a,z,i,j,b[MAX],k=0,count=0,l,p,n=1,limit,m=0;
   printf("Enter two numbers");
   scanf("%d%d",&a,&z);
   l=z;
   /*Storing prime numbers in array*/
   for(i=a+1;i<=z-1;i++)
   {
       for(j=2;j<i;j++)
       {
          if(i%j==0)
          break;
       }

       if(j==i)
       {
          b[k]=i;
          k++;
          count++;
       }  
    }
    limit=sqrt(l);

    /*Determining the smallest multiple*/
    while(b[m]<=l)
    {
       p=1;
       if(b[m]<=limit)
          p=floor(log10(l)/log10(b[m]));
       n=n*pow(b[m],p);
       m++;
       printf("%d\n",n);
   }
   return(0);
}
Vinay Shukla
  • 1,818
  • 13
  • 41

0 Answers0