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 from the lecture on 11/16/2016 (Covered in slides 19-22 in Lecture 21 slide deck, available on the course website). 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<bits/stdc++.h>
using namespace std;
void DivMod(int n,int d)
{
int divnum=n/d;//n Div d
int rem=n%d;//n MOD d
int num=divnum*d+rem;
cout<<\"Number is using diviMod algorithm \"<<num<<endl;
}
int main(int argc, char const *argv[])
{int n,d;
cout<<\"Enter n\ \";
cin>>n;
cout<<\"Enter d\ \";
cin>>d;
DivMod(n,d);
return 0;
}
============================================================
Output:
akshay@akshay-Inspiron-3537:~/Chegg$ g++ divido.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Enter n
7
Enter d
3
Number is using diviMod algorithm 7
