Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- four_integers_sum_63.c
- Task:
- If a, b, c, d are positive integers, with a sum of 63,
- what is the maximum value of ab + bc + cd ?
- Answer: maximum sum is 991
- for a=1, b=30, c=31, d=1
- or
- for a=1, b=31, c=30, d=1
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- https://www.facebook.com/dmilicev
- */
- #include <stdio.h>
- int main(void)
- {
- int a, b, c, d, sum, max_sum=0, changed_max_sum=0;
- // maximum value for one integer is 60 when rest three integers are equal to 1.
- for(a=1; a<=60; a++)
- for(b=1; b<=60; b++)
- for(c=1; c<=60; c++)
- for(d=1; d<=60; d++)
- {
- if ( a+b+c+d == 63 )
- {
- sum = a*b + b*c + c*d;
- if (max_sum < sum)
- {
- max_sum = sum;
- changed_max_sum = 1;
- }
- if ( changed_max_sum )
- {
- printf("\n Max sum is %3d for a = %d , b = %d , c = %d , d = %d \n", max_sum, a, b, c, d );
- changed_max_sum = 0;
- }
- }
- }
- return 0;
- } // main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement