Lecture Note n DIV d integer part of dividing n by d n MOD d

Lecture Note:

n DIV d: integer part of dividing n by d

n MOD d: the remainder

n = (n DIV d) * d + (n MOD d)

Example: 7=2*3+1, so

7 DIV 3 = 2

7 MOD 3 = 1

Recall the DIVMOD algorithm

This algorithm computed (n DIV d, n MOD d) for integers n and d, both greater than 0.

Using pseudocode in your programming language of choice, expand the algorithm to accept any integer n, both positive and negative (d is still assumed to be positive). Add comments when needed to make sure your logic is clear to the reader.

Solution

#include<stdio.h>
function(int n,int d)
{
int a,b,s;
a = n/d; //here a is your n DIV d: integer part of dividing n by d
b = n%d; // here b is your n MOD d: the remainder
s = a*d+b; // This state n = (n DIV d) * d + (n MOD d)
printf(\"value of nDIVd=%d \ value of nMODd=%d\ \",a,b);
printf(\"(n DIV d) * d + (n MOD d)=%d\",s);

}
void main()
{
int a,b,x;
scanf(\"%d\",&a);
scanf(\"%d\",&b);
x= function(a,b);
}

Lecture Note: n DIV d: integer part of dividing n by d n MOD d: the remainder n = (n DIV d) * d + (n MOD d) Example: 7=2*3+1, so 7 DIV 3 = 2 7 MOD 3 = 1 Recall

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site