Idanref

Return Max Divider | C

Nov 18th, 2020 (edited)
410
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // -------- Exercise 9 H.W 5 ------------
  2.  
  3.  
  4. #include <stdio.h>
  5.  
  6. int maximum_divider(int num)
  7. {
  8.     // if prime number return 1
  9.     // else return maximum divider
  10.  
  11.     int max_divider;
  12.  
  13.     for (int i = 1; i <= num / 2; i++)
  14.     {
  15.         if (num % i == 0)
  16.         {
  17.             max_divider = i;
  18.         }
  19.     }
  20.  
  21.     return max_divider;
  22. }
  23.  
  24. void main()
  25. {
  26.     int num = maximum_divider(22);
  27. }
  28.  
  29.  
  30. // Harder version - My version
  31.  
  32.  
  33. /* #include <stdio.h>
  34.  
  35. int maximum_divider(int num)
  36. {
  37.     // if prime number return 1
  38.     // else return maximum divider
  39.  
  40.     int max_divider = num;
  41.  
  42.     for (int i = 2; i <= num / 2; i++)
  43.     {
  44.         if (num % i == 0)
  45.         {
  46.             max_divider = i;
  47.         }
  48.     }
  49.  
  50.     return max_divider;
  51. }
  52.  
  53. void main()
  54. {
  55.     int num = maximum_divider(21);
  56. } */
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×