"Learning the path to iOS: C language" If loop application exercises

Source: Internet
Author: User
Tags float max

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, &AMP;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, &AMP;N2, &AMP;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.