Find the largest number of 3 number
Hello everyone, surely you know how to find the greatest among 2 numbers a and b. We only have to compare a to b, larger number, then take it alone. But with 3 of a, b, c then why?
Simple way that you can think of is to compare a to b, then compare number to find the c. Very simple.
/* * Find max of a, b, c */ #include <stdio.h> int main() { int a, b, c; printf("Enter a, b and c\n"); scanf("%d%d%d", &a, &b, &c); int max = a; if(max < b) { max = b; } if(max < c) { max = c; } printf("Max is %d\n", max); return 0; }
Very simple, right. However, if we upgraded bit is finding the largest number of 4 No. Then why? Similar making severance, but will need more 1 again to check if the. With 5 number, 6 number, every time we would need more 1 if. Somewhat lengthy. So you think, how user content nhé. We build 1 find the max function between 2 number, then we can use it very convenient. Like the example below.
/* * Find max of a, b, c, d */ #include <stdio.h> int max(int x, int y) { if(x > y) return x; return y; } int main() { int a, b, c, d; printf("Enter a, b, c and d\n"); scanf("%d%d%d%d", &a, &b, &c, &d); int max3 = max( max(a, b), c ); printf("Max of a, b, c is %d\n", max3); int max4 = max( max(a, b), max(c, d) ); printf("Max of a, b, c, d is %d\n", max4); return 0; }
Very simple solution is not. Max function returns the maximum value of 2 number, then we can take it to compare with the other numbers.
In case of finding the largest number of lot number, then you go used to Plate to enter and store them, then find the largest number in the array.
int max(int x, int y)
{
if(x > and) return x;
return y;
}
Do you have a clear explanation of this function and more broadly how the machine executes this problem, thank you
This function is the function that finds large numbers in 2 number x, and. If x is large, return x via return x, reverse returns y.
int max(int x, int y)
{
if(x > and) return x;
return y;
}
why is it x,y but not a,b,c,d
x, and there is a, b is just a caller, variable name. Whatever you like, dk.