Tribhuwan University

Institute of Science and Technology

2081.2

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.
Define odd loop. What do you mean by goto and label? Write a program to generate the following pattern.
11
222 2
3333 3 3
44444 4 4 4
[10]

A. Odd Loop (Definition)

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.

  • In an odd loop, the user or some event decides when the loop should stop.
  • The exact number of times the loop body executes is unpredictable at compile time.
  • Example situations: Reading input until user says "no", processing data until end-of-file is reached.

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.


B. Goto and Label (Definition)

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:

  • goto causes an unconditional jump to the labeled statement.
  • The label must be in the same function as the goto statement.
  • Excessive use of goto makes code difficult to read and debug (spaghetti code), so it is generally avoided in structured programming.

Example:

printf("Hello\n");
goto skip;
printf("This will be skipped\n");
skip:
printf("World\n");

Output: Hello → World (middle statement is skipped)


C. Program to Generate the Pattern

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:

  • The outer loop (i) runs from 1 to 4, representing each row.
  • The inner loop (j) runs from 1 to i, printing the value of i repeatedly.
  • After the inner loop completes, \n moves the cursor to the next line.

Dry Run:

  • When i = 1 → inner loop runs 1 time → prints: 1
  • When i = 2 → inner loop runs 2 times → prints: 2 2
  • When i = 3 → inner loop runs 3 times → prints: 3 3 3
  • When i = 4 → inner loop runs 4 times → prints: 4 4 4 4

Conclusion: 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.

2.
When do we need external function?Illustrate the process of swapping the value of two variables using the concept of pass by value and pass by reference.[3+7]
3.
Differentiate between fgets() and fgetc(). What is the task of typedef? Write a program to define a structure Book with data members name, price and author. Display the names of the book having price greater than 1000.[10]
Section B

Short Answers Questions

Attempt any Eight questions.
[8*5=40]
4.
Write an algorithm and flowchart to test whether the given integer is odd or even. [5]
5.
Define token. Describe any three types of statements. [5]
6.
List any five types of functions for string manipulation with their uses. [5]
7.
What is the task of sizeof and comma operator? Discuss about type conversion. [5]
8.
What is union? Differentiate between pointer and array. [5]
9.
Differentiate between switch statement and if else if ladder. [5]
10.
Give any two types of logical operators. What are formatted and unformatted I/O means? [5]
11.
Define pre-processor. How do you pass pointer as argument? [5]
12.
Write a program to add two 3×3 matrices. [5]