Credit:Eli TamangEli Tamang

Tribhuwan University

Institute of Science and Technology

2081

Bachelor Level / First Year / First Semester / Science

Bachelors in Information Technology (BIT102)

(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.

Section A

Long Answers Questions

Attempt any TWO questions.
[2*10=20]
1.
What is loop? Differentiate between break and continue statement.Write a program to find sum of first N natural numbers.[6+4]

What is Loop?

A loop is a control structure that allows repeated execution of a block of statements as long as a given condition remains true.

Types of Loops

  • for loop – used when number of iterations is known
  • while loop – used when number of iterations is unknown (entry-controlled)
  • do-while loop – executes at least once (exit-controlled)

Difference Between Break and Continue Statement

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

Example

// 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.


Program to Find Sum of First N Natural Numbers

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.

2.
What is pointer? How it differs from array?Write a program to add two 2D matrices.[4+6]
3.
What is string?Explain any three string function in "string.h" header file in C with example.Write a program to check whether a word is palindrome or not.[2+3+5]
Section B

Short Answers Questions

Attempt any Eight questions.
[8*5=40]
4.
What are conversion specifiers? Explain the compilation of C program in brief. [2+3]
5.
What is structure? Write a program to read roll, name and marks of 10 students using structure and then display all records. [1+4]
6.
Explain if else ladder with syntax and semantic. Write a program to find largest among 3 different numbers. [2+3]
7.
Write a program to check whether a given number is Armstrong number or not. [5]
8.
What is operator precedence? Explain assignment and logical operator. [1+4]
9.
Explain call by reference of a function with suitable example. [5]
10.
Write a program to write N numbers in file "number.txt" and then read it and display only even numbers. [5]
11.
What is constant? Explain basic data types in C programming along with their range. [1+4]
12.
Write short notes on: a) Union b) Switch statement [0+2.5+2.5]