1. Find the maximum value in three numbers
<span style= "FONT-SIZE:14PX;" ></span><pre name= "code" class= "CPP" ><span style= "FONT-SIZE:14PX;" >//Method 1: Find the maximum of two numbers first, then compare the .</span> int n1 = 0 with the maximum and the third, N2 = 0, n3 = 0; printf ("Please enter three number: \ n"); scanf ("%d%d%d", &n1, &n2, &N3); int max = 0;//defines a maximum value with an initial value of 0 if (N1 > n2) {max = N1; } else {max = N2; }//Replace with a conditional expression with max = n1 > n2? N1:N2; if (Max < n3) {max = N3; }//max = max > n3? Max:n3; printf (maximum: max =%d, max); Method 2: Use N1 respectively and N2, N3 comparison. <pre name= "code" class= "CPP" ><pre name= "code" class= "CPP" > int N1 = 0, N2 = 0, n3 = 0; printf ("Please enter three number: \ n"); scanf ("%d%d%d", &n1, &N2, &N3); int max = 0; if (N1 >n2) { I F (N1 > N3) { max = n1; } else { max = n3; } } else if (N2 > N3) { & nbsp; max = n2; } else { max = n3; } Equivalent to Max = n1 > n2? N1 > N3? N1:n3:n2? n3:n2:n3 printf (maximum: max =%d, max);
2. Enter three numbers to print out the middle value (i.e. the second largest value) in two ways
Tip: First, the maximum minimum is first, and the second, only the conditional operator is used
Method 1 float x = 0.0; Float y = 0.0; float z = 0.0; float max = 0.0;//define max float min = 0.0;//define minimum float mid = 0.0;//Define intermediate value printf ("Please input three number:\n") ; scanf ("%f%f%f", &x, &y, &z); max = x > y? x > Z? X:z: y > Z? Y:z; min = x < y? x < Z? X:z: Y < z? Y:z; Mid = x + y + z-min-max; printf ("The mid number is:%.2f\n", mid); Method 2 float x = 0.0; Float y = 0.0; float z = 0.0; float max = 0.0;//defines the maximum value of float min = 0.0;//defines the minimum value of float mid = 0.0;//defines the median value printf ("Please input three number:\n"); scanf ("%f%f%f", &x, &y, &z); if (x >= y) {if (y >= z) {mid = y; } else {if (x <= z) {mid = X; } else {mid = Z; }}}} else {if (y <= z) {mid = y; } else if (x > z) {mid = y; } else {mid = x; }} printf ("The mid number is:%.2f\n", mid);
"Learning the path to iOS: C language" If loop application exercises