פיתרון תרגיל 2
 

#include <stdio.h>
#include <conio.h>
int sum(int a, int b) 
{
   return((b==0) ? a : sum(a+1,b-1));
}


int difference(int a, int b) 
{
   return((b==0) ? a : difference(a-1,b-1));
}

int multiply(int a, int b) 
{
   return((b==0) ? 0 : multiply(a,b-1)+a);
}

int divide(int a, int b) 
{
   return((a<b) ? 0 : divide(a-b,b)+1);
}

int modulo(int a, int b) 
{
   return((a<b) ? a : modulo(a-b,b));
}

void main()
{
   int a, b;

   do 
   {
      clrscr();
      printf("Enter two natural numbers: ");
      scanf("%d%d", &a, &b);
   }
   while((a<1)||(b<1));
   printf("%d+%d=%d\n", a, b, sum(a,b));
   printf("%d-%d=%d\n", a, b, difference(a,b));
   printf("%d*%d=%d\n", a, b, multiply(a,b));
   printf("%d/%d=%d(%d)\n", a, b, divide(a,b), modulo(a,b));
   getch();
}
הקודם
הבא