Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Q2: (Integer Sequences with While Loops) (20 points): Loops are very useful for

ID: 3879601 • Letter: Q

Question

Q2: (Integer Sequences with While Loops) (20 points): Loops are very useful for counting and generating sequences of integers. If you can use a variable with a loop to count from 0 to n, then you can often transform that variable n using a formula or function such that f(n) results in a new integer sequence. Write a program that prompts the user to enter a number between 2 and 20, and then uses a series of while loops to generate some interesting integer sequences. The loop counters should all start at zero, but the transformed number may start at a number other than zero. The exact prompts and outputs needed can be found in the sample output given below. Use 2 spaces between each integer printed. Your program output should look as follows: Sample Input and Output: Enter a number between 2 and 20: 8 The first 8 whole numbers: 0 1 2 3 4 5 6 7 The first 8 even whole numbers: 0 2 4 6 8 10 12 14 The first 8 odd whole numbers: 1 3 5 79 11 13 15 The first 8 whole numbers counting by 7: 0 7 14 21 28 35 42 49 The first 8 perfect squares: 0 1 4 9 16 25 36 49

Explanation / Answer

#include "stdio.h"

main() {
// DECLARING variables
int n;
int counter = 0;
  
// taking user input
printf("Enter a number between 2 and 20: ");
scanf("%d",&n);
  
// for the first part
printf(" The first %d whole numbers: ",n);
while(counter < n)
{
printf ("%d ",counter);
counter++;
}
  
// for the second part
counter = 0;
printf(" The first %d even whole numbers: ",n);
while(counter < n*2)
{
printf ("%d ",counter);
counter = counter+2;
}
  
// for the third part
counter = 1;
printf(" The first %d odd whole numbers: ",n);
while(counter < n*2)
{
printf ("%d ",counter);
counter = counter+2;
}
  
// for the fourth part
counter = 0;
printf(" The first %d even whole numbers: ",n);
while(counter < n*7)
{
printf ("%d ",counter);
counter = counter+7;
}
}
/*SAMPLE OUTPUT
Enter a number between 2 and 20: 9

The first 9 whole numbers:
0 1 2 3 4 5 6 7 8

The first 9 even whole numbers:
0 2 4 6 8 10 12 14 16

The first 9 odd whole numbers:
1 3 5 7 9 11 13 15 17

The first 9 even whole numbers:
0 7 14 21 28 35 42 49 56
*/

/*

NOTE: One question at a time please -- Chegg Policy

*/