51.5k views
4 votes
Assume the int variables i and result, have been declared but not initialized. Write a for loop header -- i.e. something of the form for ( . . . ) for the following loop body: result = result * i; When the loop terminates, result should hold the product of the odd numbers between 10 and 20. NOTE: just write the for loop header; do not write the loop body itself.

2 Answers

2 votes

Final answer:

The for loop header the student needs to calculate the product of odd numbers between 10 and 20 is: for(i = 11; i <= 19; i += 2) { }.

Step-by-step explanation:

The student is asking for a for loop header that will iterate over the odd numbers between 10 and 20 and multiply them together. The initial product value, stored in the variable result, should be set to 1, as any number multiplied by 1 is the number itself. This is a programming task likely related to Java or a similar programming language. The loop header should initialize i to the first odd number greater than 10, which is 11, and increment it by 2 to skip even numbers, stopping once it goes past 20. Since we do not want to include 20 in the computation and we are only interested in odd numbers, the stopping condition should be when i is less than or equal to 19 (the last odd number before 20).

The for loop header that achieves the desired functionality will look like this: for(i = 11; i <= 19; i += 2) { }

Note that the actual multiplication (result = result * i;) would be done inside the loop body, thus not included in this task as per the requirement.

User GProst
by
8.6k points
2 votes

Answer:

Loop header :- for(i=11;i<20;i+=2)

Step-by-step explanation:

We want to find the product of odd integers between 10 and 50.Since 10 and 20 are even numbers we can exclude them and start the loop by 11 and end it on 19 and increasing the iterator i by 2 so that it is odd only .For example:- i=11 increasing by 2 then i = 13 which is odd.

User DaveEdelstein
by
8.2k points