Learn how to Discover the Most A number of in C


The problem

Given a Divisor and a Sure , Discover the most important integer N , Such That ,

Situations :

  • N is divisible by divisor
  • N is lower than or equal to certain
  • N is better than 0.

Notes

  • The parameters (divisor, certain) handed to the perform are solely constructive values .
  • It’s assured that a divisor is Discovered .Enter » Output Examples
maxMultiple (2,7) ==> return (6)

Rationalization:

(6) is divisible by (2) , (6) is lower than or equal to certain (7) , and (6) is > 0 .


maxMultiple (10,50)  ==> return (50)

Rationalization:

(50) is divisible by (10) , (50) is lower than or equal to certain (50) , and (50) is > 0 .*


maxMultiple (37,200) ==> return (185)

Rationalization:

(185) is divisible by (37) , (185) is lower than or equal to certain (200) , and (185) is > 0 .

The answer in C

Choice 1:

int maxMultiple(int divisor, int certain)  {
    return certain / divisor * divisor;
}

Choice 2:

int maxMultiple(int divisor, int certain) {
    return certain - certain % divisor;
}

Choice 3:

int maxMultiple(int divisor, int certain)  {
    whereas(certain>0) {
      if(boundpercentdivisor==0)
      break;
      else
      bound--;
    }
  return certain;
}

Check instances to validate our answer

#embrace <criterion/criterion.h>

int maxMultiple(int divisor, int certain);

Check(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);
}
Check(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);
}
See also  How you can Reverse Phrases or Sentences in Python

Leave a Reply