Radical Developers welcomes all the visitors to be a member of the team. Come join us. Its all about open-source concept. All Programs are written and tested in MacOS X, Unix, Linux and may not match with the outputs of Turbo C++ in Windows

Tuesday, January 4, 2011

Write a Program to find out the Greatest Common Divisor (GCD) of Two Numbers

Source Code :

#include<stdio.h>
void main()
{
      int a,b,i,s,cf=0;
      clrscr();
      printf("\nEnter Two Numbers : ");
      scanf("%d%d",&a,&b);
      if(a<b)
            s=a;        //Assigning smallest number
      else
            s=b;
      for(i=2;i<s;i++) 
      {
            if(a%i==0 && b%i==0)    //Finding out the common divisor
            {
                  if(i>cf)    //Checking for Greatest Common Divisor
                        cf=i;
            }
      }
      printf("\n\nGCD : %d",cf);          //Display Result
      getch();
}

Output :

Enter Two Numbers : 24 48

GCD : 12

No comments:

Post a Comment