For loops

Write a for loop that prints: 1 2 … userNum

Ex: userNum = 4 prints:

1 2 3 4

 

#include <stdio.h>

int main(void) {
 int userNum;
 int i;

userNum = 4;

for(i = 1; i <= userNum; i++) {
 printf("%d ", i);
 }

return 0;
}

Write code that prints: userNum … 2 1
Your code should contain a for loop. Print a newline after each number. Ex: userNum = 3 outputs:

3
2
1
#include <stdio.h>

int main(void) {
 int userNum;
 int i;

userNum = 3;

for(i = userNum; i >= 1; i--) {
 printf("%d\n", i);
 }
 return 0;
}

 

Write code that prints: Ready! numVal … 2 1 Run!
Your code should contain a for loop. Print a newline after each number and after each line of text Ex: numVal = 3 outputs:

Ready!
3
2
1
Run!
#include <stdio.h>

int main(void) {
 int numVal;
 int i;

numVal = 3;

printf("Ready!\n");
 for(i = numVal; i >= 1; i--){
 printf("%d\n", i);
 }
printf("Run!\n");
 return 0;
}

 

Write a for loop that prints the numbers from numVal to 0. Ex: numVal = -3 outputs:

-3 -2 -1 0
#include <stdio.h>

int main(void) {
 int numVal;
 int i;

numVal = -3;

for(i = numVal; i <= 0; i++) {
 printf("%d ", i);
 }

return 0;
}

 

Write a for loop that prints from firstNumber to finalNumber. Ex: firstNumber = -3 and finalNumber = 1 outputs:

-3 -2 -1 0 1

 

#include <stdio.h>

int main(void) {
 int firstNumber;
 int finalNumber;
 int i;

firstNumber = -5;
 finalNumber = 6;

for(i = firstNumber; i <= finalNumber; i++) {
 printf("%d ", i);
 }

return 0;
}