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 (x >= y) { x -= y; quotient++; } //remainder = numerator – (quotient * divisor) remainder = a - ( multiplication( b , quotient) ); return remainder; } int main(){ int x = 9; int y = 5; int r = 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(a == 0 || b == 0) { return 0;} //works for all positive numbers if(b > -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( (a < 0 && b > -1) || (a > -1 && b < 0) ){ result_in_minus = 1; } //check if minus exist, make all positive if (a < 0){ x = multipication(a , -1); } if (b < 0){ y = multipication(b , -1); } //this condition works for positive numbers only while (x >= 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 x = -7; int y = 5; int r = remainder_modulous(x , y ); printf("remainder of x(%d) / y(%d) is r=%d\n" , x , y , r); } |
- Division Operation Using Minus Operator In C/C++, Algorithm
- Remainder / Modulus Using Add(+) and Minus(-) or Subtract(-) Operator in C/C++
- Google Alim Ul Karim
- Visit Alim Ul Karim's Portfolio
No comments:
Post a Comment