【正文】
unt to be paid is %.2f\n, price*(1discount))。 } 6/2/2022 Chapter 5 Decision Making and Branching Write a program that will read the value of x and evaluate the following using (a) nested if statements (b) else if statements, and (c) conditional operator ? : ??????????010001xf o rxf o rxf o ry6/2/2022 Chapter 5 Decision Making and Branching main ( ) /* (a) */ { float x。 printf ( Please input the value of x: )。 scanf ( %f, amp。x )。 if ( x != 0 ) { if ( x 0 ) printf ( y = 1\n )。 else printf ( y = 1\n )。 } else printf ( y = 0\n )。 } 6/2/2022 Chapter 5 Decision Making and Branching main ( ) /* (b) */ { float x。 printf ( Please input the value of x: )。 scanf ( %f, amp。x )。 if ( x 0 ) printf ( y = 1\n )。 else if ( x == 0 ) printf ( y = 0\n )。 else printf ( y = 1\n )。 } 6/2/2022 Chapter 5 Decision Making and Branching main ( ) /* (c) */ { float x。 printf ( Please input the value of x: )。 scanf ( %f, amp。x )。 printf ( y = %d\n, x 0 ? 1 : x 0 ? 1 : 0 )。 } 6/2/2022 Chapter 5 Decision Making and Branching Write a program to pute the real roots of a quadratic equation The roots are given by the equations The program should request for the values of the constants a, b and c and print the values of x1, and x2. Use the following rules: (a) No solution, if both a and b are zero (b) There is only one root, if a=0 (x=c/b) (c) There are no real roots, if b24ac is negative (d) Otherwise, there are two real roots Test your program with appropriate data so that all logical paths are working as per your design. Incorporate appropriate output messages. aacbbxaacbbx2424 2221????????02 ??? cbxax6/2/2022 Chapter 5 Decision Making and Branching include main ( ) { float a, b, c, f。 printf ( Please input the value of a, b and c in the quadratic equation:\n ax*x + bx + c = 0\n )。 printf ( a = )。 scanf ( %f, amp。a )。 printf ( b = )。 scanf ( %f, amp。b )。 printf ( c = )。 scanf ( %f, amp。c )。 printf ( About %.2fx*x + %.2fx + %.2f = 0: , a, b, c )。 if ( a == 0 amp。amp。 b == 0 ) printf(There is no solution!\n)。 else if ( a == 0 ) printf(There is only one root: %.2f.\n, c/b)。 else { f = b * b 4 * a * c。 if ( f 0 ) printf(There are no real roots!\n)。 else printf (There are 2 real roots: %.2f, %.2f.\n, (b + sqrt(f))/2/a, (b sqrt(f))/2/a )。} } 6/2/2022 Chapter 6 Decision Making and Looping Given a number, write a program using while loop to reverse the digits of the number. For example, the number 12345 should be written as 54321. (Hint: Use modulus operator to extract the last digit and the integer division by 10 to get the n1 digit number from the n digit number.) 6/2/2022 Chapter 6 Decision Making and Looping main ( ) { long n。 printf ( Input a positive integer: )。 scanf ( %ld, amp。n )。 while ( n != 0 ) { printf ( %d, n % 10 )。 n = n / 10。 /* number is decreased by 10 times */ } } 6/2/2022 Chapter 6 Decision Making and Looping The factorial of an integer m is the product of consecutive integers from 1 to m. That is, factorial m = m! = m (m1) …… 1. Write a program that putes and prints a table of factorials for any given m. 6/2/2022 Chapter 6 Decision Making and Looping main ( ) { int m。 long n = 1。 printf ( Please input a number: )。 scanf ( %d, amp。m )。 printf ( %d!=, m )。 while ( m = 1 ) { n *= m。 m 。 } printf ( %ld\n, n )。 } 6/2/2022 Chapter 6 Decision Making and Looping Write a program to pute the sum of the digits of a given integer number. 6/2/2022 Chapter 6 Decision Making and Looping main ( ) { long n。 int sum = 0。 printf ( Input a positive integer: )。 scanf ( %ld, amp。n )。 while ( n != 0 ) { sum = sum + n % 10 。 n = n / 10。 /* number is decreased by 10 times */ } printf ( The sum of the digits is %d.\n, sum )。 } 6/2/2022 Chapter 6 Decision Making and Looping The numbers in the sequence 1 1 2 3 5 8 13 21 …… are called Fibonacci numbers. Write a program using a do...while loop to calculate and print the first m Fibonacci numbers. (Hint: After the first two numbers in the series, each number is the sum of the two preceding numbers.) 6/2/2022 Chapter 6 Decision Making and Looping main ( ) { long f1 = 1, f2 = 1, f = 1。 int i = 2, m。 printf ( How many numbers do you want to output? )。 scanf ( %d, amp。m )。 printf ( %12ld , f )。 while ( i = m ) { printf ( %12ld , f )。 f = f1 + f2。 f1 = f2。 f2 = f。 if ( i % 4 == 0 ) printf ( \n )。 i ++。 } } 6/2/2022 Chapter 6 Decision Making and Looping Rewrite the program of the Example using the for statement. 6/2/2022 Chapter 6 Decision Making and Looping main ( ) { int count, n。 float x, y。 printf ( Enter the values of x and n: )。 scanf ( %f %d, amp。x, amp。n)。 for ( y = , count = 1。 count = n。 count++ ) y = y * x 。 printf ( \nx = %f。 n = %d。 x to power n = %f\n, x, n, y )。 } 6/2/2022 Chapter 6 Decision Making and Looping Write a program to evaluate the following investment equation V = P(1+r)n and print the tables which would give the value of V for various bination of the following values of P, r and n. P: 1000, 2022, 3000, ……, 10,000 r: , , , ……, n: 1, 2, 3, ……, 8 (I change the value of n for format.) (Hint: P is the principal amount and V is the value of money at the end of n years. This equation can be recursively written as V=P(1+r) P=V That is, the value of money at the end of f