4- For each of the following outputs, write a code segment using either a for or
ID: 3537186 • Letter: 4
Question
4- For each of the following outputs, write a code segment using either a for or a while loop that would produce it (you may need to use a nested loop). The code segment must output one value at every iteration and may not use arrays. For example, the output 0 1 2 3 4 5 can be produced by the following code segment:
int i = 0;
while (i < 6)
{
System.out.print(i + %u201C %u201C);
i++;
}
Output 1: 1 3 5 7 9 11 13
Output 2: 13 10 7 4 1 -2
Output 3: 1 1 2 2 3 3 4 4 5 5
Output 4: 1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
Output 5: 1 2 4 7 11 16 22
Explanation / Answer
Ans 1:
for(int i=1;i<=13;i=i+2)
System.out.print(i+" ");
Ans2:-
for(int i=13;i>=-2;i=i-3)
System.out.print(i+" ");
Ans 3:
for(int i=1;i<=5;i++)
System.out.print(i+" "+i+" ");
Ans 4:
for(int i=1;i<=6;i++)
System.out.print(i+" ");
Ans 5:-
for(int d=1,i=1;i<=22;i=i+d,d++)
System.out.print(i+" ");
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.