Semester
Subject
Year
Tribhuwan University
2081
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
A loop is a control structure that allows repeated execution of a block of statements as long as a given condition remains true.
| Feature | break | continue |
|---|---|---|
| Function | Terminates the entire loop immediately | Skips the current iteration and moves to next iteration |
| Control | Transfers control outside the loop | Transfers control to the loop's condition check |
| Effect | Loop stops completely | Loop continues with next iteration |
| Use case | Exit loop when a condition is met | Skip specific values during iteration |
| Example | Breaking out when element is found | Skipping negative numbers in a sum |
// break example
for(int i = 1; i <= 10; i++) {
if(i == 5)
break; // loop ends at i=5
printf("%d ", i);
}
// Output: 1 2 3 4
// continue example
for(int i = 1; i <= 10; i++) {
if(i == 5)
continue; // skips i=5, continues loop
printf("%d ", i);
}
// Output: 1 2 3 4 6 7 8 9 10
Conclusion: Break exits the loop entirely, while continue only skips the current iteration and the loop keeps running.
The sum of first N natural numbers means adding all numbers from 1 to N.
#include <stdio.h>
int main() {
int n, i, sum = 0;
printf("Enter the value of N: ");
scanf("%d", &n);
for(i = 1; i <= n; i++) {
sum = sum + i;
}
printf("Sum of first %d natural numbers = %d", n, sum);
return 0;
}
Sample Output:
Enter the value of N: 5
Sum of first 5 natural numbers = 15
Explanation: The for loop runs from 1 to N, adding each value of i to the variable sum. After the loop ends, the total sum is displayed.
Short Answers Questions