Advertisement
dmilicev

four_integers_sum_63.c

Jan 31st, 2020
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.03 KB | None | 0 0
  1. /*
  2.  
  3.     four_integers_sum_63.c
  4.  
  5.     Task:
  6.  
  7.     If a, b, c, d are positive integers, with a sum of 63,
  8.     what is the maximum value of ab + bc + cd ?
  9.  
  10.     Answer: maximum sum is 991
  11.     for a=1, b=30, c=31, d=1
  12.     or
  13.     for a=1, b=31, c=30, d=1
  14.  
  15.  
  16.     You can find all my C programs at Dragan Milicev's pastebin:
  17.  
  18.     https://pastebin.com/u/dmilicev
  19.  
  20.     https://www.facebook.com/dmilicev
  21.  
  22. */
  23.  
  24. #include <stdio.h>
  25.  
  26. int main(void)
  27. {
  28.     int a, b, c, d, sum, max_sum=0, changed_max_sum=0;
  29.  
  30.     // maximum value for one integer is 60 when rest three integers are equal to 1.
  31.     for(a=1; a<=60; a++)
  32.         for(b=1; b<=60; b++)
  33.             for(c=1; c<=60; c++)
  34.                 for(d=1; d<=60; d++)
  35.                 {
  36.                     if ( a+b+c+d == 63 )
  37.                     {
  38.                         sum = a*b + b*c + c*d;
  39.  
  40.                         if (max_sum < sum)
  41.                         {
  42.                             max_sum = sum;
  43.                             changed_max_sum = 1;
  44.                         }
  45.  
  46.                         if ( changed_max_sum )
  47.                         {
  48.                             printf("\n Max sum is %3d  for  a = %d , b = %d , c = %d , d = %d \n", max_sum, a, b, c, d );
  49.                             changed_max_sum = 0;
  50.                         }
  51.                     }
  52.                 }
  53.  
  54.     return 0;
  55.  
  56. } // main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement