Thursday 26 May 2011

Finding LCM and GCD

Finding LCM and GCD



#include<stdio.h>
#include<conio.h>


void main()
{
  int a[20],n,i,j,c,max,min;
  unsigned long prod;
  clrscr();
  printf("Enter the no. of entries: ");
  scanf("%d",&n);
  printf("Enter the entries:
");
  for(i=0;i<n;i++)
  {
    scanf("%d",&c);
    if(c>0)
      a[i]=c;
    else
    {
      printf("Invalid Entry");
      return;
    }
  }


  max=a[0];
  for(i=0;i<n;i++)
    if(a[i]>=max)
      max=a[i];
  min=a[0];
  for(i=0;i<n;i++)
    if(a[i]<min)
      min=a[i];


  for(i=0,prod=1;i<n;i++)
    prod=prod*a[i];


  for(i=max;i<=prod;i+=max)
  {


    c=0;
    for(j=0;j<n;j++)
      if(i%a[j]==0)
c+=1;
    if(c==n)
    {
      printf("The LCM of the nos: %d
",i);
      break;
    }
  }


  for(i=min;i>0;i--)
  {
    if (min%i==0)
    {
      c=0;
      for(j=0;j<n;j++)
if(a[j]%i==0)
  c+=1;
    }
    if(c==n)
    {
      printf("The GCD of the nos: %d",i);
      break;
    }
  }
  getch();
}

1 comments:

yamalpadberg said...

Mummys Gold Casino | Jackson Hole | Jackson Hole Casino
Mummys Gold 전라북도 출장마사지 Casino, just minutes from Jackson Hole 여주 출장마사지 Casino in Jackson Hole, is the 창원 출장안마 closest casino in town. With over 200 slot machines 속초 출장마사지 and a 동해 출장마사지 300 table

Post a Comment