Implement other arithmetic operators using + operator


This is the program of implementing *,-,/ using + arithmetic operator.

#include<stdio.h>
#include<conio.h>
int multiply(int i,int j)
{
int s=0;
for(int k=0;k<j;k++)
{
s=s+i;
}
return s;
}
int substract(int i,int j)
{
int d=j<0?1:-1;
return i+multiply(d,j);
}

int divide(int i,int j)
{
int count=0;
if(i>j)
{
int d=i;

while(d>0)
{
d=substract(d,j);
count++;
}
}
else
{
count=0;
}
return count;
}
int main()
{
int a=20,b=5;
printf("%d\n",substract(a,b));
printf("%d\n",multiply(a,b));
printf("%d\n",divide(a,b));
getch();
}

Comments

Popular posts from this blog

Tricky Questions or Puzzles in C

Program to uncompress a string ie a2b3c4 to aabbbcccc

Programs and Puzzles in technical interviews i faced