Lecture Note: n DIV d: integer part of dividing n by d n MOD d: the remainder n
ID: 3779338 • Letter: L
Question
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.
Explanation / Answer
Your program is as below :
//This program is to write function divMod algorithm. n = (n DIV d) *d + (n MOD d)
#include <stdio.h>
int divMod(int n,int d); //Function Declaration
int main(void) {
int n,d,result=0;
//Input number and divisor
printf("Please enter number :");
scanf("%d",&n);
label :
printf("Please enter divisor : ");
scanf("%d",&d);
if(d<0){ //Checking for positive divisior
printf("Please enter positive divisor");
goto label;
}
result = divMod(n,d); //function call
printf("Your result is : %d",result);
return 0;
}
int divMod(int n,int d){
int t = (n/d)*d+(n%d);
return t;
}
Output :
(1)
Please enter number : 7
Please enter divisor : 3
Result is : 7
(2)
Plese enter number : 7
Please enter divisor : -3
Please enter positive integer
Please enter divisor : 3
Result is : 7
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.