Lab Maual FCS
Lab Maual FCS
Introduction to C Programming
Aim:
The aim of this introduction is to understand the basics of C programming, including the general
structure of a C program, how to write and execute a simple "Hello, World!" program, and how
to run it using a text editor.
Theory
C programming is essential for building efficient and robust applications, offering a balance
between high-level programming capabilities and low-level hardware interaction.
Open a text editor (such as Notepad, Sublime Text, or any IDE like Code::Blocks or Visual
Studio).
Conclusion
C programming is foundational for system-level programming and applications. By following the basic
structure, you can write, compile, and execute C programs.
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Experiment 02
Aim:
The aim of this program is to demonstrate the usage of different types of operators in C,
including arithmetic operators, relational operators, logical operators, and assignment
operators.
Theory:
1. Operators in C:
Operators are symbols that perform operations on variables and values. In C, operators are used
to manipulate data and variables. These operators can be classified into several types, each
serving a specific function:
Types of Operators in C:
1. Arithmetic Operators:
These operators are used to perform mathematical calculations such as addition, subtraction,
multiplication, division, and modulus.
Examples:
% (Modulus): Returns the remainder when the first operand is divided by the second.
Example:
These operators compare two values and return a boolean value (1 for true, 0 for false). They are
used to evaluate conditions in decision-making structures.
Examples:
> (Greater than): Checks if the left operand is greater than the right operand.
< (Less than): Checks if the left operand is less than the right operand.
Example
3. Logical Operators:
Logical operators are used to perform logical operations between two or more conditions. They
are commonly used in decision-making and control flow statements.
Examples:
! (Logical NOT): Reverses the boolean value of the condition (i.e., converts true to false and vice
versa).
Example:
result = (x && y); // True if both 'x' and 'y' are true
result = (x || y); // True if either 'x' or 'y' is true
result = !x; // Reverses the value of 'x'
Assignment Operators:
Assignment operators are used to assign values to variables. The most basic assignment operator
is the = sign.
Examples:
= (Assignment): Assigns the value of the right operand to the left operand.
+= (Addition Assignment): Adds the right operand to the left operand and assigns the result to
the left operand.
-= (Subtraction Assignment): Subtracts the right operand from the left operand and assigns the
result to the left operand.
*= (Multiplication Assignment): Multiplies the left operand by the right operand and assigns the
result to the left operand.
/= (Division Assignment): Divides the left operand by the right operand and assigns the result to
the left operand.
Example:
3. Conclusion:
This program helps in understanding the fundamental types of operators in C. Operators are
essential for performing calculations, making comparisons, controlling program flow, and
assigning values to variables.
#include <stdio.h>
int main() {
// Declare variables
int a = 10, b = 5;
int result;
// Arithmetic Operators
result = a + b;
printf("Addition: %d + %d = %d\n", a, b, result);
result = a - b;
printf("Subtraction: %d - %d = %d\n", a, b, result);
result = a * b;
printf("Multiplication: %d * %d = %d\n", a, b, result);
result = a / b;
printf("Division: %d / %d = %d\n", a, b, result);
result = a % b;
printf("Modulus: %d %% %d = %d\n", a, b, result);
// Relational Operators
printf("\nRelational Operators:\n");
printf("%d > %d = %d\n", a, b, a > b);
printf("%d < %d = %d\n", a, b, a < b);
printf("%d == %d = %d\n", a, b, a == b);
printf("%d != %d = %d\n", a, b, a != b);
// Logical Operators
int x = 1, y = 0;
printf("\nLogical Operators:\n");
printf("x && y = %d\n", x && y);
printf("x || y = %d\n", x || y);
printf("!x = %d\n", !x);
// Assignment Operators
printf("\nAssignment Operators:\n");
int c = 5;
c += 3; // c = c + 3
printf("c += 3: %d\n", c);
c -= 2; // c = c - 2
printf("c -= 2: %d\n", c);
return 0;
}
Output:
Addition: 10 + 5 = 15
Subtraction: 10 - 5 = 5
Multiplication: 10 * 5 = 50
Division: 10 / 5 = 2
Modulus: 10 % 5 = 0
Relational Operators:
10 > 5 = 1
10 < 5 = 0
10 == 5 = 0
10 != 5 = 1
Logical Operators:
x && y = 0
x || y = 1
!x = 0
Assignment Operators:
c += 3: 8
c -= 2: 6
Experiment 03
Program to illustrate switch case statement
AIM:
To demonstrate the use of the switch statement in C for efficient multi-way branching based on a
variable's value, simplifying decision-making in programs.
Theory:
The switch statement in C is a control structure used to handle multiple possible execution paths
based on the value of an expression or variable. It simplifies the process of decision-making
when there are many potential conditions to evaluate.
The switch statement allows you to compare a single variable or expression against a
list of predefined constants.
It is a more readable and efficient alternative to writing multiple if-else statements
when handling numerous conditions.
Syntax:
switch (expression) {
case constant1:
// Code to execute if expression equals constant1
break;
case constant2:
// Code to execute if expression equals constant2
break;
default:
// Code to execute if no cases match (optional)
break;
}
Expression:
The expression in a switch statement is evaluated once and compared against the values
specified in the case labels.
It must result in an integer or a value that can be converted to an integer (e.g., char).
Case Labels:
Break Statement:
The break statement prevents the code from "falling through" to subsequent cases.
If a break is omitted, execution will continue into the next case (fall-through behavior).
Default Case:
The default label is optional and provides a catch-all for cases where no other case
matches the expression.
If omitted and no cases match, the switch block will be skipped.
Conclusion:
The switch statement is a powerful tool for managing multi-way branching in a program. It is
particularly useful when there are multiple conditions based on a single variable or expression.
By ensuring proper use of break and default cases, programmers can create efficient and easy-to-
read decision-making structures
int main() {
char operator;
int num1, num2, result;
// Input
printf("Enter operator (+, -, *, /): ");
scanf(" %c", &operator); // Space before %c to consume any leftover newline
character
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
// Output result
printf("Result: %d %c %d = %d\n", num1, operator, num2, result);
return 0;
}
Experiment No-4
Aim:
To write and execute programs that illustrates the use of conditional statements for decision-
making in C programming.
Theory:
Conditional statements are used in programming to make decisions based on certain conditions.
These conditions evaluate to either true or false, and the program executes a specific block of code
depending on the result. Conditional statements add logical flow and allow programs to handle
different situations effectively.
1. if Statement:
Syntax:
if (condition)
if-else Statement:
Provides two possible paths for execution: one if the condition is true and the other if it is false.
Syntax
if (condition) {
} else {
A nested if-else statement is an if or else statement that is contained inside another if or else
statement. This allows for more complex decision-making by checking additional conditions
within each branch of the logic.
Syntax
if (condition1) {
if (condition2) {
} else {
} else {
Conclusion
Conditional statements like if, if-else, and nested if-else allow a program to make decisions based on
conditions. They enable the execution of different code blocks depending on whether a condition is true
or false. Nested if-else provides more complex decision-making by allowing multiple layers of conditions
to be checked.
int main() {
int num;
// Input a number
printf("Enter a number: ");
scanf("%d", &num);
return 0;
}
b) Finding the largest among three number using nested if- else
#include <stdio.h>
int main() {
int a, b, c, largest;
return 0;
}
Experiment No 5
Aim: The aim is to understand and implement entry-controlled and exit-controlled loops,
Theory:
Looping is a fundamental concept in programming that allows repetitive execution of a block of code. It
helps automate repetitive tasks and can handle situations where a set of instructions needs to be executed
multiple times. In C, there are different types of loops: entry-controlled loops and exit-controlled loops,
which determine when the loop condition is checked in relation to the execution of the loop body.
The for loop is used when the number of iterations is known in advance. It is ideal for tasks that require
repetitive actions a fixed number of times. The loop is controlled by an initialization statement, a
condition, and an increment or decrement that updates the loop control variable.
Syntax:
multi-dimensional data or performing complex tasks that require repetition at multiple levels. The outer
loop controls the main iteration, while the inner loop handles secondary repetitions for each outer
iteration.
Syntax:
The while loop is another entry-controlled loop where the condition is evaluated before each
iteration.
while (condition) {
// Code to be executed
If the condition is false initially, the loop body will not be executed.
do-while Loop
The do-while loop differs from the while loop in that it guarantees at least one execution of the loop
body, regardless of the condition. The condition is checked after the loop body executes, ensuring that
the code inside the loop is always executed at least once. This is useful for tasks like menu-driven
programs or input validation where the task should run at least once.
Syntax
do {
// Code to be executed
} while (condition);
Condition: The loop continues as long as this condition is true. The condition is evaluated after each
Conclusion:
Entry-controlled loops check the condition before executing the loop body, while exit-controlled
loops ensure the body is executed at least once. Both loops are essential for efficient repetitive
tasks in programming.
#include <stdio.h>
int main() {
int rows;
return 0;
}
int main() {
int i = 1;
return 0;
}
c) C program that calculates the sum of positive numbers using a do-while loop:
#include <stdio.h>
int main() {
int number, sum = 0;
return 0;
}
Experiment No 6
In C programming, every program starts with a special function called main(), and additional
functions can be user-defined or predefined in libraries like printf() or scanf().
Types of Functions:
Description: The function does not take any input (no arguments) and does not return any value. It
performs a specific task independently.
Syntax:
void functionName() {
Description: The function takes input arguments but does not return a value. It uses the
arguments to perform a task.
Syntax:
void functionName(dataType arg1, dataType arg2, ...) {
Description: The function takes input arguments and returns a value to the calling
function.
Syntax:
returnType functionName() {
Conclusion:
Understanding different types of functions helps in writing modular, reusable, and efficient code.
Functions with arguments provide flexibility for dynamic operations, while return values
facilitate result-driven processing.
Aim:
Theory:
Recursion is a programming technique where a function calls itself to solve a smaller instance of
the same problem. A recursive function must have two parts:
1. Base Case: The condition at which the recursion stops. It prevents the function from
making infinite recursive calls.
2. Recursive Case: The part of the function where it calls itself to solve a smaller problem
Syntax:
returnType functionName(parameters) {
if (baseCondition) {
return value;
} else {
return functionName(smallerParameters);
Conclusion:
Recursion is a powerful tool that simplifies problems by breaking them into smaller
subproblems. It is most useful for tasks with repetitive or hierarchical structures, such as
factorials, Fibonacci sequences, and tree traversals.
Recursive function to calculate factorial of a number
#include <stdio.h>
// Recursive function to calculate factorial
int factorial(int n) {
if (n == 0 || n == 1) { // Base case
return 1;
}
return n * factorial(n - 1); // Recursive call
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
printf("Factorial of %d is %d\n", num, factorial(num));
}
return 0;
}
Experiment No 8
Aim;To understand and implement arrays in C programming, focusing on one-dimensional arrays, their
declaration, initialization, and operations.
Theory
dataType arrayName[size];
arrayName[index];
Conclusion:
Arrays are essential for storing and manipulating large collections of similar data efficiently.
One-dimensional arrays are simple yet powerful tools for linear data organization. By
understanding their declaration, initialization, and access methods, programmers can handle
repetitive data storage and processing tasks effectively.
}
Experiment No 9
Aim:
Theory:
A pointer is a variable that stores the memory address of another variable. Pointers are powerful
tools in C programming, allowing direct access and manipulation of memory, efficient array
handling, and dynamic memory allocation.
Key Concepts:
1. Pointer Declaration:
Declares a pointer that can store the address of a variable.
Syntax:
dataType *pointerName;
Example:
int *ptr; // Pointer to an integer
Pointer Initialization:
Assigns the address of a variable to a pointer.
Example:
int x = 10;
Dereferencing Pointers:
Access the value at the address stored in a pointer using the dereference operator *:
Conclusion:
Pointers are essential for efficient memory management, allowing programs to directly access
and manipulate memory. They enhance functionality by enabling operations like dynamic
memory allocation, array traversal, and passing values by reference.
Swapping Two Integers Using Pointers
#include <stdio.h>
// Function to swap two integers using pointers
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int num1, num2;
// Taking input
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
// Printing values before swapping
printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);
// Swapping the values
swap(&num1, &num2);
// Printing values after swapping
printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);
return 0;
}
Experiment No 10
Program to illustrate structures
Aim:
Theory:
A structure in C is a user-defined data type that groups variables of different types under a
single name. It is particularly useful for handling complex data, such as records in a database or
attributes of an entity like a student or employee.
1. Declaration of Structures:
Structures are defined using the struct keyword:
struct structureName {
dataType member1;
dataType member2;
...
};
variableName.memberName;
4. Initialization of Structures:
Structure members can be initialized during declaration:
Conclusion:
Structures in C enable the organization of complex data by grouping related variables of different
types into a single unit. They are essential for managing real-world data, making programs
modular and easier to maintain.
"C Program to Store and Display a Single Student's Information Using Structures"
#include <stdio.h>
#include <string.h>
struct Student {
char name[50];
int rollNo;
float marks;
};
int main() {
struct Student s1; // Declare structure variable as s1
printf("Enter student's name: ");
scanf("%s", s1.name); // Directly read the name into the structure
printf("Enter student's roll number: ");
scanf("%d", &s1.rollNo);
printf("Enter student's marks: ");
scanf("%f", &s1.marks);
printf("\n Student Information:\n");
printf("Name: %s\n", s1.name);
printf("Roll Number: %d\n", s1.rollNo);
printf("Marks: %.2f\n", s1.marks);
return 0;
}
C Program to Store and Display Multiple Students' Information Using Structures and Arrays"
#include <stdio.h>
#include <string.h>
// Define a structure for student information
struct Student {
char name[50];
int rollNo;
float marks;
};
int main() {
int n;
// Input the number of students
printf("Enter the number of students: ");
scanf("%d", &n);
struct Student s[n]; // Array of structures
// Input details for each student
for (int i = 0; i < n; i++) {
printf("\n Enter details for student %d:\n", i + 1);
printf("Name: ");
scanf("%s", s[i].name); // Directly store the name
printf("Roll Number: ");
scanf("%d", &s[i].rollNo);
printf("Marks: ");
scanf("%f", &s[i].marks);
}
// Display details of all students
printf("\nStudent Information:\n");
for (int i = 0; i < n; i++) {
printf("\n Student %d:\n", i + 1);
printf("Name: %s\n", s[i].name);
printf("Roll Number: %d\n", s[i].rollNo);
printf("Marks: %.2f\n", s[i].marks);
}
return 0;
}