Pages

Jul 30, 2011

Alim Ul Karim's Blog: Division Operation Using Minus Operator In C/C++, ...

Alim Ul Karim's Blog: Division Operation Using Minus Operator In C/C++, ...: "Division Operation Using Minus Operator In C/C++, Algorithm Division Operation Using Minus Operator In C/C++, Algorithm It is not ve..."

Remainder Base Operation Using Minus In C/C++, Algorithm


Remainder Base Operation Using Minus In C/C++, Algorithm

Remainder Base Operation Using Minus In C/C++, Algorithm



Remainder Base Operation Using Minus In C/C++, Algorithm

It is not very hard to write such an algorithm “Remainder Base Operation Using minus & plus Operator” in C. To accomplish our motives we can create a user-defined function, where we will use only minus to get our division result. Algorithm is the main fact for those kinds of problems. However, the algorithm is very easy to understand.
Division-by-Division Operator:
1.       9 / 3
2.       9 / 5
3.       3 / 2
We know that a division is a combination of (numerator / denominator or divisor).
Think as a simple division as above, what we do is subtract the numerator (9) by the divisor (3) each time if the numerator is not less than the divisor (3).
numerator = numerator – divisor; // (if numerator is not equal or less than the divisor)
Once we get the quotient, we can get the remainder very easily.
remainder = numerator – (quotient * divisor) ;
From that above algorithm, we can hence our code:
/* Code Written By Alim Ul Karim */
/* email : auk.junk@live.com */
#include <stdio.h>

int multiplication(int a, int b){
    int i , sum = 0;
    for (i = 1; i <= b ; i++ ){
        // a multiplication of 5 x 6 is actually adding 5,  6 times
        sum += a;
    }
    return sum;
}

int remainder_module(int a, int b)
{
    //approximate result of the division operation between a ,b using minus
    int quotient = 0;
    int remainder = 0;
    int x = a , y = b ;

    //this condition works for positive numbers only
    while (>= y) {
        x -= y;
        quotient++;
    }

    //remainder = numerator – (quotient * divisor)
    remainder = a - multiplication( b , quotient) );
    return remainder;
}


int main(){
    int = 9;
    int = 5;

    int = remainder_module(x , y );
    printf("%d" , r);
}

However, since we have a problem with the negative number so we can include 2 more conditions in our code to make it better.
Code for all numbers:
/* Code Written By Alim Ul Karim */
/* email : auk.junk@live.com */

#include <stdio.h>

int multipication(int a, int b){
    int i , sum = 0;
    if(== 0 || b == 0) { return 0;}
    //works for all positive numbers
    if(> -1){
        for (i = 1; i <= b ; i++ ){
            // a multipication of 5 x 6 is actually adding 5,  6 times
            sum += a;
        }
    } else {
         //if we assume b = -6 , we are incrementing +1 every time until we reaches the -1
        for (i = b; i <= -1 ; i++ ){
            sum -= a;
            //printf("a=%d , b = %d , mul = %d\n" , a ,b , sum );
        }
    }
    return sum;
}

int remainder_modulous(int a, int b)
{
    //approximate result of the division operation between a ,b using minus
    int quotient = 0;
    int result_in_minus = 0;
    int remainder = 0;
    int x = a , y = b ;

     //if one is positive and the other is negetive the result would be in negative
    if( (< 0 && b > -1) || (> -1 && < 0) ){
        result_in_minus = 1;
    }

    //check if minus exist, make all positive
    if (< 0){ = multipication(a , -1)}
    if (< 0){ y = multipication(b , -1)}

    //this condition works for positive numbers only
    while (>= y) {
        x -= y;
        quotient++;
    }

    //if one is positive and the other is negetive the result would be in negative
    if(result_in_minus == 1) { quotient = multipication(quotient , -1) ;}
    //printf("quotient= %d\n" , quotient);

    //remainder = numerator  (quotient * divisor)
    remainder = a - ( multipication( b , quotient) );
    return remainder;
}


int main(){
    int = -7;
    int = 5;

    int = remainder_modulous(x , y );
    printf("remainder of x(%d) / y(%d) is r=%d\n"  , x , y , r);
}

 See also:

Author Related Links:
SEO Labels:



Remainder / Remainder in C / Remainder In C++ / modulus(%) user-defined function / modulus using custom codes / Remainder get using minus and plus or add operator only / remainder get without using % 


Remainder Base Operation Using Minus In C/C++, Algorithm


Remainder / Remainder in C / Remainder In C++ / modulus(%) user-defined function / modulus using custom codes / Remainder get using minus and plus or add operator only / remainder get without using %


Division Operation Using Minus Operator In C/C++, Algorithm


Division Operation Using Minus Operator In C/C++, Algorithm


Division Operation Using Minus Operator In C/C++, Algorithm

It is not very hard to write such a function “Division  Between Two Numbers Using Without Using Division Operator” in C. To accomplish our motives we can create a user-defined function, where we will use only minus to get our division result. Algorithm is the main fact for those kinds of problems. However, the algorithm is very easy to understand.
Division-by-Division Operator:
1.       9 / 3
2.       9 / 5
3.       3 / 2
We know that a division is a combination of (numerator / denominator or divisor).
Think as a simple division as above, what we do is subtract the numerator (9) by the divisor (3) each time if the numerator is not less than the divisor (3).
numerator = numerator – divisor; // (if numerator is not equal or less than the divisor)
From that above algorithm, we can hence our code:
/* Code Written By Alim Ul Karim */
/* email : auk.junk@live.com */

#include <stdio.h>

int division(int x, int y) {
    int quotient = 0;
    while (x >= y) {
        x  -=  y; // (if numerator is not equal or less than the divisor)
        quotient++;
    }
    return quotient;
}

void main() {
    int x = 9;
    int y = 3;
  
    //works fine for all positive numbers, but not for negative numbers
    int r = division(x ,y); // result 1
    printf("%d" , r);
}

However, since we have a problem with the negative number so we can include 2 more conditions in our code to make it better.
Code for all numbers:
/* Code Written By Alim Ul Karim */
/* email : auk.junk@live.com */

#include <stdio.h>

int division(int a, int b)
{
    //approximate result of the division operation between a ,b using minus
    int quotient = 0;
    int result_in_minus = 0;

     //if one is positive and the other is negative the result would be in negative
    if ((< 0 && b > -1) || (> -1 && b < 0) ){
        result_in_minus = 1;
    }

    //check if minus exist, make all positive
    if (< 0){ a = a * (-1)}
    if (< 0){ b = b * (-1)}

    //this condition works for positive numbers only
    while (>= b) {
        a -= b;
        quotient++;
    }

    //if one is positive and the other is negative the result would be in negative
    if(result_in_minus == 1) { quotient = quotient * -1 ;}

    return quotient;
}


void main(){
    int x = 9;
    int y = -3;
    // works for all numbers
    int r = division(x , y);
    printf("%d" , r);
}


See also related links from Alim Ul Karim's Blog:

SEO Labels:



Division Operation Using Minus Operator In C/C++, Algorithm