1.) In order to have a loop that adds odd values from 10 through
20, what would be entered in the blanks below:
int oddAccumulator = 0;
for (int i = [a]; i < [b];
i++)
{
if ((i % 2) != 0)
{
oddAccumulator += [c];
}
}
WriteLine("The sum of odd integers in the range of 10 - 20 is: {0}
", oddAccumulator);
*I've figured out the answer for [a] = 10, [b] = 20, but I can't figure out [c]. Please answer [c].
2.) n order to have a loop that displays the values 5 through 15
on separate lines, what would be entered in the blank below:
for (int i = 5; i < ______; i++)
{
WriteLine(i);
}
*I've put 15 and 20, which is wrong. Please answer with explanation.
Solution
[a]=10
[b]=20
[c]=i
Explanation
i value is initially 10 the loop will execute upto I<20
if ((i % 2) != 0)
this loop will work only for odd numbers
11,13,15,17,19
oddoddAccumulator=oddAccumulator +i;
oddAccumulator =0+11
oddAccumulator=11
so it will accumulate odd numbers one by one
Correct Code
int oddAccumulator = 0;
for (int i =10; i < 20;
i++)
{
if ((i % 2) != 0)
{
oddAccumulator += i;
}
}
WriteLine("The sum of odd integers in the range of 10 - 20 is: {0}
", oddAccumulator);
--
2)
Answer
16
Explanation
for example if you put 15, it will print from 5,6,7,8...14
so 16 will be the correct answer
i<16
this will execute 15<16
Correct code
for (int i = 5; i < 16; i++)
{
WriteLine(i);
}
--
all the best
please upvote
Get Answers For Free
Most questions answered within 1 hours.