【文章內(nèi)容簡(jiǎn)介】
irst year bees the principal amount for the next year and so on.) 6/2/2022 Chapter 6 Decision Making and Looping main ( ) { int n。 float p, pp, r, v。 char ch。 for ( p = 1000。 p = 10000。 p = p + 1000 ) { printf ( \nPrincipal amount: %.0f\nrate , p )。 for ( n = 1。 n = 8。 n++ ) if ( n == 1 ) printf ( %d year, n )。 else printf ( %d years, n )。 for ( r = 。 r 。 r = r + ) { pp = p。 printf ( \n%.2f: , r)。 for ( n = 1。 n = 8。 n++ ) { v = pp * ( 1 + r )。 pp = v。 printf(%,v)。 } } printf ( \nPress enter key to continue... )。 scanf ( %c, amp。ch )。 } } 6/2/2022 Chapter 6 Decision Making and Looping Write programs to print the following outputs using for loops. (a) 1 (b) ***** 22 **** 333 *** 4444 ** 55555 * 6/2/2022 Chapter 6 Decision Making and Looping main ( ) /* (a) */ { int i, j。 for ( i = 1。 i = 5。 i++ ) { for ( j = 1。 j = i。 j++ ) printf ( %d, i )。 printf ( \n )。 } } 6/2/2022 Chapter 6 Decision Making and Looping main ( ) /* (b) */ { int i, j。 for ( i = 5。 i = 1。 i ) { for ( j = 1。 j = 5 i。 j++ ) printf ( )。 for ( j = 1。 j = i。 j++ ) printf ( * )。 printf ( \n )。 } } 6/2/2022 Chapter 6 Decision Making and Looping Write a program to read the age of 100 persons and count the number of persons in the age group 50 to 60. Use for and continue statements. 6/2/2022 Chapter 6 Decision Making and Looping main ( ) { int age, count = 0, i。 for ( i = 1。 i = 100。 i++ ) { printf ( Please input the age of person %d:, i )。 scanf ( %d, amp。age )。 if ( age 50 || age 60 ) continue。 count ++ 。 } printf ( The number of persons in the age group 50 to 60 is %d.\n, count )。 } 6/2/2022 Chapter 6 Decision Making and Looping Write a program to print a table of values of the function y = exp(x) for x varying from to in steps of . The table should appear as follows: Table for Y = EXP(X) ________________________________________________ x …… …… ________________________________________________ 6/2/2022 Chapter 6 Decision Making and Looping include main ( ) { double x = 0。 int i, j。 printf ( \nY = EXP(X)\n )。 for ( i = 1。 i = 80。 i++ ) printf ( _ )。 printf (\n x\t)。 for ( i = 1。 i = 9。 i++ ) printf ( \t0.%d, i)。 for ( i = 0。 i = 9。 i++ ) { printf ( \n\n%, i )。 for ( j = 0。 j = 9。 j++ ) { x = i + *j。 printf ( \t%.2le, exp(x) )。 } } for ( i = 1。 i = 80。 i++ ) printf ( _ )。 } 6/2/2022 Chapter 6 Decision Making and Looping Write a program that will read a positive integer and determine and print its binary equivalent. (Hint: The bits of the binary representation of an integer can be generated by repeatedly dividing the number and the successive quotients by 2 and saving the remainder, which is either 0 or 1, after each division.) 6/2/2022 Chapter 6 Decision Making and Looping main ( ) { long number, n。 int m = 0, i, j。 printf ( Please input a positive number: )。 scanf ( %ld, amp。number )。 n = number。 while ( n 0) { n = n / 2。 m ++。 } printf ( The equivalent binary of %ld is: , number )。 for ( i = m。 i = 1 。 i ) { n = number。 for ( j =1。 j i。 j++ ) n = n / 2。 printf ( %ld, n % 2 )。 } } 6/2/2022 Chapter 6 Decision Making and Looping Write a program using for and if statement to display the capital letter S in a grid of 15 rows and 18 columns shown below. ******************* ******************* ******************* **** **** **** ******************* ******************* ******************* **** **** **** ******************* ******************* ******************* 6/2/2022 Chapter 6 Decision Making and Looping main ( ) { int i, j。 for ( i = 1。 i = 15。 i++ ) { printf ( \n )。 if ( i =4 amp。amp。 i =6 ) printf ( **** )。 else if ( i =10 amp。amp。 i =12 ) printf ( %18s, **** )。 else for ( j = 1。 j = 18。 j++ ) printf ( * )。 } } 6/2/2022 Chapter 6 Decision Making and Looping Write a program to pute the value of Euler39。s number e, that is used as the base of natural logarithms. Use the following formula. e = 1 + 1/1! + 1/2 ! + 1/3 ! + …… + 1/n! Use a suitable loop construct. The loop must terminate when the difference between two successive values of e is less than . 6/2/2022 Chapter 6 Decision Making and Looping define ERROR main ( ) { float e = 1, item。 int i, m = 1。 long n。 printf ( e=1 )。 do { for ( n = 1, i = 1。 i = m。 i++ ) n = n * i。 item = / n。 e = e + item。 printf ( +1/%d!, m )。 m++。 } while ( item = ERROR )。 printf ( =%f\n, e )。 } e=1+1/1!+1/2!+1/3!+1/4!+1/5!+1/6!+1/7!+1/8!+1/9!= 6/2/2022 Chapter 6 Decision Making and Looping The present value (popularly known as book value) of an item is given by the relationship. P = c(1d)n where c = original cost d = rate of depreciation (per year) n = number of years P = present value after n years If P is considered the scrap value at the end of useful life of the item, write a program to pute the useful life in years given the original cost, depreciation rate, and the scrap value. The program should request the user to input the data interactively. 6/2/2022 Chapter 6 Decision Making and Looping main ( ) { float c, d, p。 int n, i。 printf ( The original cost of the item is: )。 scanf ( %f, amp。c )。 printf ( The rate of depreciation (per year)