1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

find out lcm and gcd in c programming

Discussion in 'C' started by Ziaur Rahman, Oct 24, 2006.

Thread Status:
Not open for further replies.
  1. Ziaur Rahman

    Ziaur Rahman New Member

    Joined:
    Oct 22, 2006
    Messages:
    11
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Pune
    Finding LCM and GCD
    Code:
    
    #include<stdio.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();
    }
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
    We already have one article related to LCM GCD. Ziaur Rahman I must say nice way to start off.
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
    Duplicate of [thread=1714]find out lcm and gcd[/thread]. Thread closed.
     
Thread Status:
Not open for further replies.

Share This Page