Semester
Subject
Year
Tribhuwan University
2081.2
Bachelor Level / First Year / First Semester / Science
(C Programming)
Full Marks: 60
Pass Marks: 24
Time: 3 Hours
Candidates are required to give their answers in their own words as for as practicable.
The figures in the margin indicate full marks.
Long Answers Questions
An odd loop is a loop in which the number of iterations (repetitions) is not known in advance and depends on some condition evaluated during runtime.
Common implementation:
char ch = 'y';
while(ch == 'y')
{
printf("Enter data: ");
// process data
printf("Continue? (y/n): ");
scanf(" %c", &ch);
}
Here, the loop runs an odd (unknown) number of times depending on user input.
goto is an unconditional branching statement in C that transfers the control of the program to a specified label without checking any condition.
A label is a user-defined identifier followed by a colon (:) that marks a position in the program where control can be transferred using goto.
Syntax:
goto label_name;
// ...
label_name:
statement;
Key Points:
Example:
printf("Hello\n");
goto skip;
printf("This will be skipped\n");
skip:
printf("World\n");
Output: Hello → World (middle statement is skipped)
Pattern:
1
2 2
3 3 3
4 4 4 4
Program in C:
#include <stdio.h>
int main()
{
int i, j;
for(i = 1; i <= 4; i++)
{
for(j = 1; j <= i; j++)
{
printf("%d ", i);
}
printf("\n");
}
return 0;
}
Explanation:
i) runs from 1 to 4, representing each row.j) runs from 1 to i, printing the value of i repeatedly.\n moves the cursor to the next line.Dry Run:
12 23 3 34 4 4 4Conclusion: Odd loops are useful when iteration count is runtime-dependent, goto provides unconditional branching to a label, and nested for loops are the standard approach for generating number patterns.
Short Answers Questions