פיתרון תרגיל 4 .
 
#include <stdio.h>
#include <conio.h>

int iterative_gcf(int a, int b)
{
   int temp;    

   while(a!=b)  
      if(a>b)    
      {
         temp=a;
         a=b;
         b=temp-b;
      }
      else if(a<b) 
         b-=a;
   return(a);
}

int recursive_gcf(int a, int b)
{
   if(a==b)             
      return(a);
   if(a>b)              
      return(recursive_gcf(b,a-b));            
   if(a<b)             
      return(recursive_gcf(a,b-a));         
}

void main()
{
   int a, b;

   do 
   {
      clrscr();
      printf("Enter two natural numbers: ");
      scanf("%d%d", &a, &b);
   }
   while((a<1)||(b<1));
   printf("The greatest common factor of %d and %d is: %d.\n",
          a, b, recursive_gcf(a,b));
   getch();
}
הקודם
הבא