How to Discover the Optimum Several in C

The obstacle

Provided a Divisor and a Bound, Discover the biggest integer N, Such That,

Conditions:

  • N is divisible by divisor
  • N is less than or equivalent to bound
  • N is higher than 0

Notes

  • The criteria (divisor, bound) passed to the function are just favorable worths
  • It’s ensured that a divisor is Discovered Input” Output Examples
 maxMultiple (2,7) ==> > return (6 )
. 

Description:

( 6 ) is divisible by (* )( 2) , (* )( 6) is less than or equivalent to > bound (7 )
, and ( 6 ) is > 0. maxMultiple( 10,50 )== > return (50). Description:


( 50 )

is > divisible by (10 ),( 50 ) is less than or equivalent to bound ( 50 ), and ( 50 ) is > > 0. * maxMultiple( 37,200)== > return( 185 ). Description:


( 185)(
*) is divisible by(* )( 37) (* ),

( 185
)

is less than or equivalent to bound( 200) , and( 185) is > 0. The service in C # Choice 1: int maxMultiple( int divisor, int bound) {

return bound/ divisor *
divisor;
}
Choice 2:

int maxMultiple( int divisor, int bound) {<. return bound - bound % divisor; }

int maxMultiple( int divisor, int bound) {while( bound > 0) {if( bound % divisor = = 0).
break;.
else. bound–;.} return bound;.}

 Test cases to verify our service

#

 #include < criterion/criterion.
h >.
int maxMultiple( int divisor, int bound );.
Test( Maximum_Multiple, Check_Small_Positives).
{cr_assert_eq( maxMultiple( 2,7), 6);.
cr_assert_eq( maxMultiple( 3,10), 9);.
cr_assert_eq( maxMultiple (7,17), 14);.
} Test (Maximum_Multiple, Larger_Positives). {cr_assert_eq (maxMultiple (10,50), 50);. cr_assert_eq (maxMultiple (37,200), 185);. cr_assert_eq (maxMultiple (7,100), 98);.} 

.

Like this post? Please share to your friends:
Leave a Reply

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: