NGF College of Engineering & Technology
Department of Applied Science Humanities
Subject: programming for problem solving Session: July-Dec 2025
Max Marks: 30
Class: Btech AI/ML (code- csu-101-v) 1st Sem Sessional/MID Term: 2 Max Time: 90 Min
Note: Question No. 1 is Compulsory and attempt any three from rest.
Q1. Briefly explain any three from the following: 2* 3 = 06 Mks
a) What is the syntax of if statement? (CO 2)
b) What is a function? (CO 2)
c) How structure is represented? (CO 3)
d) Define String and how can we declare a string in c. (CO 2)
e) Difference between the built-in and user defined function? (CO 3)
[Link] Recursion with the example of fibbonacci series. (CO1) 8 mks
Q 3)(a) Difference between call by value and call by reference with suitable example.
(CO1) 4 mks.
Q3: (b) Write a program for factorial use Recursion? (CO1) 4 mks
Q 4) Define the sorting algorithm(bubble, insertion, selection with their algorithm .
(CO 2) 8 Mks
Q 5)Explain pointer with an example. Write a programme to explain the concept of Structure in c .
(CO 1) 8 Mks.
------------------------------------- ALL THE BEST---------------------------------
Q1. Compulsory: Briefly Explain any Three (2*3 = 6 Mks)
a) What is the syntax of if statement?
The if statement is a control flow statement that allows a program to execute a block of code only
if a specified condition is true.
The basic syntax in C (and many other languages) is:
C
if (condition) {
// Code to be executed if 'condition' is true
}
An extended form, the if-else statement, executes a different block of code if the condition is
false:
C
if (condition) {
// Code if true
} else {
// Code if false
}
b) What is a function?
A function is a self-contained block of statements that performs a specific, well-defined task.
Functions are used to organize code, promote reusability, and make a program modular.
Key characteristics:
They may accept input values (called arguments or parameters).
They perform a sequence of operations.
They may produce an output value (called the return value).
Example (C): int add(int a, int b) { return a + b; }
c) How structure is represented?
A Structure (often abbreviated as struct in C) is a user-defined data type that allows you to
logically group related data items of different data types under a single name.
A structure is represented (declared) using the struct keyword, followed by a tag (the structure's
name) and a list of its members enclosed in curly braces.
Syntax for Declaration and Definition:
C
// 1. Structure Declaration
struct Student {
int roll_number; // Member 1 (int type)
char name[50]; // Member 2 (array of char type)
float percentage; // Member 3 (float type)
};
// 2. Variable Definition (Creating a variable of the structure type)
struct Student s1; // s1 is a variable of type 'struct Student'
d) Define String and how can we declare a string in C.
Definition: In C, a String is fundamentally a sequence of characters that is stored in a
contiguous block of memory and is terminated by a null character (\0). Strings are treated
as a special type of character array.
Declaration in C: Since a string is a character array, it's declared using the char data type.
o Method 1: Using a character array (most common):
char myString[20] = "Hello";
o Method 2: Initializing character by character (includes \0):
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
e) Difference between the built-in and user defined function?
Feature Built-in Function (Library Function) User-defined Function
Functions that are pre-defined and Functions that are created by the
Definition supplied with the compiler, stored in programmer to perform specific tasks
standard libraries. required by the program.
Accessible by including the relevant
Must be explicitly declared (prototype) and
Availability header file (e.g., <stdio.h>,
defined by the user.
<math.h>).
The programmer only calls them; they The programmer has complete control over
Control
cannot change their implementation. their design and implementation.
A function you write to calculate simple
Example (C) printf(), scanf(), sqrt(), strlen().
interest or a factorial.
Q2. Illustrate Recursion with the Example of Fibonacci Series
(8 Mks)
What is Recursion?
Recursion is a programming technique where a function calls itself, either directly or indirectly, to
solve a problem. It is an alternative to iteration (loops).
Key components of a recursive function:
1. Base Case: A condition that stops the recursion. It solves the simplest version of the
problem without further calls. Crucial to prevent infinite loops.
2. Recursive Step: The part where the function calls itself to solve a smaller subproblem.
Fibonacci Series using Recursion
The Fibonacci sequence is defined as a series where each number is the sum of the two preceding
ones, starting from 0 and 1.
$F(0) = 0$
$F(1) = 1$
$F(n) = F(n-1) + F(n-2)$, for $n > 1$.
C Program:
C
#include <stdio.h>
// Function to calculate the n-th Fibonacci number recursively
int fibonacci(int n) {
// 1. Base Case: Stops the recursion for the first two numbers
if (n == 0) {
return 0;
}
if (n == 1) {
return 1;
}
// 2. Recursive Step: Calls itself with n-1 and n-2
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int num = 7;
int i;
printf("Fibonacci Series up to the %d-th term:\n", num);
for (i = 0; i < num; i++) {
printf("%d ", fibonacci(i));
}
// Output for num=7: 0 1 1 2 3 5 8
return 0;
}
Explanation:
To calculate fibonacci(4), the function calls:
1. fibonacci(4) returns fibonacci(3) + fibonacci(2)
2. fibonacci(3) returns fibonacci(2) + fibonacci(1)
3. fibonacci(2) returns fibonacci(1) + fibonacci(0)
4. The base cases (fibonacci(0)=0 and fibonacci(1)=1) eventually return a value, which is
summed up the chain of calls until fibonacci(4) gets its final result (3).
Q3. Programming Concepts (4 Mks + 4 Mks)
a) Difference between Call by Value and Call by Reference (4 Mks)
This distinction describes how arguments (data) are passed from the calling function to the called
function.
Feature Call by Value Call by Reference
A copy of the actual argument's value The address (or pointer) of the actual
Mechanism is passed to the function's formal argument is passed to the function's formal
parameter. parameter.
Changes made to the formal parameter Changes made to the data pointed to by the
Modification DO NOT affect the original (actual) formal parameter DO affect the original
argument outside the function. (actual) argument.
Formal parameter holds the address of the
Uses separate memory for the copy
Memory actual argument, referring to the same
inside the function.
memory location.
When you want the function to work When you need the function to modify the
Use Case with data without changing the original original variables or when passing large
variables. structures/arrays efficiently.
Example (C): Swapping two numbers
Call by Value (Does not work) Call by Reference (Works)
c<br>void swap_value(int a, int b) c<br>void swap_ref(int *a, int *b) {<br>
{<br> int temp = a;<br> a = b; // Only int temp = *a;<br> *a = *b; // The data
the COPY is changed<br> b = at the address is changed<br> *b =
temp;<br>}<br><br>// main:<br>int x=10, temp;<br>}<br><br>// main:<br>int x=10,
y=20;<br>swap_value(x, y); // x and y y=20;<br>swap_ref(&x, &y); // x and y
remain 10 and 20<br> become 20 and 10<br>
b) Write a program for Factorial using Recursion (4 Mks)
The factorial of a non-negative integer $n$, denoted by $n!$, is the product of all positive integers
less than or equal to $n$.
$$n! = n \times (n-1) \times (n-2) \times \dots \times 1$$
The recursive definition is:
Base Case: $0! = 1$
Recursive Step: $n! = n \times (n-1)!$
C Program:
C
#include <stdio.h>
// Function to calculate factorial using recursion
long long factorial(int n) {
// 1. Base Case: stops the recursion
if (n == 0) {
return 1;
}
// Handle negative input (optional, but good practice)
if (n < 0) {
return -1; // Indicate error
}
// 2. Recursive Step: n * factorial(n-1)
return n * factorial(n - 1);
}
int main() {
int num = 5;
long long result = factorial(num);
if (result != -1) {
printf("Factorial of %d is: %lld\n", num, result); // Output: 120
} else {
printf("Factorial not defined for negative numbers.\n");
}
return 0;
}
Trace for factorial(3):
1. factorial(3) returns $3 \times \text{factorial}(2)$
2. factorial(2) returns $2 \times \text{factorial}(1)$
3. factorial(1) returns $1 \times \text{factorial}(0)$
4. factorial(0) returns $1$ (Base Case)
5. Result rolls back: $1 \times 1 = 1$, then $2 \times 1 = 2$, then $3 \times 2 = 6$.
Q4. Define Sorting Algorithms (Bubble, Insertion, Selection)
with their Algorithm (8 Mks)
A Sorting Algorithm is an algorithm that puts elements of a list or an array in a certain order (e.g.,
numerical or lexicographical).
1. Bubble Sort
Definition: Bubble Sort is the simplest sorting algorithm that repeatedly steps through the
list, compares adjacent elements, and swaps them if they are in the wrong order. The pass-
through repeats until the list is sorted. Larger elements "bubble up" to the end of the list with
each pass.
Algorithm:
1. Start with the first element, compare it with the next adjacent element.
2. If the current element is greater than the next, swap them.
3. Move to the next pair of adjacent elements.
4. Repeat the process until the largest element is at its correct final position (the end of
the unsorted portion).
5. Repeat steps 1-4 for the remaining unsorted portion of the list until no swaps are
needed in an entire pass.
2. Insertion Sort
Definition: Insertion Sort builds the final sorted array (or list) one item at a time. It iterates
through the input elements and consumes one input element in each iteration, placing it at its
correct position in the already sorted part of the list.
Algorithm:
1. The first element is considered sorted.
2. Start with the second element and compare it with the elements to its left (the sorted
sub-list).
3. If the element is smaller than the ones to its left, shift the larger elements one
position to the right.
4. Insert the element into its correct, sorted position.
5. Repeat steps 2-4 until all elements are in the correct place.
3. Selection Sort
Definition: Selection Sort works by repeatedly finding the minimum element from the
unsorted part and putting it at the beginning of the sorted portion.
Algorithm:
1. Start at the beginning of the list.
2. Search the entire unsorted list to find the element with the smallest value.
3. Swap the smallest found element with the element at the current position in the list.
4. Increment the current position pointer to mark the new boundary between the sorted
and unsorted portions.
5. Repeat steps 2-4 until the entire list is sorted.
Q5. Pointers and Structures in C (8 Mks)
Explain Pointer with an Example
A Pointer is a variable whose value is the memory address of another variable. Pointers are
essential for dynamic memory allocation, working with arrays/strings, and implementing data
structures.
Key concepts:
Declaration: Uses the asterisk $(*)$ operator. int *ptr; means ptr is a pointer that can
hold the address of an int variable.
Address-of Operator (&): Used to get the memory address of a variable.
Dereference Operator (*): Used to access the value stored at the memory address pointed
to by the pointer.
Example:
C
#include <stdio.h>
int main() {
int value = 100; // An integer variable
int *ptr; // A pointer to an integer
ptr = &value; // 'ptr' now holds the memory address of 'value'
printf("Value of 'value': %d\n", value); // Output: 100
printf("Address of 'value' (stored in ptr): %p\n", ptr); // Output: memory
address (e.g., 0x7ffd...)
printf("Value at the address (using *ptr): %d\n", *ptr); // Output: 100
(Dereferencing)
*ptr = 250; // Change the value at the address pointed to by ptr
printf("New value of 'value': %d\n", value); // Output: 250 (Original
'value' is changed)
return 0;
}
Write a Program to Explain the Concept of Structure in C
A Structure (struct) allows you to combine data items of different data types under one name.
This example demonstrates defining a structure for a Book and accessing its members.
C Program:
C
#include <stdio.h>
#include <string.h>
// 1. Structure Definition/Declaration
struct Book {
char title[100];
char author[100];
int pages;
float price;
};
int main() {
// 2. Structure Variable Definition (creating instances of the Book
structure)
struct Book book1;
struct Book book2;
// --- Assigning values to members of book1 ---
strcpy([Link], "The C Programming Language");
strcpy([Link], "Dennis Ritchie");
[Link] = 272;
[Link] = 599.50;
// --- Initializing book2 during definition ---
struct Book book2 = {"Data Structures in C", "A. S. Tenenbaum", 680,
750.00};
// 3. Accessing and printing structure members using the dot (.) operator
printf("--- Book 1 Details ---\n");
printf("Title: %s\n", [Link]);
printf("Author: %s\n", [Link]);
printf("Pages: %d\n", [Link]);
printf("Price: %.2f\n", [Link]);
printf("\n--- Book 2 Details ---\n");
printf("Title: %s\n", [Link]);
printf("Author: %s\n", [Link]);
printf("Pages: %d\n", [Link]);
printf("Price: %.2f\n", [Link]);
return 0;
}