Friday, 5 April 2013

FINDING REMAINDER THROUGH A C PROGRAM IN THREE DIFFERENT WAYS

FINDING REMAINDER THROUGH A C PROGRAM IN THREE DIFFERENT WAYS

Although the heading of this post seems so usual and is not at all eye catching, but I'm sure that this post will definitely help the beginners in programming to grab the concept of algorithm formation. Here I have show how to solve a single problem through three different ways in C programming. This problem seems very easy  when just seen and ignored, but when this problem is taken up with a mood for solving it, definitely it goes a bit tricky. Viewers may find it very easy, but they should at least go through the code so as to know the three, totally different solutions. I know that this is not related to any practical application as many of my posts were like heap sort, determining week-day, tower of hanoi, lexicographic sorting and some more, but I'm sure that this post will definitely boost up your capabilities of algorithm development.

Basically I have used three methods for finding the remainder :-
  1. Using the modulus operator % (definitely everyone is acquainted with this).
  2. 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).
  3. 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",&dividend);
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",&dividend);
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",&dividend);
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:-




3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Bug in The Method 3 if u divide 12 by 6 it gives wrong result just check it out.
    Try this code
    while((a-b)>=0)
    {
    a=a-b;
    }
    cout<<"Remainder="<<a;

    ReplyDelete
  3. Thank you very much Mayank...
    you 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 :)

    ReplyDelete