Assignment 3
Q1. What is function? Explain program of parameter passing in function?
Explanation:
A function in C is a block of code that performs a specific task. Functions help in code
reusability, readability, and modularity. Instead of writing the same code repeatedly, we can
define a function once and call it whenever needed.
Types of Function Parameter Passing
1. Call by Value: The function gets a copy of the argument, so changes inside the function do
not affect the original variable.
2. Call by Reference: The function gets the actual address of the argument, so changes inside
the function affect the original variable.
Example Program for Parameter Passing -
#include <stdio.h>
// Function using Call by Value
void callByValue(int a) {
a = a * 2; // Changes will not affect the original variable
printf("Inside callByValue: a = %d\n", a);
}
// Function using Call by Reference
void callByReference(int* b) {
*b = *b * 2; // Changes will affect the original variable
printf("Inside callByReference: b = %d\n", *b);
}
int main() {
int x = 10, y = 10;
// Call by Value
printf("Before callByValue: x = %d\n", x);
callByValue(x);
printf("After callByValue: x = %d\n\n", x); // x remains unchanged
// Call by Reference
printf("Before callByReference: y = %d\n", y);
callByReference(&y);
printf("After callByReference: y = %d\n", y); // y gets updated
return 0;
}
Output -
Before callByValue: x = 10
Inside callByValue: a = 20
After callByValue: x = 10
Before callByReference: y = 10
Inside callByReference: b = 20
After callByReference: y = 20
Q2.What is Recursion? Write program of it.
Explanation:
Recursion in C is a technique where a function calls itself to solve a problem in a smaller or
simpler form. It is useful for problems that can be broken down into smaller, repetitive
subproblems, such as factorial calculation, Fibonacci series, and tree traversal.
Example Program: Factorial using Recursion-
#include <stdio.h>
// Recursive function to calculate factorial
int factorial(int n) {
if (n == 0) // Base condition
return 1;
return n * factorial(n - 1); // Recursive call
}
int main() {
int num = 5;
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}
Output -
Factorial of 5 is 120
Q3. Write a program of Fibonacci series?
Explanation:
The Fibonacci series is a sequence where each number is the sum of the two preceding ones,
starting from 0 and 1.
Example -
#include <stdio.h>
// Recursive function to find Fibonacci number
int fibonacci(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int terms = 10; // Number of terms
printf("Fibonacci Series: ");
for (int i = 0; i < terms; i++) {
printf("%d ", fibonacci(i));
}
return 0;
}
Output -
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34
Q4. What is structure? Explain Structure and Array of Structures?
Explanation:
A structure in C is a user-defined data type that allows grouping of different data types under a
single name. It is useful for storing related information together.
Syntax of Structure:
struct Student {
char name[50];
int age;
float marks;
};
Example: Structure in C
#include <stdio.h>
// Define structure
struct Student {
char name[50];
int age;
float marks;
};
int main() {
struct Student s1 = {"John", 20, 85.5};
// Access structure members
printf("Name: %s\n", s1.name);
printf("Age: %d\n", s1.age);
printf("Marks: %.2f\n", s1.marks);
return 0;
}
Output -
Name: John
Age: 20
Marks: 85.50
Array of Structures
An array of structures stores multiple records of the same structure type.
Example: Array of Structures
Input -
#include <stdio.h>
// Define structure
struct Student {
char name[50];
int age;
float marks;
};
int main() {
struct Student students[3] = {
{"Krish", 18, 90.0},
{"Deepak", 19, 85.5},
{"Puneet", 20, 88.0}
};
// Display student details
for (int i = 0; i < 3; i++) {
printf("Student %d:\n", i + 1);
printf("Name: %s\n", students[i].name);
printf("Age: %d\n", students[i].age);
printf("Marks: %.2f\n\n", students[i].marks);
}
return 0;
}
Output -
Student 1:
Name: Krish
Age: 18
Marks: 90.00
Student 2:
Name: Deepak
Age: 19
Marks: 85.50
Student 3:
Name: Puneet
Age: 20
Marks: 88.00