FINDING REMAINDER THROUGH A C PROGRAM IN THREE DIFFERENT WAYS

Basically I have used three methods for finding the remainder :-
- Using the modulus operator % (definitely everyone is acquainted with this).
- Using the division method, here when an integer is divided by another integer then the result is such that all its decimal points are truncated and the result is also an integer. Using this property of programming, we can find the remainder(see the second code and you will easily grasp the concept).
- It is a very interesting method, and most of you may not be acquainted with this one. In this method I have not used modulus operator, division operator nor the multiplication operator. So the code becomes a bit interesting. This method is called method of successive subtraction.
Method 1
/******************************************************************************/
#include<stdio.h>
#include<conio.h>
int main()
{
int dividend, divisor, remainder;
clrscr();
printf("ENTER THE DIVIDEND : ");
scanf("%d",÷nd);
printf("ENTER THE DIVISOR : ");
scanf("%d",&divisor);
remainder=dividend%divisor;
printf("THE REMAINDER ON DIVIDING %d BY %d IS %d",dividend,divisor,remainder);
getch();
return 1;
}
/****************************************************************************/
Method 2
/******************************************************************************/
#include<stdio.h>
#include<conio.h>
int main()
{
int dividend,divisor,remainder;
clrscr();
printf("ENTER THE DIVIDEND : ");
scanf("%d",÷nd);
printf("ENTER THE DIVISOR : ");
scanf("%d",&divisor);
remainder=dividend-((int)dividend/divisor)*divisor;
printf("THE REMAINDER ON DIVIDING %d BY %d IS %d",dividend,divisor,remainder);
getch();
return 1;
}
/******************************************************************************/
Method 3
/*******************************************************************************/
#include<stdio.h>
#include<conio.h>
int main()
{
int dividend, divisor, remainder,a,b;
clrscr();
printf("ENTER THE DIVIDEND : ");
scanf("%d",÷nd);
printf("ENTER THE DIVISOR : ");
scanf("%d",&divisor);
a=dividend;
b=divisor;
while(a>=0)
{
a-=b;
remainder=a+b;
}
printf("THE REMAINDER ON DIVIDING %d BY %d IS %d",dividend,divisor,remainder);
getch();
return 1;
}
/******************************************************************************/
The output of all the three programs is:-
This comment has been removed by the author.
ReplyDeleteBug in The Method 3 if u divide 12 by 6 it gives wrong result just check it out.
ReplyDeleteTry this code
while((a-b)>=0)
{
a=a-b;
}
cout<<"Remainder="<<a;
Thank you very much Mayank...
ReplyDeleteyou are right...but now I have updated it, and you can check...
actually I was in a bit hurry while writing the code and I didn't checked my program through enough test cases and missed this big thing....thank you once again :)