required iterations is known. Source: Section 5.8 “Focus on Software Engineering
” Starting out with C++ Alternate 2nd Edition, by Tony Gaddis ...
Looping Exercises Deciding Which Loop to Use At this time you have learned four looping constructs (also called repetition statements). Each of these four loops are ideal to use in different situations. • The while Loop The while loop is a pre-test loop. It is ideal in situations where you do not want the loop to iterate if the condition is false from the beginning. It is also ideal if you want to use a sentinel. • The while-break Loop The while-break loop is a mid-test loop. It is ideal in situations where you want to test an exit condition within the loop.
• The do-while Loop The do-while loop is a post test loop. It is ideal in situations where you always want the loop to iterate at least once. • The for Loop The for loop is a pre-test loop that first executes an initialization expression. In addition, it automatically executes an update expression at the end of each iteration. It is ideal for situations where a counter variable is needed. The for loop is primarily used when the exact number of required iterations is known. © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Source: Section 5.8 “Focus on Software Engineering” Starting out with C++ Alternate 2nd Edition, by Tony Gaddis
8.2
Essentials of Counter-Controlled Repetition
• For each question write down these 3 elements of countercontrolled repetition
– Data type, name, and initial value of the control variable int counter = 2;
– The condition that tests for the final value of the control variable counter = 3, of this sequence is given by an = an-1 + an-2 Thus a3 = a2 + a1 =1+1 = 2, a4 = a3 + a2 =1+2 = 3, and so on… Such a sequence is called a Fibonacchi sequence. Write a program that given any first two numbers, using this algorithm, determine the nth number an, n 12 16 The first two Fibonacci numbers are 12 and 16 Enter the desired Fibonacci number to be determined -> 10 The 10th Fibonacci number is 796. Source: Chapter 5 “Control Stucture II” C++ Programming, by D.S. Malik © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
More Programming Exercises 1. An integer is divisible by 9 if the sum of its digits is divisible by 9. Write a program that prompts the user to input an integer. The program should then output the number and a message string stating whether the number is divisible by 9. It does so by first adding the digits and then checking whether the sum of the digits is divisible by 9. 2. Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. For example, it should output the individual digits of 3456 as 3 4 5 6; output the individual digits of 8030 as 8 0 3 0; output the individual digits of 2345526 as 2 3 4 5 5 2 6; output the individual digits of 4000 as 4 0 0 0; and output the individual digits of-2345 as 2 3 4 5 . 3. Write a program that prompts the user to input an integer and then outputs the number with the digits reversed. For example, if the input is 12345, the output should be 54321.Your program must also output 5000 as 0005 and 980 as 098. 4. Write a program that reads a set of integers, and then finds and prints the sum of the even and odd integers. 5. Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (Note: An even number is prime if it is divisible by 2. An odd integer is prime if it is not divisible by any odd integer less than or equal to the square root of the number.) Source: Chapter 5 “Control Stucture II” C++ Programming, by D.S. Malik © Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.