C language programming exercise questions reference answers Chapter 3 (2) Selection Structure

Source: Internet
Author: User

/* 3.6 calculates the maximum value among the three numbers. Similar to example 1.2 */
# Include <stdio. h>
Void main ()
{
Int A, B, C, Max;
Printf ("\ n please enter three integers separated by spaces: \ n ");
Scanf ("% d", & A, & B, & C );
If (A> B) max =;
Else max = B;
If (max <c) max = C;
Printf ("the maximum value is % d \ n", max );
}

/* 3.6 calculates the maximum value among the three numbers. Similar to example 1.2 */
# Include <stdio. h>
Void main ()
{
Int A, B, C, Max;
Printf ("\ n please enter three integers separated by spaces: \ n ");
Scanf ("% d", & A, & B, & C );
Max = A> B? A: B;
Max = max> C? MAX: C;
Printf ("the maximum value is % d \ n", max );
}

/* 3.7 enter X to calculate the value of Y. Similar to example 3.13, you can also use the switch statement */
# Include <stdio. h>
# Include <math. h>
Void main ()
{
Float X, Y;
Printf ("\ n please input a real number: \ n ");
Scanf ("% F", & X );
If (x> = 0 & x <10)
Y = sin (X );
If (x> = 10 & x <20)
Y = cos (X );
If (x> = 20 & x <30)
Y = exp (x)-1;
If (x> = 30 & x <40)
Y = Log (x + 1 );
If (x <0 | x> = 40)
Printf ("undefined ");
Else
Printf ("Y = % F \ n", y );
}
/* 3.7 enter X to calculate the value of Y. Similar to example 3.13, solution 2 */
# Include <stdio. h>
# Include <math. h>
Void main ()
{
Float X, Y;
Printf ("\ n please input a real number: \ n ");
Scanf ("% F", & X );

If (x> 0 & x <= 40)
Switch (INT) (X/10 ))
{
Case 0: Y = sin (x); printf ("Y = % F \ n", Y); break;
Case 1: Y = cos (x); printf ("Y = % F \ n", Y); break;
Case 2: Y = exp (x)-1; printf ("Y = % F \ n", Y); break;
Case 3: Y = Log (x + 1); printf ("Y = % F \ n", Y); break;
}

Else

Printf ("undefined ");
}

/* 3.7 enter X to calculate the value of Y. Solution 3 */

# Include <stdio. h>
# Include <math. h>
Void main ()
{
Float X, Y;
Scanf ("% F", & X );
If (x <0 | x> = 40)
Printf ("no definition \ n ");
Else if (x> 30)
Y = Log (x + 1 );
Else if (x> 20)
Y = exp (x)-1;
Else if (x> 10)
Y = cos (X );
Else
Y = sin (X );
If (x> = 0 & x <40)
Printf ("Y = % F \ n", y );
}

/* 3.8 enter a percentage system score and give a prompt. Similar to example 3.14, you can also use the switch statement */

# include "stdio. H "
void main ()
{< br> int score;
printf (" enter a score (integer), for example 85 \ n ");
scanf ("% d", & score);
If (score> = 90 & score <= 100)
printf ("Excellent \ n");
If (score> = 80 & score <= 89)
printf ("good \ n ");
If (score> = 70 & score <= 79)
printf ("Middle \ n ");
If (score >=60 & score <= 69)
printf ("Pass \ n");
If (score <60)
printf ("fail \ n");
}

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.