Note: This question helps in clearing Cognizant conceptual technical interview12345678910111213#include <stdio.h> int main() { for (int num = 1; num <= 5; num++) { if (num % 2 == 0) { continue; } for (int i = 1; i <= num; i++) { printf("%d ", i); } printf("\n"); } return 0; }
Question
Note: This question helps in clearing Cognizant conceptual technical interview12345678910111213#include <stdio.h> int main() { for (int num = 1; num <= 5; num++) { if (num % 2 == 0) { continue; } for (int i = 1; i <= num; i++) { printf("%d ", i); } printf("\n"); } return 0; }
Solution
The code you've posted is written in C language. It's a simple program that uses two for loops and an if condition. Here's a step-by-step explanation of what it does:
-
The main function is declared. This is the entry point of the program.
-
A for loop is initiated with a variable 'num' that starts at 1 and increments by 1 each time until it reaches 5.
-
Inside this loop, an if condition checks if 'num' is an even number by using the modulus operator (%). If 'num' is even, the 'continue' statement is executed. This means that the rest of the current iteration is skipped, and the loop continues with the next iteration.
-
If 'num' is not even (i.e., it's odd), another for loop is initiated with a variable 'i' that starts at 1 and increments by 1 each time until it reaches the value of 'num'.
-
Inside this second loop, the value of 'i' is printed to the console, followed by a space.
-
After the second loop finishes, a newline character is printed to the console. This means that the next print statement will start on a new line.
-
The process repeats until 'num' is greater than 5.
-
The main function returns 0, which signifies that the program has ended successfully.
So, the output of this program would be:
1 1 2 3 1 2 3 4 5
This is because the 'continue' statement skips the iterations where 'num' is even, so the inner loop only runs when 'num' is odd.
Similar Questions
what is conceptualize interaction? discuss about importance of conceptualized design?
Multiple Choice QuestionA person using ______ skills has the ability to think in abstract terms and see how parts fit together to form the whole.Multiple choice question.technicalhuman relationsoperationalconceptual
describe in detail cognitive framework
Describe in detail cognitive framework
Describe in detail cognitive framework
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.