What does the following program print?#include <stdio.h>int main() { for (int i = 1; i < 10; i++) { if (i % 2 == 0) continue; printf("%d ", i); } return 0;}
Question
What does the following program print?#include <stdio.h>int main() { for (int i = 1; i < 10; i++) { if (i % 2 == 0) continue; printf("%d ", i); } return 0;}
Solution
The program prints the odd numbers from 1 to 9. Here's how it works:
-
The
forloop initializesito 1 and will continue as long asiis less than 10, incrementingiby 1 after each loop. -
Inside the loop, the
ifstatement checks ifiis divisible by 2 (i.e., ifiis an even number). If it is, thecontinuestatement is executed. This skips the rest of the current iteration and goes straight to the next iteration of the loop. -
If
iis not divisible by 2 (i.e., ifiis an odd number), the program skips theifstatement and executes theprintfstatement, which prints the value ofifollowed by a space. -
This process repeats until
iis no longer less than 10.
So, the output of the program is: 1 3 5 7 9
Similar Questions
What will print out when the following code runs?
What will be the output of the following program?
What is the output of the following program :print "Hello World"[::-1]
What is the output for the following code?
What does the program below display?full_name = "Jane"full_name = full_name + "Brown"print( full_name )*
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.