Download as pdf or txt
Download as pdf or txt
You are on page 1of 168

B V RAJU INSTITUTE OF TECHNOLOGY

COMPUTATIONAL THINKING AND PROGRAMMING

NOTES
Academic Year 2023-24
I Year I Semester CSE(AI & ML)
(2023 Batch)

Prepared By:
Dr. G Uday Kiran
Ms. Srilakshmi V
I Year I Sem Computational Thinking and Programming

NOTES
Syllabus
Unit I: (10 Lectures)
Introduction to Computational Thinking: Definition, Objectives, How is Computational
Thinking Used? Logical and Algorithmic Thinking.
Problem Solving Elements: Algorithms, Definition and Characteristics; Flowchart,
Flowchart Symbols, Pseudo Code Solution to Problems, Basic Structures of C Language,
Creating and Running Programs, Variables and Data Types, Input and Output Statements,
Types of Operators, Precedence of Operators, Type Conversion.

Unit II: (10 Lectures)


Computational Problems: If, If-else, Nested If, Switch-case, Ternary Operator, Goto,
Iteration with For, While, Do-while, Break, Continue, Standard Problems [GCD, Finding
Roots, Generating Factors of a Number, Checking for Leap Year], Number Series Problems
(Fibonacci Series, Natural Numbers, Even Numbers, Prime Numbers, Multiplication Table,
Palindrome Numbers).

Unit III: (10 Lectures)


Arrays: One- and Two-Dimensional Arrays, Creating, Accessing and Manipulating Elements
of Arrays, Row/Column Major Formats, Standard Problems (Addition and Multiplication of
Two Matrixes, Mean, Variance, SD, Maximum and Minimum Elements using Arrays).
Functions: Declaring a Function, Signature of a Function, Passing Parameters to Functions,
Passing Arrays to Functions, Recursion, Standard Problems [Factorial of a Given Number,
Sum of Natural Numbers using Recursion), External, Auto, Local, Static, Register Variables.

Unit IV: (10 Lectures)


Pointers: Pointer, Pointer to Pointer, Pointers and Functions (Call by Reference) Arguments,
Pointers and Arrays, Address Arithmetic, Character Pointers and Functions, Dynamic
Memory Allocation Methods.
Strings: Declaring, Initializing, String Input and Output Functions, String Handling
Functions.

Unit V: (10 Lectures)


Structures: Defining Structures, Initializing Structures, Structures and Functions, Array of
Structures.
File Management: Creating, Reading, Writing Text and Binary Files, Random Access using
fseek, ftell and Rewind Functions.

Textbook:
1. Computational Thinking - A Beginner's Guide to Problem-Solving and Programming, Karl
Beecher, BCS Learning & Development Limited, ISBN: 9781780173641, August 2017.
2. Schaum's Outline of Programming with C, Byron Gottfried, McGraw-Hill.
3. Programming in ANSI C, E. Balaguruswamy, Tata McGraw-Hill.

References:
1. The C Programming Language, Brian W. Kernighan, Dennis M. Ritchie, Prentice Hall of India
2. Let us C, Yashavant Kanetkar, BPB Publications, New Delhi.
3. Absolute Beginners' Guide to C, Greg Perry, SAMS Publishing

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 2/168


I Year I Sem Computational Thinking and Programming

Table of Contents

Syllabus ................................................................................................................... 2

Table of Contents ..................................................................................................... 3

Unit I ....................................................................................................................... 6

Topics to be Discussed: ............................................................................................... 6

Introduction to Computational Thinking ..................................................................... 6

Definition ................................................................................................................... 6

Objectives ................................................................................................................... 7

How is Computational Thinking Used? ........................................................................ 8

Logical and Algorithmic Thinking ................................................................................ 9

Problem Solving Elements ......................................................................................... 10

Algorithms - Definition and Characteristics ............................................................... 10

Flowchart ................................................................................................................. 13

Pseudo Code Solution to Problems ............................................................................ 17

Basic Structures of C Language ................................................................................ 18

Variables and Data Types ......................................................................................... 21

Input and Output Statements ................................................................................... 25

Types of Operators .................................................................................................... 27

Precedence of Operators............................................................................................ 33

Type Conversion ....................................................................................................... 33

Unit II .................................................................................................................... 35

Topics to be Discussed: ............................................................................................. 35

Selection Statements / Control Statements ............................................................... 35

If Statements: ........................................................................................................... 36

Switch-Case ............................................................................................................. 43

Ternary Operator / Conditional Operator .................................................................. 49

Goto ......................................................................................................................... 52

Iteration / Looping Constructs .................................................................................. 54

While ........................................................................................................................ 55

Do- While ................................................................................................................. 57

For ........................................................................................................................... 59

Break ....................................................................................................................... 62

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 3/168


I Year I Sem Computational Thinking and Programming

Continue .................................................................................................................. 62

Standard Problems ................................................................................................... 64

Number Series Problems ........................................................................................... 69

Unit III ................................................................................................................... 81

Topics to be Discussed: ............................................................................................. 81

Arrays ...................................................................................................................... 81

One Dimensional Arrays – Creation of Arrays ............................................................ 87

Accessing and Manipulating Elements of Arrays ........................................................ 88

Two-Dimensional Array ............................................................................................. 90

Row/Column Major Formats ..................................................................................... 91

Standard Problems ................................................................................................... 92

Mean ........................................................................................................................ 97

Variance ................................................................................................................... 97

Standard Deviation ................................................................................................... 98

Maximum and Minimum Elements using Arrays ....................................................... 99

Mean, Variance, Standard Deviation, Minimum and Maximum Element of An Array .. 99

Functions ............................................................................................................... 101

Declaring a Function .............................................................................................. 102

Signature of a Function .......................................................................................... 103

Passing Parameters to Functions ............................................................................ 104

Passing Arrays to Functions .................................................................................... 105

Recursion ............................................................................................................... 107

Standard Problems ................................................................................................. 108

External ................................................................................................................. 113

Auto ....................................................................................................................... 114

Local ...................................................................................................................... 115

Static ..................................................................................................................... 115

Register Variables ................................................................................................... 116

Unit IV ................................................................................................................. 118

Topics to be Discussed: ........................................................................................... 118

Pointers .................................................................................................................. 118

Pointer to Pointer .................................................................................................... 125

Pointer and Functions (Call by Reference) Arguments .............................................. 126

Pointers and Arrays ................................................................................................ 128

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 4/168


I Year I Sem Computational Thinking and Programming

Address Arithmetic ................................................................................................. 129

Characters and Pointers ......................................................................................... 131

Dynamic Memory Allocation Methods ...................................................................... 133

Strings ................................................................................................................... 136

Declaring ................................................................................................................ 136

Initializing .............................................................................................................. 136

String Input and Output Functions ......................................................................... 136

String Handling Functions ...................................................................................... 137

Unit V .................................................................................................................. 142

Topics to be Discussed: ........................................................................................... 142

Structures .............................................................................................................. 142

Why Shall We Study Structures / Why Structures Shall be Studied? ....................... 144

Defining Structures ................................................................................................ 144

Initializing Structures ............................................................................................. 147

Structures and Functions ....................................................................................... 149

Array of Structures ................................................................................................. 151

Case Study: ............................................................................................................ 153

File – Definition ...................................................................................................... 158

Introduction to Files ............................................................................................... 159

File Operations in C Programming ........................................................................... 159

File Management .................................................................................................... 159

Creating ................................................................................................................. 159

Reading .................................................................................................................. 161

Writing Text and Binary Files .................................................................................. 164

Random Access Using FSeek, FTell and Rewind Functions ...................................... 166

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 5/168


I Year I Sem Computational Thinking and Programming

Unit I
Topics to be Discussed:
Introduction to Computational Thinking: Definition, Objectives, How is Computational
Thinking Used? Logical and Algorithmic Thinking.
Problem Solving Elements: Algorithms, Definition and Characteristics; Flowchart,
Flowchart Symbols, Pseudo Code Solution to Problems, Basic Structures of C Language,
Creating and Running Programs, Variables and Data Types, Input and Output Statements,
Types of Operators, Precedence of Operators, Type Conversion.

Introduction to Computational Thinking


Computational Thinking is a fundamental problem-solving approach that transcends the
boundaries of computer science, applicable to various disciplines and real-life scenarios. It
equips individuals with a structured method to dissect complex problems, design solutions,
and efficiently communicate ideas. Unlike traditional coding skills, computational thinking
doesn't require prior knowledge of programming.
Imagine planning a road trip with friends. You need to find the optimal route considering
factors like distance, traffic, and stops. This situation showcases computational thinking:
breaking down a problem (route planning) into smaller tasks (considering factors), identifying
patterns (traffic patterns), and devising a systematic solution (optimal route). This case study
illustrates how computational thinking helps tackle problems methodically, applicable
beyond coding.

Definition
Computational thinking involves breaking down complex problems, recognizing patterns,
formulating precise instructions, and devising creative solutions that can be executed by
computers or humans. In the road trip example, it's about analyzing factors, determining a
sequence of actions, and reaching a solution. The correlation lies in dissecting a non-
computer problem as one would with a coding challenge.

“Computational Thinking is a Mental Process for Designing Solutions to Problems in a Way that
Leverages the Strengths of Computers.”
-Jeanette Wing

“Computational Thinking Empowers People to be Both Critical Thinkers About the World and
Designers of their Own Digital Tools.”
-Seymour Papert
“It's a Set of Skills and Approaches that Help Individuals Solve Complex Problems in all Areas
of Life.”
-Seymour Papert
Why Study Computational Thinking?
• Problem-Solving: It enhances analytical skills, enabling systematic and efficient
problem-solving in diverse domains.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 6/168


I Year I Sem Computational Thinking and Programming

• Transdisciplinary Application: Regardless of the field, computational thinking aids


in tackling complex issues and making informed decisions.
• Logical Reasoning: It fosters logical thinking, helping break down intricate problems
into manageable parts.
• Creativity: By identifying patterns and designing solutions, it encourages creative
thinking.
• Future-Proofing: In a tech-driven world, computational thinking is a vital skill for
adapting to changing technologies.
• Collaboration: It promotes structured communication and teamwork, crucial in
modern workplaces.

Relevance of Computational Thinking to Undergraduate Students


• Foundational Skill: Computational thinking lays the foundation for understanding
technology's role in their future careers, regardless of major.
• Cognitive Development: It sharpens cognitive abilities, promoting structured
thinking and problem deconstruction.
• Holistic Approach: Encourages looking at problems holistically, fostering well-
rounded perspectives.
• Digital Citizenship: Equips students to make informed decisions in the digital age
and understand ethical implications.
• Innovation: Computational thinking is pivotal for driving innovation, allowing
students to create novel solutions.
• Adaptability: Prepares students for dynamic job markets, enhancing their
adaptability and versatility.

Objectives
• Critical Thinking: Develop the ability to dissect problems and evaluate multiple
solution paths.
• Pattern Recognition: Identify recurring patterns within problems and apply relevant
strategies.
• Algorithmic Thinking: Formulate step-by-step approaches to solving complex
problems.
• Abstraction: Grasp the concept of simplification by focusing on essential details while
ignoring irrelevant ones.
• Creativity: Apply computational thinking to devise innovative solutions and
approaches.
• Interdisciplinary Application: Demonstrate how computational thinking is relevant
across disciplines.
• Effective Communication: Clearly articulate problem-solving processes and
solutions.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 7/168


I Year I Sem Computational Thinking and Programming

How is Computational Thinking Used?


CT can be applied by anyone who is attempting to solve a problem and have a computer play
a role in the solution. For example, what could algorithmic thinking mean in different
situations? To a computer scientist, it means the study of algorithms and their application to
different problems. To a mathematician, it might mean carrying out long division factoring or
doing carries in addition or subtraction. A scientist might think of it as the process of doing
an experimental procedure.
Similarly, abstraction has applications beyond the computer scientist’s view of it. When a
linguist uses simile and metaphor, or writes a story with branches, they’re using abstraction,
as are social scientists who summarize facts and use them to draw conclusions. When a
scientist builds a model or a mathematician uses algebra, they too have introduced
abstraction into their work.
Example 1: Pipelining a Graduating Ceremony
Dean Randy Bryant was pondering how to make the diploma ceremony at commencement go
faster. By careful placement of where individuals stood, he designed an efficient pipeline so
that upon the reading of each graduate’s name and honors by Assistant Dean Mark Stehlik,
each person could receive his or her diploma, then get a handshake or hug from Mark, and
then get his or her picture taken. This pipeline allowed a steady stream of students to march
across the stage (though a pipeline stall occurred whenever the graduate’s cap would topple
while getting hug from Mark).

Example 2: Predicting a Climate Change


Predicting global climate change is only possible because of advanced computer models.
According to the UK Met Office, ‘The only way to predict the day-to-day weather and changes
to the climate over longer timescales is to use computer models.

Example 3: Sorting Music Charts


I showed up to a big band gig, and the band leader passed out books with maybe 200
unordered charts and a set list with about 40 titles we were supposed to get out and place in
order, ready to play. Everyone else started searching through the stack, pulling out charts
one-at-a-time. I decided to sort the 200 charts alphabetically O(N log N)6 and then pull the
charts O(M log(N)). I was still sorting when other band members were halfway through their
charts, and I started to get some funny looks, but in the end, I finished first. That’s
computational thinking.

Example 4: Assisting Police, Lawyers and Judges


Computational Thinking has a long tradition in influencing the law, especially in the dream
of providing a set of logical rules that can automate the process of reaching a verdict,
[underpinning] its desire to minimise human discretion and maximise predictability of
outcome… legal reasoning systems have been making inroads where they merely try to assist
those making legal decisions. For instance, researchers at the Joseph Bell Centre have built
a system that constructs a space of hypotheses to explain the evidence in a crime scene. This
has been used to remind detectives of hypotheses they might otherwise have missed.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 8/168


I Year I Sem Computational Thinking and Programming

Logical and Algorithmic Thinking


Logical thinking and algorithmic thinking are essential cognitive skills, particularly in the
fields of computer science, mathematics, and problem-solving. They involve systematic and
structured approaches to address complex problems and arrive at solutions.
Logical Thinking:
Logical thinking refers to the ability to reason and make decisions based on a systematic and
structured approach. It involves drawing conclusions from available information, identifying
patterns, and making sound judgments. Logical thinking is characterized by the following
key aspects:
• Critical Analysis: Logical thinking involves critically examining information and data.
It requires evaluating the reliability and relevance of information before drawing
conclusions.
• Pattern Recognition: Identifying patterns in data or information is a crucial aspect
of logical thinking. Recognizing recurring elements or trends can help in problem-
solving and decision-making.
• Deductive Reasoning: Deductive reasoning involves drawing specific conclusions
from general premises or observations. It starts with a broad principle and narrows
down to specific conclusions.
• Inductive Reasoning: Inductive reasoning works in the opposite direction, starting
with specific observations and drawing general conclusions or hypotheses. It is often
used in scientific research and data analysis.
• Problem-Solving: Logical thinking is a key component of problem-solving. It helps
individuals break down complex problems into smaller, manageable parts and
formulate logical steps to arrive at solutions.
• Decision-Making: In decision-making, logical thinking helps in evaluating various
options, considering their pros and cons, and selecting the best course of action based
on reasoning and evidence.
• Logical Arguments: Constructing and evaluating logical arguments is an important
skill in logical thinking. It involves making a claim, providing evidence, and drawing
a valid conclusion.

Algorithmic Thinking:
Algorithmic thinking is a subset of logical thinking that focuses on the systematic design and
analysis of algorithms. An algorithm is a step-by-step procedure or set of instructions for
solving a specific problem or performing a task. Algorithmic thinking involves the following
aspects:
• Algorithm Design: It is the process of creating a well-defined sequence of steps to
solve a particular problem. This requires breaking down the problem into smaller
subproblems and designing a logical flow to solve each subproblem.
• Efficiency: Algorithmic thinking considers the efficiency of algorithms. Efficient
algorithms perform tasks with minimal time and resources. Analyzing time complexity
and space complexity are common practices in algorithmic thinking.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 9/168


I Year I Sem Computational Thinking and Programming

• Pseudocode: Pseudocode is a way of representing an algorithm using a combination


of natural language and simplified programming constructs. It is a tool often used in
algorithmic thinking to outline the logic of an algorithm before implementation.
• Flowcharts: Flowcharts are visual representations of algorithms, showing the flow of
control and decision points. They are useful for planning and communicating the logic
of an algorithm.
• Problem Abstraction: Algorithmic thinking involves abstracting real-world problems
into algorithmic terms. This abstraction simplifies complex problems into a series of
algorithmic steps.
• Recursion: Recursive thinking is an aspect of algorithmic thinking where a problem
is solved by breaking it down into smaller instances of the same problem. Recursion
is commonly used in algorithm design.
• Algorithm Analysis: Evaluating the correctness and efficiency of algorithms is a
critical part of algorithmic thinking. This includes considering worst-case and best-
case scenarios, as well as average-case performance.

Logical thinking is a broader cognitive skill that encompasses reasoning, analysis, and
decision-making, while algorithmic thinking is a specific subset of logical thinking focused
on the systematic design and analysis of algorithms. Both are fundamental in problem-
solving, mathematics, computer science, and various fields where structured and systematic
approaches are required to arrive at solutions.
Problem Solving Elements

Algorithms - Definition and Characteristics


An algorithm is a step-by-step procedure or formula for solving a problem. It is a finite, well-
defined, and unambiguous sequence of instructions designed to perform a specific task or
solve a particular problem. Algorithms are the foundation of all computer programs and are
critical for executing complex computational tasks in an optimized manner.
A set of rules or well-defined instructions that defines a series of operations to be carried out
to decode a specific issue is known as an Algorithm.
In simple words, we can say that an algorithm is a procedure that accepts input as a collection
of values and, in return, provides output by solving the problem.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 10/168


I Year I Sem Computational Thinking and Programming

We use an algorithm to generate a solution to any given problem in the form of steps. When
we use a computer for solving any given problem (in well-defined multiple steps), we need to
communicate these steps to the solution properly to the computer.
When we execute any algorithm on a computer, we need to combine various operations like
subtraction and addition for performing various complex operations of mathematics. We can
express algorithms using flowcharts, natural language, and many more.

Characteristics:
• Input: An algorithm should have zero or more inputs.
• Output: An algorithm should produce one or more outputs.
• Definiteness: Each instruction must be clear and unambiguous.
• Finiteness: The algorithm should have a finite number of steps.
• Effectiveness: The operations should be basic so that they can be performed exactly
and in a finite amount of time.

Input:
An algorithm should have zero or more inputs provided externally.
Example: In the binary search algorithm, the inputs are a sorted array and a target value to
find.
Explanation: Inputs are essential for an algorithm to operate. In the case of binary search,
without an array to search through or a target value to find, the algorithm cannot perform its
intended task.

Output:
An algorithm should produce at least one output, which is the solution to the problem.
Example: In the sorting algorithm Quick Sort, the output is an array sorted in ascending or
descending order.
Explanation: The purpose of running an algorithm is to arrive at a solution, which is
delivered through its output. In Quick Sort, the output is a sorted array, fulfilling the
requirement of providing a result.

Definiteness:
Each step of the algorithm must be clear, unambiguous, and effectively computable.
Example: In a factorial calculation algorithm, the instruction "Multiply the current number
by the next integer decrementing towards 1" is a definite step.
Explanation: Definiteness ensures that each step can be performed without ambiguity, thus
making it possible for the algorithm to be implemented accurately. In the case of calculating
factorial, each step is clearly defined so that it can be executed precisely.

Finiteness
The algorithm must terminate after a finite number of steps.

Example: In the Euclidean algorithm for finding the Greatest Common Divisor (GCD) of two
numbers, the algorithm terminates once the remainder becomes zero.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 11/168


I Year I Sem Computational Thinking and Programming

Explanation: An algorithm must not run indefinitely; it must have an end point. The
Euclidean algorithm satisfies this by terminating when the remainder is zero, ensuring that
it doesn't loop infinitely.

Effectiveness
The operations of the algorithm must be basic so that they can be executed in a finite amount
of time and resources.
Example: In a linear search algorithm, the operation "Compare the target value with the
current array element" is effective and simple.
Explanation: Effectiveness ensures that each step of the algorithm can be executed with
practical constraints of time and resources. The operation in linear search is straightforward
and can be executed quickly, making it effective.

Algorithm to Find the Largest of the Three Numbers


1. Start
2. Input three numbers A, B, C
3. Set Largest to A
4. If B is greater than Largest, then set Largest to B
5. If C is greater than Largest, then set Largest to C
6. Output Largest as the result
7. End

Figure: Characteristics of Algorithm


Why Algorithms Are Required?
In the realm of computer science, algorithms play a vital role in enabling efficient problem-
solving. There are several reasons why algorithms are essential. Let's explore a couple of these
reasons with an example:

Consider the task of sorting a list of names alphabetically. You have a long list of names, and
you want to arrange them in A-to-Z order. Now, let's apply the two points mentioned:
• Scalability: Imagine you have a list of only 10 names. Sorting them manually might
not be too time-consuming. However, if you have a list of 10,000 names or more, doing

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 12/168


I Year I Sem Computational Thinking and Programming

it by hand becomes impractical. Algorithms allow you to break down this large real-
world problem of sorting a massive list into smaller, manageable steps. You can divide
the list into smaller portions, sort each portion, and then merge them back together
using an algorithm like Merge Sort or Quick Sort. This scalability is crucial because
it helps you efficiently handle large-scale problems.
• Feasibility: Sorting a list of names is feasible because it can be broken down into
smaller steps. You can compare two names at a time, determine their order, and
repeat this process until the entire list is sorted. This step-by-step breakdown into
smaller tasks demonstrates the feasibility of the problem. Algorithms provide a
systematic way to tackle each of these steps, ensuring that the problem can be solved
effectively.

So, in this example, the task of sorting a large list of names illustrates the importance of
algorithms. They allow you to scale up your problem-solving capabilities and confirm the
feasibility of addressing complex tasks by breaking them down into manageable steps.
Flowchart
A flowchart is a graphical representation of a process, system, or algorithm using
standardized symbols, shapes, and arrows to depict the sequence of steps or actions.
Flowcharts are a visual tool that helps visualize, understand, and communicate the flow of
activities within a system or procedure.
Importance of Flowcharts:
• Visual Clarity: Flowcharts provide a visual representation of complex processes,
making them easier to understand and follow. They break down intricate systems into
manageable steps, helping individuals grasp the overall structure and logic.
• Process Analysis and Improvement: Flowcharts are instrumental in analyzing
existing processes. They allow individuals and teams to identify bottlenecks,
redundancies, or inefficiencies in a process. This analysis provides a basis for making
improvements and optimizing workflows.
• Documentation and Communication: Flowcharts serve as effective documentation
tools. They enable organizations to document their processes and procedures in a
clear and standardized format. Additionally, flowcharts are useful for communicating
processes and systems to stakeholders, team members, or clients, ensuring everyone
is on the same page.
• Problem Solving and Decision-Making: Flowcharts aid in problem-solving by
breaking down a problem into its constituent parts and showing the relationships
between those parts. When facing complex decisions or challenges, individuals can
use flowcharts to systematically analyze the situation and explore potential solutions.
• Quality Control and Standardization: Flowcharts are valuable for quality control
and standardization efforts. They help ensure that processes are executed
consistently and according to predefined standards. By documenting best practices
in flowcharts, organizations can maintain quality and reduce errors.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 13/168


I Year I Sem Computational Thinking and Programming

Flowchart Symbols

• Data object – The Data object, often referred to as the I/O Shape shows the Inputs to
and Outputs from a process.
• Rectangle – This is used to represent an event which is controlled within the process.
Typically this will be a step or action which is taken.
• Diamond – Used to represent a decision point in the process. Typically, the statement
in the symbol will require a `yes’ or `no’ response and branch to different parts of the
flowchart accordingly.
• Document – The Document object is a rectangle with a wave-like base. This shape is
used to represent a Document or Report in a process flow.
• Rounded box – This is used to represent an event which occurs automatically. Such
an event will trigger a subsequent action, for example, receive a telephone call, or
describe a new state of affairs.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 14/168


I Year I Sem Computational Thinking and Programming

• Stored data – This is a general data storage object used in the process flow as opposed
to data which could be also stored on a hard drive, magnetic tape, memory card, of
any other storage device.
• Manual input – This object is represented by a rectangle with the top sloping up from
left to right. The Manual Input object signifies an action where the user is prompted
for information that must be manually inputted into a system.
• Direct data – Direct data object in a process flow represents information stored which
can be accessed directly. This object represents a computer’s hard drive.
• Circle – Used to represent a point at which the flowchart connects with another
process. The name or reference for the other process should appear within the symbol.
• Internal storage – This is an object which is commonly found in programming
flowcharts to illustrate the information stored in memory, as opposed to on a file.
• Predefined process – This allows you to write one subroutine and call it as often as
you like from anywhere in the code.

Flowchart Examples:

Adding Two Numbers:

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 15/168


I Year I Sem Computational Thinking and Programming

Largest of Three Numbers:

Roots of a Quadratic Equation:

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 16/168


I Year I Sem Computational Thinking and Programming

Fibonacci series Till Term≤1000:

Pseudo Code Solution to Problems


Pseudocode serves as a method for representing algorithms in programming. It doesn't rely
on specific programming language syntax, so it can't be directly executed on a computer.
Several formats can be used for writing pseudocode, often borrowing structures from
programming languages like FORTRAN, Lisp, C, and others.
Many times, an algorithm can be effectively communicated through pseudocode, making it
accessible to programmers familiar with multiple programming languages. Pseudocode allows
for the inclusion of various control structures, such as repeat-until, if-then-else, while, for,
and case statements. It is typically written in a high-level language-like manner.
Example:
Let's consider an algorithm for finding the sum of all even numbers between 1 and N, where
N is a positive integer.
Set sum to 0
Set counter to 1
While counter <= N
If counter is even
Add counter to sum
End If
Increment counter by 1
End While

Display sum

In this pseudocode example, common control structures like "While" to iterate through the
numbers, "If-Then-Else" to check if a number is even, and "Set" to assign values to variables
have been used. This pseudocode provides a clear and language-independent representation

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 17/168


I Year I Sem Computational Thinking and Programming

of the algorithm for summing even numbers, making it easy for programmers with different
language backgrounds to understand and implement.

Pseudocode to Find the Largest of the Three Numbers


1. Start
2. Input three numbers A, B, C.
3. Set Largest to A.
4. If B > Largest, then set Largest to B.
5. If C > Largest, then set Largest to C.
6. Output Largest as the result.
7. End

Basic Structures of C Language


The structure of a C program typically follows a sequential order of statements, with the
program starting at the main function and proceeding line by line until it reaches the end of
the function. There are a set of defined parts in a Structure of C program, which are discussed
as follows:
• Documentation
• Preprocessor Section
• Definition
• Global Declaration
• Main() Function
• Sub Programs
Documentation:
This section is used to include comments which describe the purpose of the program, the
author, the date, and any other relevant information. It is crucial for maintaining code and
ensuring that other developers can understand the purpose and functioning of the program.
/*
Program to Calculate Sum of Two Numbers
Author: UKG
Date: 22nd Sept 2023
Description: This is a simple program to demonstrate the structure of a C program.
*/
Preprocessor Section:
The preprocessor section includes preprocessor directives, which are instructions for the
compiler. These generally start with a # symbol. The most common directive is #include,
which is used to include header files.
#include<stdio.h>
Definition:
Here, you define constants, macros or any other definitions that will be used throughout your
program.
#define MAX 100

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 18/168


I Year I Sem Computational Thinking and Programming

Global Declaration:
In this section, global variables and function prototypes are declared. These are accessible
throughout the program.
int globalVariable = 10;
void displayMessage(); // function prototype
Main() Function:
Every C program must have a main() function. This is the starting point of execution for the
program.
int main() {
int num1 = 5, num2 = 15, sum;

sum = num1 + num2;


printf("Sum of %d and %d is: %d\n", num1, num2, sum);

displayMessage();

return 0;
}
Sub Programs:
Sub programs (or functions) are blocks of code that perform a specific task. They are defined
outside the main() function (either above or below it). These functions can be called from
main() or from other functions.
void displayMessage() {
printf("This is a message from a sub program!");
}
Now, when we combine all these sections together, we get the complete structure of the C
program:
/*
Program to Calculate Sum of Two Numbers
Author: John Doe
Date: 22nd Sept 2023
Description: This is a simple program to demonstrate the structure of a C program.
*/

#include<stdio.h>
#define MAX 100

int globalVariable = 10;


void displayMessage(); // function prototype

int main() {
int num1 = 5, num2 = 15, sum;

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 19/168


I Year I Sem Computational Thinking and Programming

sum = num1 + num2;


printf("Sum of %d and %d is: %d\n", num1, num2, sum);

displayMessage();

return 0;
}

void displayMessage() {
printf("This is a message from a sub program!");
}

Example to Perform Arithmetic Operations


/*
Program to Perform Arithmetic Operations on Two Numbers
Author: John Doe
Date: 22nd Sept 2023
Description: This program takes two numbers as input and performs basic arithmetic
operations.
*/

#include<stdio.h>

// Function prototypes
float add(float a, float b);
float subtract(float a, float b);
float multiply(float a, float b);
float divide(float a, float b);

int main() {
float num1, num2;

// Take input
printf("Enter two numbers separated by space: ");
scanf("%f %f", &num1, &num2);

// Perform operations and display results


printf("Sum of %.2f and %.2f is: %.2f\n", num1, num2, add(num1, num2));
printf("Difference between %.2f and %.2f is: %.2f\n", num1, num2, subtract(num1,
num2));
printf("Product of %.2f and %.2f is: %.2f\n", num1, num2, multiply(num1, num2));

if(num2 != 0.0) {

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 20/168


I Year I Sem Computational Thinking and Programming

printf("Division of %.2f by %.2f is: %.2f\n", num1, num2, divide(num1, num2));


} else {
printf("Division by zero is not allowed.\n");
}

return 0;
}

// Function definitions
float add(float a, float b) {
return a + b;
}

float subtract(float a, float b) {


return a - b;
}

float multiply(float a, float b) {


return a * b;
}

float divide(float a, float b) {


return a / b;
}
• Documentation: Provides basic details about the program.
• Preprocessor Section: Includes the standard I/O header file stdio.h which is needed
for input-output functions.
• Function Prototypes: These declare the functions that will be used later in the
program. It's a way to tell the compiler that these functions exist.
• main() Function: Takes two numbers as input, performs arithmetic operations using
the defined functions, and displays the results.
• Sub Programs (Functions): Implement the arithmetic operations (addition,
subtraction, multiplication, and division).
The user is prompted to enter two numbers. The program then performs the arithmetic
operations on these numbers and displays the results. For division, there's a check to ensure
that the second number isn't zero to avoid division by zero errors.
Variables and Data Types
Variables:
Naming conventions for identifiers in C programming are rules and guidelines that dictate
how you should name variables, functions, constants, and other elements in the
Programming.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 21/168


I Year I Sem Computational Thinking and Programming

CamelCase: This convention capitalizes the first letter of each word except the first one, and
there are no spaces or underscores between words. It is often used for function names and
variable names in C
int myVariable;
void calculateInterestRate();
snake_case: In this convention, words are separated by underscores, and all letters are
lowercase. It's often used for variable names and function names in some coding styles.
Example:
int my_variable;
void calculate_interest_rate();
UPPER_CASE: All letters in the identifier are capitalized, and words are separated by
underscores. This convention is often used for constants.
Example:
#define MAX_VALUE 100
const int ARRAY_SIZE = 10;
Leading Underscore: Identifiers beginning with an underscore (_) are often used to indicate
that a variable or function is private or intended for internal use within a module or library.
They should not be used for global identifiers.

Example:
static int _internal_counter;
In C programming, identifiers (which include variable names) must adhere to specific naming
convention rules for them to be valid and readable.
Rules for Naming Identifiers in C:
Character Set:
• Identifiers can consist of letters (both uppercase and lowercase), digits, and
underscore (_).
• The first character of an identifier must be a letter (uppercase or lowercase) or an
underscore (_).

Length Limitation:
• Identifiers can be of any length, but only the first 31 characters are significant. Some
compilers may have different limits, so it's a good practice to keep identifiers
reasonably short and meaningful.

Case Sensitivity:
• C is case-sensitive, which means that uppercase and lowercase letters are treated as
distinct. So, myVar and myvar are considered different identifiers.

Reserved Words:
• You cannot use C reserved words (also known as keywords) as identifiers. These words
have special meanings in the language and are used for specific purposes. Examples
of reserved words in C include int, for, if, and while.

Consistency:

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 22/168


I Year I Sem Computational Thinking and Programming

• While there is no strict rule for choosing between CamelCase, snake_case, PascalCase,
or other naming conventions, it's essential to be consistent within your codebase.
Choose a convention and stick to it to make your code more readable.

Meaningful Names:
• Choose descriptive and meaningful names for your variables. Names like counter or
value convey more information than generic names like x or temp.

Underscores and Leading Underscores:


• Although it's not forbidden, avoid starting your identifiers with a double underscore
(__) or a single underscore followed by an uppercase letter (_X). Such names are often
reserved for system and compiler-specific identifiers.
• Leading underscores (_) are often used for private or internal identifiers within a
module or library.

Digits in Identifiers:
• You can use digits in identifiers, but they cannot be the first character. For example,
myVar123 is a valid identifier.

Examples:
Valid Identifiers:
int myVar;
float interestRate;
char _internalCounter;
long long accountBalance;
int myVar123;

Invalid Identifiers:
int 123myVar; // Starts with a digit
int for; // Reserved word
int while; // Reserved word
int my-variable; // Contains a hyphen (not allowed)
double my%var; // Contains a special character (not allowed)

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 23/168


I Year I Sem Computational Thinking and Programming

Data Types

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 24/168


I Year I Sem Computational Thinking and Programming

Example: Integer Data Type


#include <stdio.h>
int main()
{
int num1 = 42;
int num2 = -15;
printf("Number 1: %d\n", num1);
printf("Number 2: %d\n", num2);
return 0;
}
Example: Float Data Type
#include <stdio.h>
int main() {
float floatValue = 3.14159;
double doubleValue = 12345.6789;
printf("Float Value: %f\n", floatValue);
printf("Double Value: %lf\n", doubleValue);
return 0;
}
Example: Character Data Type
#include <stdio.h>
int main()
{
char grade = 'A';
printf("Grade: %c\n", grade);
return 0;
}

Input and Output Statements


Types of Input and Output Statements:
• Standard Input/Output (stdio.h): This is the most common way to perform input
and output operations in C using functions like printf and scanf.
• File Input/Output: C provides functions like fopen, fclose, fread, fwrite, etc., for
working with files.
• Formatted Input/Output: Functions like printf and scanf allow you to format input
and output data with format specifiers.
• Unformatted Input/Output: Functions like putc, getc, fread, and fwrite allow reading
and writing data as raw bytes without formatting.
• Character Input/Output: Functions like putchar and getchar are used to read and
write individual characters.

Example 1: Standard Input/Output (stdio.h)


#include <stdio.h>

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 25/168


I Year I Sem Computational Thinking and Programming

int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("You entered: %d\n", num);

return 0;
}
Example 2: File Input/Output
#include <stdio.h>
int main() {
FILE *file;
char text[] = "Hello, File I/O!";
// Open a file for writing
file = fopen("output.txt", "w");
if (file == NULL) {
printf("Error opening the file!\n");
return 1;
}
// Write to the file
fputs(text, file);
// Close the file
fclose(file);
return 0;
}
Example 3: Formatted Input/Output
#include <stdio.h>
int main() {
int num = 42;
float pi = 3.14159;
char name[] = "John";
printf("Integer: %d\n", num);
printf("Float: %.2f\n", pi);
printf("Name: %s\n", name);
return 0;
}
Example 4: Character Input/Output
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
ch = getchar();
printf("You entered: ");

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 26/168


I Year I Sem Computational Thinking and Programming

putchar(ch);
return 0;
}

Types of Operators
• Arithmetic Operators: These operators perform mathematical calculations.
• Relational Operators: Used to compare two values and return a Boolean result (true
or false).
• Logical Operators: Used to combine and manipulate Boolean values.
• Bitwise Operators: Used to perform bitwise operations on integers.
• Assignment Operators: Used to assign values to variables.
• Increment and Decrement Operators: Used to increase or decrease the value of a
variable.
• Conditional (Ternary) Operator: A shorthand way to write if-else statements.

Logical Operators
Logical AND (&&):
The logical AND operator returns true if both of its operands are true; otherwise, it returns
false.
It is represented by the && symbol.
Example:
if (x > 5 && y < 10) {
// This condition is true if both x is greater than 5 and y is less than 10.
}
Logical OR (||):
The logical OR operator returns true if at least one of its operands is true; it returns false only
if both operands are false.
It is represented by the || symbol.
Example:
if (x < 0 || y > 100) {
// This condition is true if either x is less than 0 or y is greater than 100.
}
Logical NOT (!):
The logical NOT operator is used to negate or reverse the value of its operand. If the operand
is true, ! makes it false, and vice versa.
It is represented by the ! symbol.
Example:
if (!isReady) {
// This condition is true if isReady is false (i.e., if something is not ready).
}
Summary
&& (Logical AND) Truth Table:
true && true = true

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 27/168


I Year I Sem Computational Thinking and Programming

true && false = false


false && true = false
false && false = false
|| (Logical OR) Truth Table:
true || true = true
true || false = true
false || true = true
false || false = false
! (Logical NOT) Truth Table:
!true = false
!false = true

Bitwise Operators
Bitwise operators in C are used to manipulate individual bits of data in variables. These
operators work at the binary level and are often used in low-level programming, hardware
manipulation, and optimizing code. C provides six bitwise operators.
Bitwise AND (&):
The bitwise AND operator performs a bitwise AND operation between corresponding bits of
two integers. If both bits are 1, the result bit is 1; otherwise, it's 0.
Example:
int a = 5; // binary: 0101
int b = 3; // binary: 0011
int result = a & b; // binary result: 0001 (decimal: 1)
Explanation:
• a is assigned the decimal value 5, which in binary is 0101.
• b is assigned the decimal value 3, which in binary is 0011.
• Now, let's perform the bitwise AND operation between a and b:
• For each bit position (from left to right), we compare the corresponding bits in a and
b:
• The leftmost bits: 0 & 0 results in 0.
• The second leftmost bits: 1 & 0 results in 0.
• The third leftmost bits: 0 & 1 results in 0.
• The rightmost bits: 1 & 1 results in 1.
• After performing the bitwise AND operation for each bit position, we get the binary
result: 0001.
• Finally, the result is assigned to the variable result, which has a decimal value of 1.
• So, result will have the value 1, which is the decimal representation of the binary
result 0001.
• Therefore, the output of the code is that result is equal to 1.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 28/168


I Year I Sem Computational Thinking and Programming

Bitwise OR (|):
The bitwise OR operator performs a bitwise OR operation between corresponding bits of two
integers. If at least one bit is 1, the result bit is 1; otherwise, it's 0.
Example:
int a = 5; // binary: 0101
int b = 3; // binary: 0011
int result = a | b; // binary result: 0111 (decimal: 7)
Explanation:
• a is assigned the decimal value 5, which in binary is 0101.
• b is assigned the decimal value 3, which in binary is 0011.
• Now, let's perform the bitwise OR operation between a and b:
• For each bit position (from left to right), we compare the corresponding bits in a and
b:
• The leftmost bits: 0 | 0 results in 0.
• The second leftmost bits: 1 | 0 results in 1.
• The third leftmost bits: 0 | 1 results in 1.
• The rightmost bits: 1 | 1 results in 1.
• After performing the bitwise OR operation for each bit position, we get the binary
result: 0111.

• Finally, the result is assigned to the variable result, which has a decimal value of 7.
• So, result will have the value 7, which is the decimal representation of the binary
result 0111.
• Therefore, the output of the code is that result is equal to 7.

Bitwise XOR (^):


The bitwise XOR (exclusive OR) operator performs a bitwise XOR operation between
corresponding bits of two integers. If the bits are different (one is 1, and the other is 0), the
result bit is 1; otherwise, it's 0.
Example:
int a = 5; // binary: 0101
int b = 3; // binary: 0011
int result = a ^ b; // binary result: 0110 (decimal: 6)
Explanation:
• a is assigned the decimal value 5, which in binary is 0101.
• b is assigned the decimal value 3, which in binary is 0011.
• Now, let's perform the bitwise XOR operation between a and b:
• For each bit position (from left to right), we compare the corresponding bits in a and
b:
• The leftmost bits: 0 ^ 0 results in 0.
• The second leftmost bits: 1 ^ 0 results in 1.
• The third leftmost bits: 0 ^ 1 results in 1.
• The rightmost bits: 1 ^ 1 results in 0.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 29/168


I Year I Sem Computational Thinking and Programming

• After performing the bitwise XOR operation for each bit position, we get the binary
result: 0110.
• Finally, the result is assigned to the variable result, which has a decimal value of 6.
• So, result will have the value 6, which is the decimal representation of the binary
result 0110.
• Therefore, the output of the code is that result is equal to 6.

Bitwise NOT (~):


The bitwise NOT operator (also called bitwise complement) inverts all the bits of an integer. It
changes 1s to 0s and 0s to 1s.
Example:
int a = 5; // binary: 0101
int result = ~a; // binary result: 1010 (decimal: -6 in two's complement).
Explanation:
• a is assigned the decimal value 5, which in binary is 0101.
• Now, let's perform the bitwise NOT operation on a:
• For each bit position, the bitwise NOT operator inverts the bits. If the bit is 0, it
becomes 1, and if it's 1, it becomes 0.
• So, after applying the bitwise NOT operation to a, we get the binary result: 1010.
• Finally, the result is assigned to the variable result, which has a decimal value of -6.
• In two's complement representation, when the leftmost bit (the most significant bit) is
1, it indicates a negative number. In this case, 1010 in binary represents -6 in decimal.
• Therefore, the output of the code is that result is equal to -6.

Left Shift (<<):


The left shift operator shifts the bits of the left operand to the left by the number of positions
specified by the right operand. It effectively multiplies the left operand by 2 raised to the
power of the right operand.
Example:
int a = 5; // binary: 0101
int result = a << 2; // binary result: 010100 (decimal: 20)
Explanation:
• a is assigned the decimal value 5, which in binary is 0101.
• Now, let's perform the left shift operation on a by 2 positions:
• The << operator shifts the bits of the left operand (in this case, a) to the left by the
number of positions specified by the right operand (in this case, 2).
• After shifting a to the left by 2 positions, we get the binary result: 010100.
• Finally, the result is assigned to the variable result, which has a decimal value of 20.
• So, result will have the value 20, which is the decimal representation of the binary
result 010100.
• Therefore, the output of the code is that result is equal to 20.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 30/168


I Year I Sem Computational Thinking and Programming

Right Shift (>>):


The right shift operator shifts the bits of the left operand to the right by the number of
positions specified by the right operand. For signed integers, this may involve sign extension
(filling the vacant bits with the sign bit).

Example 1: Arithmetic Operators


#include <stdio.h>
int main() {
int a = 10, b = 5;
int sum, difference, product, quotient, remainder;

sum = a + b;
difference = a - b;
product = a * b;
quotient = a / b;
remainder = a % b;

printf("Sum: %d\n", sum);


printf("Difference: %d\n", difference);
printf("Product: %d\n", product);
printf("Quotient: %d\n", quotient);
printf("Remainder: %d\n", remainder);

return 0;
}
Example 2: Relational Operators
#include <stdio.h>
int main() {
int x = 5, y = 10;
printf("%d > %d is %d\n", x, y, x > y);
printf("%d < %d is %d\n", x, y, x < y);
printf("%d == %d is %d\n", x, y, x == y);
printf("%d != %d is %d\n", x, y, x != y);
return 0;
}
Example 3: Logical Operators
#include <stdio.h>
int main() {
int p = 1, q = 0;
printf("p && q is %d\n", p && q); // Logical AND
printf("p || q is %d\n", p || q); // Logical OR
printf("!p is %d\n", !p); // Logical NOT

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 31/168


I Year I Sem Computational Thinking and Programming

return 0;
}
Example 4: Assignment Operators
#include <stdio.h>
int main() {
int x = 5, y = 10;
x += y; // Equivalent to x = x + y
printf("x += y is %d\n", x);
y *= 2; // Equivalent to y = y * 2
printf("y *= 2 is %d\n", y);
return 0;
}
Example 5: Increment and Decrement Operators
#include <stdio.h>
int main() {
int a = 5, b = 5;
printf("Original values: a = %d, b = %d\n", a, b);
a++; // Increment a by 1
--b; // Decrement b by 1
printf("After increment and decrement: a = %d, b = %d\n", a, b);
return 0;
}
Example 6: Conditional (Ternary) Operator
#include <stdio.h>
int main() {
int num = 10;
printf("The number is %s\n", (num % 2 == 0) ? "even" : "odd");
return 0;
}

Example 7: Bitwise Operators


#include <stdio.h>
int main() {
int a = 5, b = 3;
printf("a & b is %d\n", a & b); // Bitwise AND
printf("a | b is %d\n", a | b); // Bitwise OR
printf("a ^ b is %d\n", a ^ b); // Bitwise XOR
printf("~a is %d\n", ~a); // Bitwise NOT
return 0;
}

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 32/168


I Year I Sem Computational Thinking and Programming

Precedence of Operators
Operator Precedence in C:
• Parentheses ( ): Highest precedence. Operations inside parentheses are evaluated first.
• Postfix Operators (++, --): Increment and decrement operators are applied after the
expression.
• Unary Operators (+, -, !, ~): Unary plus, unary minus, logical NOT, and bitwise NOT.
• Multiplicative Operators (*, /, %): Multiplication, division, and modulus operators,
evaluated left to right.
• Additive Operators (+, -): Addition and subtraction operators, evaluated left to right.
• Shift Operators (<<, >>): Left shift and right shift operators, evaluated left to right.
• Relational Operators (<, <=, >, >=): Less than, less than or equal to, greater than,
greater than or equal to.
• Equality Operators (==, !=): Equal and not equal operators.
• Bitwise AND Operator (&): Bitwise AND operator.
• Bitwise XOR Operator (^): Bitwise XOR operator.
• Bitwise OR Operator (|): Bitwise OR operator.
• Logical AND Operator (&&): Logical AND operator.
• Logical OR Operator (||): Logical OR operator.
• Conditional Operator (?:): Ternary conditional operator.
• Assignment Operators (=, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=): Assignment
operators.
• Comma Operator (,): Comma operator, used to separate expressions

Type Conversion
Types of Type Conversion:
• Implicit Type Conversion (Automatic): This type of conversion is performed by the
C compiler automatically when there's a need to convert one data type to another
without the programmer's intervention. It's also known as "type coercion."
• Explicit Type Conversion (Manual): This type of conversion is done explicitly by the
programmer using casting operators. It allows you to specify the desired type for
conversion.

Example 1: Implicit Type Conversion


#include <stdio.h>
int main() {
int num1 = 5;
double num2 = 2.5;
double result = num1 + num2;
printf("Result: %lf\n", result);
return 0;
}
Example 2: Explicit Type Conversion
#include <stdio.h>

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 33/168


I Year I Sem Computational Thinking and Programming

int main() {
int num1 = 5;
double num2 = 2.5;
double result = (double)num1 + num2;
printf("Result: %lf\n", result);
return 0;
}

Example 3: Type Conversion between Data Types


#include <stdio.h>
int main() {
int integerNum = 42;
float floatNum = 3.14;
// Implicit type conversion from int to float
float result = integerNum + floatNum;
printf("Result: %f\n", result);
return 0;
}

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 34/168


I Year I Sem Computational Thinking and Programming

Unit II
Topics to be Discussed:
Computational Problems: If, If-else, Nested If, Switch-case, Ternary Operator, Goto,
Iteration with For, While, Do-while, Break, Continue, Standard Problems [GCD, Finding
Roots, Generating Factors of a Number, Checking for Leap Year], Number Series Problems
(Fibonacci Series, Natural Numbers, Even Numbers, Prime Numbers, Multiplication Table,
Palindrome Numbers).
A
Selection Statements / Control Statements
Selection statements, also known as control statements, play a crucial role in C programming
as they allow the program to make decisions and execute different code blocks based on
specific conditions. These statements are fundamental for creating programs that can adapt
to various situations and perform different actions as needed.
In C language, the control of the program flows from a given instruction to another. This type
of control flow that occurs from any given command to another is known as the sequential
control flow. Now, in any C program, a programmer might want to repeat some sets of
instructions or even skip the instructions when they are writing logic. Declarations in C, also
known as control declarations or decision-making, help them in making such decisions.
Conditional statements are used in the C programming language for making certain decisions
on the basis of the available conditions. These conditional statements get executed
sequentially in case no condition is present around the statements.
Importance of Selection Statements:
• Conditional Execution: Selection statements enable the program to execute specific
code blocks only when certain conditions are met. This conditional execution is
essential for building flexible and responsive programs. For example, you can use
selection statements to handle different cases or scenarios in your program.
• Decision-Making: They facilitate decision-making within the program. Based on the
evaluation of a condition, the program can choose to follow one path or another. This
is crucial for creating logic in the program, allowing it to choose the appropriate course
of action based on input or other factors.
• Branching: Selection statements provide branching capabilities, allowing the program
to branch out into different execution paths. This branching is essential for
implementing complex logic and handling various scenarios efficiently.
• Error Handling: They are often used for error handling in C programs. If an error
condition is encountered, a selection statement can be used to catch the error and
take appropriate corrective action, such as displaying an error message or logging the
issue.
• User Interaction: Selection statements are instrumental in creating interactive
programs. They can be used to process user input and respond accordingly. For
example, in a menu-driven program, selection statements can be used to determine
which menu option the user has chosen and execute the corresponding functionality.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 35/168


I Year I Sem Computational Thinking and Programming

• Control Flow: They control the flow of execution in a program. By using conditions
and decision-making constructs like if, else if, and else, you can design programs that
follow specific logic paths, ensuring that the code behaves as intended.
• Efficiency: Selection statements can improve program efficiency by allowing you to
execute only the necessary code based on conditions. This helps optimize resource
usage and reduce unnecessary computations, resulting in faster and more efficient
programs.
• Code Readability: Well-designed selection statements can make code more readable
and understandable. By using meaningful condition names and organizing code
logically, programmers can easily follow the program's logic and purpose.
In C programming, some common selection statements include:
• If Statement: Used for simple conditional execution.
• If-Else Statement: Used for branching between two code blocks based on a condition.
• Switch Statement: Used for multiple branching based on the value of an expression.
• Conditional (Ternary) Operator: Provides a concise way to make decisions in
expressions.
If Statements:
This type of statement would enable a programmer to choose various instruction sets on the
basis of the available condition. The instruction sets will only get executed when the
evaluation of the condition turns out to be true. In case the evaluation of the condition is
false, there will be an execution of a different instruction set. These are also known as decision
control statements.
Simple If Statement
A simple if statement is used to conditionally execute a block of code if a specified condition
is true.
Syntax:
if (condition)
{
// Code to be executed if the condition is true
}

Example 1:
#include <stdio.h>
int main()
{
int number = 5;
// Check if the number is positive
if (number > 0) {
printf("The number is positive.\n");
}
printf("This line is always executed.\n");
return 0;
}

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 36/168


I Year I Sem Computational Thinking and Programming

Output:
The number is positive.
This line is always executed.
Explanation:
• In the example above, the program that checks whether a variable number is positive
using a simple if statement.
• The if statement begins with the keyword if, followed by a condition enclosed in
parentheses. In this case, the condition is number > 0, which checks if number is
greater than 0.
• If the condition in the parentheses is true, the block of code enclosed in curly braces
{} following the if statement is executed. In this case, it prints "The number is positive."
• After the if block, there is a printf statement outside of the if block. This statement is
not inside the if block's curly braces, so it is always executed, regardless of whether
the condition in the if statement is true or false.
• In the given example, since number is indeed greater than 0 (it's 5), the condition in
the if statement is true, and therefore, the code inside the if block is executed. As a
result, the program prints "The number is positive."
• After that, it prints "This line is always executed" because it's not part of the if block
and is executed unconditionally.

Example 2:
#include <stdio.h>
int main()
{
int age = 25;
// Check if the person is eligible to vote
if (age >= 18) {
printf("You are eligible to vote.\n");
}
printf("Thank you for checking your eligibility.\n");
return 0;
}
Output:
Enter your age: 20
You are eligible to vote.
Thank you for using our program.

Enter your age: 15


Thank you for using our program.
Explanation:
In this example, the C program that checks whether a person is eligible to vote based on their
age, which is stored in the age variable.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 37/168


I Year I Sem Computational Thinking and Programming

The if statement begins with the keyword if, followed by a condition enclosed in parentheses.
In this case, the condition is age >= 18, which checks if the age is greater than or equal to
18, the legal voting age in many countries.
If the condition in the parentheses is true, the block of code enclosed in curly braces {}
following the if statement is executed. In this case, it prints "You are eligible to vote."
After the if block, there is a printf statement outside of the if block. This statement is not
inside the if block's curly braces, so it is always executed, regardless of whether the condition
in the if statement is true or false.
In the given example, the age is 25, which is indeed greater than or equal to 18. Therefore,
the condition in the if statement is true, and the code inside the if block is executed. As a
result, the program prints "You are eligible to vote."
After that, it prints "Thank you for checking your eligibility" because it's not part of the if
block and is executed unconditionally.

If-Else Statement
The if-else statement in C allows to conditionally execute one block of code if a specified
condition is true and another block of code if the condition is false.

Syntax:
if (condition) {
// Code to be executed if the condition is true
} else {
// Code to be executed if the condition is false
}
Example 1:
#include <stdio.h>
int main() {
int number = -3;

// Check if the number is positive or negative


if (number > 0) {
printf("The number is positive.\n");
} else {
printf("The number is non-positive (negative or zero).\n");
}
printf("This line is always executed.\n");
return 0;
}

Output:
The number is non-positive (negative or zero).
This line is always executed.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 38/168


I Year I Sem Computational Thinking and Programming

Explanation:
• In this example, the C program checks whether a variable number is positive or non-
positive using an if-else statement.
• The if statement begins with the keyword if, followed by a condition enclosed in
parentheses. In this case, the condition is number > 0, which checks if number is
greater than 0.
• If the condition in the parentheses is true, the code inside the first set of curly braces
{} is executed. In this case, it prints "The number is positive."
• If the condition is false, the code inside the else block's curly braces is executed. Here,
it prints "The number is non-positive (negative or zero)."
• After either the if or else block is executed, the program continues to the next line,
which is outside both blocks and unconditionally prints "This line is always executed."
• In the given example, the value of number is -3, which is not greater than 0, so the
condition in the if statement is false. As a result, the program executes the code inside
the else block and prints "The number is non-positive (negative or zero)." After that, it
prints "This line is always executed."

Example 2:
#include <stdio.h>
int main() {
int score;
// Prompt the user for their exam score
printf("Enter your exam score: ");
scanf("%d", &score);

// Check if the score is passing (greater than or equal to 60)


if (score >= 60) {
printf("Congratulations! You passed the exam.\n");
} else {
printf("Sorry, you did not pass the exam.\n");
}
printf("Thank you for using our program.\n");
return 0;
}
Output:
Enter your exam score: 75
Congratulations! You passed the exam.
Thank you for using our program.

Enter your exam score: 45


Sorry, you did not pass the exam.
Thank you for using our program.
Explanation:

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 39/168


I Year I Sem Computational Thinking and Programming

• In this program, the user is prompted to enter their exam score using the printf and
scanf functions.
• After reading the user's score into the score variable, we use an if-else statement to
check whether the score is passing, which in this case is defined as being greater than
or equal to 60. The condition is score >= 60.
• If the condition is true (i.e., the user's score is 60 or higher), the code inside the if
block is executed, which prints "Congratulations! You passed the exam."
• If the condition is false (i.e., the user's score is less than 60), the code inside the else
block is executed, which prints "Sorry, you did not pass the exam."
• After either the if or else block is executed, the program continues to the next line,
which is outside both blocks and unconditionally prints "Thank you for using our
program."
• In Scenario 1, where the user enters a score of 75, the condition in the if statement is
true, and the program prints "Congratulations! You passed the exam." before printing
the thank-you message.
• In Scenario 2, where the user enters a score of 45, the condition in the if statement is
false, and the program skips the code inside the if block. It then proceeds to execute
the code inside the else block, which prints "Sorry, you did not pass the exam,"
followed by the thank-you message.

Else-If Ladder
An "else-if ladder" statement in C is used to handle multiple conditions in a structured way.
It allows you to test multiple conditions in sequence and execute the code block associated
with the first true condition.

Syntax:
if (condition1) {
// Code to be executed if condition1 is true
} else if (condition2) {
// Code to be executed if condition2 is true
} else if (condition3) {
// Code to be executed if condition3 is true
}
// ...
else {
// Code to be executed if none of the conditions are true
}
Example 1:
#include <stdio.h>
int main() {
int score;
// Prompt the user for their exam score
printf("Enter your exam score: ");

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 40/168


I Year I Sem Computational Thinking and Programming

scanf("%d", &score);
// Check and display the grade based on the score
if (score >= 90) {
printf("Your grade is A.\n");
} else if (score >= 80) {
printf("Your grade is B.\n");
} else if (score >= 70) {
printf("Your grade is C.\n");
} else if (score >= 60) {
printf("Your grade is D.\n");
} else {
printf("Your grade is F (Fail).\n");
}
printf("Thank you for using our program.\n");
return 0;
}
Output:
Enter your exam score: 95
Your grade is A.
Thank you for using our program.

Enter your exam score: 82


Your grade is B.
Thank you for using our program.

Enter your exam score: 68


Your grade is D.
Thank you for using our program.

Enter your exam score: 55


Your grade is F (Fail).
Thank you for using our program.
Explanation:
• In this example, the program prompts the user to enter their exam score and stores
it in the score variable.
• It then uses an "else-if ladder" to check the score against multiple conditions in a
specific order. The conditions are structured from highest grade (A) to lowest grade
(F).
• The first condition checks if the score is greater than or equal to 90, and if true, it
prints "Your grade is A." If this condition is true, the rest of the conditions are skipped.
• If the first condition is false, the program checks the second condition (score >= 80),
and if it's true, it prints "Your grade is B." This process continues down the ladder.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 41/168


I Year I Sem Computational Thinking and Programming

• If none of the conditions in the ladder are true, the else block is executed, which
means the score is less than 60, and it prints "Your grade is F (Fail)."
• After printing the grade message, the program always prints "Thank you for using our
program."

Example 2:
#include <stdio.h>
int main() {
int day;
// Prompt the user for a day of the week (1-7)
printf("Enter a day of the week (1-7): ");
scanf("%d", &day);

// Check and display the corresponding day name


if (day == 1) {
printf("Sunday\n");
} else if (day == 2) {
printf("Monday\n");
} else if (day == 3) {
printf("Tuesday\n");
} else if (day == 4) {
printf("Wednesday\n");
} else if (day == 5) {
printf("Thursday\n");
} else if (day == 6) {
printf("Friday\n");
} else if (day == 7) {
printf("Saturday\n");
} else {
printf("Invalid input. Please enter a number between 1 and 7.\n");
}
printf("Thank you for using our program.\n");
return 0;
}
Output:
Enter a day of the week (1-7): 3
Tuesday
Thank you for using our program.

Enter a day of the week (1-7): 7


Saturday
Thank you for using our program.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 42/168


I Year I Sem Computational Thinking and Programming

Enter a day of the week (1-7): 9


Invalid input. Please enter a number between 1 and 7.
Thank you for using our program.
Explanation:
• In this example, the program prompts the user to enter a day of the week as a number
(1-7) and stores it in the day variable.
• It uses an "else-if ladder" to check the value of day against multiple conditions in a
specific order. Each condition corresponds to a day of the week.
• If day matches one of the conditions (e.g., day == 3 for Tuesday), the program prints
the corresponding day name (e.g., "Tuesday") and skips the rest of the conditions in
the ladder.
• If day doesn't match any of the conditions in the ladder, the else block is executed,
which means the input is invalid. In this case, it prints "Invalid input. Please enter a
number between 1 and 7."
• After printing the day name or the error message, the program always prints "Thank
you for using our program."

Switch-Case
The switch-case statement in C is used to evaluate the value of an expression against a series
of possible constant values (cases). It allows the program to execute different code blocks
based on the value of the expression.
A switch statement is a multi-way type of selection statement that would resolve this issue.
The switch declaration comes into play when more than three alternatives (conditions) exist
in a program. This command then switches between all the available blocks on the basis of
the expression value. Then, each block has a corresponding value with it.

Syntax:
switch (expression) {
case constant1:
// Code to be executed if expression matches constant1
break;

case constant2:
// Code to be executed if expression matches constant2
break;

// ... More cases ...

default:
// Code to be executed if expression doesn't match any case
}
Example 1:
#include <stdio.h>

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 43/168


I Year I Sem Computational Thinking and Programming

int main() {
int day;

// Prompt the user for a day of the week (1-7)


printf("Enter a day of the week (1-7): ");
scanf("%d", &day);

// Check and display the corresponding day name using a switch-case statement
switch (day) {
case 1:
printf("Sunday\n");
break;
case 2:
printf("Monday\n");
break;
case 3:
printf("Tuesday\n");
break;
case 4:
printf("Wednesday\n");
break;
case 5:
printf("Thursday\n");
break;
case 6:
printf("Friday\n");
break;
case 7:
printf("Saturday\n");
break;
default:
printf("Invalid input. Please enter a number between 1 and 7.\n");
}

printf("Thank you for using our program.\n");

return 0;
}
Output:
Enter a day of the week (1-7): 3
Tuesday
Thank you for using our program.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 44/168


I Year I Sem Computational Thinking and Programming

Enter a day of the week (1-7): 7


Saturday
Thank you for using our program.

Enter a day of the week (1-7): 9


Invalid input. Please enter a number between 1 and 7.
Thank you for using our program.
Explanation:
• In this example, the program prompts the user to enter a day of the week as a number
(1-7) and stores it in the day variable.
• It uses a switch-case statement to evaluate the value of day against different cases
(constant values) that represent each day of the week.
• If day matches one of the cases (e.g., case 3: for Tuesday), the program executes the
code block associated with that case (e.g., printf("Tuesday\n");) and then exits the
switch statement using the break keyword.
• If day doesn't match any of the cases in the switch statement, the program executes
the code block inside the default case, which prints "Invalid input. Please enter a
number between 1 and 7."
• After printing the day name or the error message, the program always prints "Thank
you for using our program."
• The switch-case statement is useful when you want to compare the value of an
expression to multiple constant values and execute different code blocks based on the
match. The break keyword is used to exit the switch statement after a match is found.

Example 2:
#include <stdio.h>
int main() {
int month;
// Prompt the user to enter a month number (1-12)
printf("Enter a month number (1-12): ");
scanf("%d", &month);

// Check and display the name of the month using a switch-case statement
switch (month) {
case 1:
printf("January\n");
break;
case 2:
printf("February\n");
break;
case 3:
printf("March\n");

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 45/168


I Year I Sem Computational Thinking and Programming

break;
case 4:
printf("April\n");
break;
case 5:
printf("May\n");
break;
case 6:
printf("June\n");
break;
case 7:
printf("July\n");
break;
case 8:
printf("August\n");
break;
case 9:
printf("September\n");
break;
case 10:
printf("October\n");
break;
case 11:
printf("November\n");
break;
case 12:
printf("December\n");
break;
default:
printf("Invalid input. Please enter a number between 1 and 12.\n");
}
printf("Thank you for using our program.\n");
return 0;
}
Output:
Enter a month number (1-12): 3
March
Thank you for using our program.

Enter a month number (1-12): 7


July
Thank you for using our program.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 46/168


I Year I Sem Computational Thinking and Programming

Enter a month number (1-12): 9


September
Thank you for using our program.

Enter a month number (1-12): 13


Invalid input. Please enter a number between 1 and 12.
Thank you for using our program.
Explanation:
• In this example, the program prompts the user to enter a month number as an integer
(1-12) and stores it in the month variable.
• It uses a switch-case statement to evaluate the value of month against different cases
representing each month of the year.
• If month matches one of the cases (e.g., case 3: for March), the program executes the
code block associated with that case (e.g., printf("March\n");) and then exits the
switch statement using the break keyword.
• If month doesn't match any of the cases in the switch statement, the program executes
the code block inside the default case, which prints "Invalid input. Please enter a
number between 1 and 12."
• After printing the month name or the error message, the program always prints
"Thank you for using our program."
• This switch-case statement is a clean and concise way to handle user input for month
selection and provide clear feedback in case of invalid input. It also ensures that the
code executes efficiently based on the value of the month variable.

Example 3:
#include <stdio.h>

int main() {
char grade;

// Prompt the user to enter a grade (A, B, C, D, or F)


printf("Enter your grade (A, B, C, D, or F): ");
scanf(" %c", &grade); // Note the space before %c to consume the newline character

// Check and display a message based on the entered grade using a switch-case statement
switch (grade) {
case 'A':
case 'a':
printf("Excellent! You got an A.\n");
break;
case 'B':
case 'b':
printf("Good job! You got a B.\n");

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 47/168


I Year I Sem Computational Thinking and Programming

break;
case 'C':
case 'c':
printf("You got a C. It's an average grade.\n");
break;
case 'D':
case 'd':
printf("You got a D. You need to improve.\n");
break;
case 'F':
case 'f':
printf("You got an F. You did not pass.\n");
break;
default:
printf("Invalid grade. Please enter A, B, C, D, or F.\n");
}
printf("Thank you for using our program.\n");
return 0;
}

Output:
Enter your grade (A, B, C, D, or F): B
Good job! You got a B.
Thank you for using our program.

Enter your grade (A, B, C, D, or F): f


You got an F. You did not pass.
Thank you for using our program.

Enter your grade (A, B, C, D, or F): X


Invalid grade. Please enter A, B, C, D, or F.
Thank you for using our program.
Explanation:
• In this example, the program prompts the user to enter a grade as a single character
(A, B, C, D, or F) and stores it in the grade variable.
• It uses a switch-case statement to evaluate the value of grade against different cases,
where each case represents a valid grade.
• Note that in this example, we've included both uppercase and lowercase versions of
the grade letters in the cases to make the input case-insensitive. For example, case
'A': matches both 'A' and 'a'.
• If grade matches one of the cases (e.g., case 'B': for 'B' or 'b'), the program executes
the code block associated with that case (e.g., printf("Good job! You got a B.\n");) and
then exits the switch statement using the break keyword.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 48/168


I Year I Sem Computational Thinking and Programming

• If grade doesn't match any of the cases in the switch statement, the program executes
the code block inside the default case, which prints "Invalid grade. Please enter A, B,
C, D, or F."
• After printing the appropriate message or the error message, the program always
prints "Thank you for using our program."
• This switch-case statement allows the program to provide feedback to the user based
on the entered grade while also handling different letter case variations. It's an
effective way to process and respond to character-based input.

Ternary Operator / Conditional Operator


The ternary operator, also known as the conditional operator, is a concise way to write
conditional expressions in programming. It is required to simplify the code and make it more
readable when you need to choose between two values or expressions based on a condition.
The ternary operator is used in cases where you want to assign a value to a variable or perform
an operation based on a condition.
Syntax:
condition ? expression_if_true : expression_if_false;
• condition is the expression that is evaluated as either true or false.
• expression_if_true is the value or expression to be used if the condition is true.
• expression_if_false is the value or expression to be used if the condition is false.
The ternary operator is typically used in the following scenarios:
• Conditional Assignment: Assigning a value to a variable based on a condition.
• Conditional Expression: Evaluating an expression conditionally and using different
values or expressions based on the condition.
• Inline Condition: Using a concise and readable way to represent simple conditional
logic.
• Avoiding Repetition: When you want to avoid repeating similar conditional code
multiple times.
• Short and Clear Conditions: When the condition and the resulting expressions are
short and clear, making the code more concise.
Example 1:
#include <stdio.h>

int main() {
int num1, num2;

// Prompt the user to enter two numbers


printf("Enter the first number: ");
scanf("%d", &num1);

printf("Enter the second number: ");


scanf("%d", &num2);

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 49/168


I Year I Sem Computational Thinking and Programming

// Use the conditional operator to find the largest number


int largest = (num1 > num2) ? num1 : num2;

// Display the largest number


printf("The largest number is: %d\n", largest);

return 0;
}

Output:
Enter the first number: 8
Enter the second number: 3
The largest number is: 8
Enter the first number: 15
Enter the second number: 20
The largest number is: 20

Explanation:
• It prompts the user to enter two numbers, num1 and num2.
• The conditional operator (num1 > num2) ? num1 : num2 is used to compare num1
and num2. If num1 is greater than num2, it assigns the value of num1 to the largest
variable; otherwise, it assigns the value of num2 to largest. This effectively determines
the largest of the two numbers.
• Finally, it displays the value of the largest number using printf.

Example 2:
#include <stdio.h>

int main() {
int num1, num2, num3;

// Prompt the user to enter three numbers


printf("Enter the first number: ");
scanf("%d", &num1);

printf("Enter the second number: ");


scanf("%d", &num2);

printf("Enter the third number: ");


scanf("%d", &num3);

// Use the conditional operator to find the largest number


int largest = (num1 > num2) ? ((num1 > num3) ? num1 : num3) : ((num2 > num3) ? num2

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 50/168


I Year I Sem Computational Thinking and Programming

: num3);

// Display the largest number


printf("The largest number is: %d\n", largest);

return 0;
}
Output:
Enter the first number: 8
Enter the second number: 3
Enter the third number: 12
The largest number is: 12

Enter the first number: 15


Enter the second number: 20
Enter the third number: 10
The largest number is: 20

The program uses nested conditional (ternary) operators to efficiently compare the three input
numbers and assign the largest among them to the largest variable. This allows you to easily
determine and display the largest number among any set of three numbers provided as input.

Explanation:
• The program prompts the user to enter three numbers: num1, num2, and num3.
• We use nested conditional operators to compare the three numbers:
• The first conditional operator (num1 > num2) ? ... compares num1 and num2. If num1
is greater than num2, it proceeds to the next nested conditional (num1 > num3) ?
num1 : num3. If num1 is not greater than num2, it evaluates (num2 > num3) ? num2
: num3. This way, we determine whether num1 or num2 is the largest.
• In the nested condition (num1 > num3) ? num1 : num3, we compare num1 and num3.
If num1 is greater than num3, it assigns the value of num1 to largest. Otherwise, it
assigns the value of num3 to largest.
• Similarly, in the nested condition (num2 > num3) ? num2 : num3, we compare num2
and num3. If num2 is greater than num3, it assigns the value of num2 to largest.
Otherwise, it assigns the value of num3 to largest.
• Finally, the program displays the value of the largest number in the largest variable.
• This nested conditional operator structure allows us to determine the largest of the
three numbers efficiently. The program compares each pair of numbers sequentially,
ensuring that the largest variable ultimately holds the value of the largest number
among the three provided by the user.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 51/168


I Year I Sem Computational Thinking and Programming

Goto
The goto statement in C is used for transferring program control to a labeled statement within
the same function. It is required in very rare and specific situations when other control
structures like loops (for, while, do-while) and conditional statements (if, else, switch) are not
suitable for achieving the desired control flow. The goto statement should be used sparingly
and with caution, as it can make the code less readable and harder to maintain if used
improperly.

Syntax:
label_name:
// Statements

// ...

goto label_name;

• label_name is a user-defined identifier followed by a colon (:), creating a labeled


statement.
• The goto statement is followed by the label name, which specifies the destination
where program control will be transferred.

Example 1:
#include <stdio.h>

int main() {
int count = 1;

start:
if (count <= 5) {
printf("Count: %d\n", count);
count++;
goto start; // Jump back to the "start" label
}

return 0;
}
Output:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 52/168


I Year I Sem Computational Thinking and Programming

Explanation:
• We have a labeled statement named start followed by a colon (start:). This creates a
label where program control can be transferred.
• Inside the main function, we initialize a variable count to 1.
• We use a goto statement to jump to the start label. This creates a loop-like behavior,
repeatedly printing the value of count as long as it is less than or equal to 5.
• Within the loop, we check if count is less than or equal to 5. If it is, we print the value
of count, increment it, and then use the goto statement to jump back to the start
label, effectively creating a loop.
• The loop continues until count becomes greater than 5, at which point the program
exits.

Use Cases for the goto Statement:


The goto statement should be used very sparingly and only in situations where it provides a
clear and necessary advantage over other control structures. Some rare situations where goto
may be considered are:
• Error Handling in Complex Code: In large and complex codebases, goto can be used
for error handling when multiple resources need to be released before exiting a
function.
• Jumping Out of Nested Loops: In situations where you need to exit from nested
loops prematurely, goto can be used to jump out of multiple levels of loops.
• Code Cleanup: In situations where you need to perform cleanup operations (e.g.,
closing files, releasing memory) before exiting a function, goto can help consolidate
cleanup code.

Example 2:
#include <stdio.h>
int main() {
int i = 1;
loop_start:
if (i <= 5) {
printf("Iteration %d\n", i);
i++;
if (i == 4) {
goto skip_iteration;
}
goto loop_start;
}
skip_iteration:
printf("Iteration 4 is skipped.\n");
return 0;
}

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 53/168


I Year I Sem Computational Thinking and Programming

Output:
Iteration 1
Iteration 2
Iteration 3
Iteration 4 is skipped.
Iteration 5

Explanation:
• We have a labeled statement named loop_start followed by a colon (loop_start:). This
creates a label where program control can be transferred.
• Inside the main function, we initialize an integer variable i to 1.
• We use a goto statement to jump to the loop_start label, creating a loop-like behavior.
The loop prints the value of i and increments it as long as i is less than or equal to 5.
• Within the loop, we check if i is equal to 4. If it is, we use another goto statement to
jump to the skip_iteration label, effectively skipping the iteration when i is 4.
• After skipping the iteration, we continue with the loop and print the value of i when
it's not equal to 4.
• The loop completes when i becomes greater than 5, and the program proceeds to the
printf statement after the skip_iteration label.

This example demonstrates how the goto statement can be used to skip specific iterations
within a loop, providing fine-grained control over the flow of the program. However, it's
important to note that such use cases should be rare, and careful consideration should be
given to maintain code readability and avoid potential issues like infinite loops.

Iteration / Looping Constructs


Iterative statements, also known as looping constructs allows to execute a block of code
repeatedly. There are mainly two types of iterations or loops – unbounded iteration or
unbounded loop and bounded iteration or bounded loop. In bounded iteration, repetition is
implemented by constructs that allow a determinate number of iterations. That is, bounded
loops should be used when we know, ahead of time, how many times we need to loop. C
provides for construct as bounded loop.
There are also many occasions when one does not know, ahead of time, how many iterations
may be required. Such occasions require unbounded loops. C provides two types of
unbounded loops: while loop and do...while loop. These types of loops are also known as
indeterminate or indefinite loops.
A loop can either be a pre-test loop or a post-test loop. Ina pre-test loop, the condition is
checked before the beginning of each iteration. If the test expression evaluates to true, the
statements associated with the pre-test loop construct are executed and the process is
repeated till the test expression becomes false. On the other hand, if the test expression
evaluates to false, the statements associated with the construct are skipped and the

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 54/168


I Year I Sem Computational Thinking and Programming

statement next to the loop is executed. So for such a construct, the statements associated
with the construct may not be executed even once.
In the post-test loop, the code is always executed once. At the completion of the loop code,
the test expression is tested. If the test expression evaluates to true, the loop repeats; if the
expression is false the loop terminates.

Figure 1: Looping Constructs - Pre-Test and Post-Test Loops

C Programming has three loop constructs: while, for, and do-while. The first two are pre-test
loops and do-while is a post-test loop. In addition to the test expression, two other processes
are associated with almost all loops. These are initialization and updating. The test expression
always involves a variable, which is known as a loop control variable. Initialization is the
statement that assigns the initial value to the loop control variable.

While
while statement is a pre-test loop. It uses a test expression to control the loop. Since it is a
pre-test loop, it evaluates the test expression before every iteration of the loop.

Figure 2: While Construct

stmT will be executed repeatedly till the value of TestExpr becomes 0 (false). stmT may be a
single statement or multiple statements terminated by a semicolon.
To use the while statement, the test expression should contain a loop control variable. The
initialization of the loop control variable has to be done before the loop starts and updating
must be included in the body of the loop.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 55/168


I Year I Sem Computational Thinking and Programming

Syntax:
while (condition) {
// Code to be executed as long as the condition is true
}
Example 1:
#include <stdio.h>
int main() {
int count = 1;
// Syntax of the while loop:
// The loop continues as long as the condition (count <= 5) is true
while (count <= 5) {
printf("Count: %d\n", count);
count++; // Increment count by 1 in each iteration
}
return 0;
}
Output:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
The while loop allows to execute the code block repeatedly as long as the condition (count <=
5) was true.
Explanation:
• An integer variable count is Declared to be used as loop control variable.
• Inside the main function, set up a while loop:
o Condition: The loop will continue as long as count is less than or equal to 5.
• Inside the loop, use printf to display the current value of count.
• After displaying the value of count, increment it by 1 using the count++ statement.
This step is crucial to ensure that the loop eventually terminates when the condition
becomes false.
• The loop continues to execute until count reaches 6. After count becomes 6, the
condition count <= 5 is no longer true, and the loop terminates.
Example 2:
#include <stdio.h>
int main()
{
int c;
c=5; // Initialization
while(c>0)
{ // Test Expression
printf(“ \n %d”,c);

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 56/168


I Year I Sem Computational Thinking and Programming

c=c-1; // Updating
}
return 0;
}
Output:
This loop contains all the parts of a while loop. When executed in a program, this loop will
output.
5
4
3
2
1
Explanation:
First, the loop initialization, with ‘c’ being assigned the value of 5, is carried out. Then, the
instructions within the while construct are executed repetitively so long as the test
expression, c > 0, is true. The moment the test expression in the while construct evaluates
as false, the program is terminated.
Now, when the while statement is encountered, the test expression, similar to an if-test
expression, is evaluated. In this case, since c is 5 and c > 0, the test expression evaluates to
true. Hence the statement body of the loop is executed sequentially. A printf statement writes
the current value of c on the screen. After this, c is updated by subtracting 1. Thus, c now
has the value of 4. On reaching the end of the loop, the loop condition is checked again. If it
is true, which it is because 4 > 0, the loop is executed once again. In a similar way, the loop
is executed five times.
At the end of the fifth iteration, c has the value of 0 for which the condition will fail since 0 is
not greater than 0. Thus the term loop being applied to this repeating control structure can
be understood since all statements inside the while loop construct will be executed several
times until the condition has failed.

Do- While
The do-while loop is another type of iterative statement in C programming. It is similar to the
while loop but with one crucial difference: the do-while loop always executes its block of code
at least once, and then it checks the condition to determine whether to continue or terminate.
Syntax:
do {
// Code to be executed at least once
} while (condition);

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 57/168


I Year I Sem Computational Thinking and Programming

Figure 3: Do-While Looping Construct

Example 1:
#include <stdio.h>

int main() {
int count = 1;

// Syntax of the do-while loop:


// The loop executes the code block at least once and continues as long as the condition
(count <= 5) is true
do {
printf("Count: %d\n", count);
count++; // Increment count by 1 in each iteration
} while (count <= 5);

return 0;
}
Output:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
The do-while loop ensures that the code block inside it is executed at least once, making it
useful when you need to guarantee the execution of certain code before checking a condition
for repetition.
Explanation:
• Inside the main function, set up a do-while loop:
o The code block inside the loop is executed unconditionally, without checking
the condition initially.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 58/168


I Year I Sem Computational Thinking and Programming

• Inside the loop, use printf to display the current value of count.
• After displaying the value of count, we increment it by 1 using the count++ statement.
This step is crucial to ensure that the loop eventually terminates when the condition
in the while part becomes false.
• After the code block is executed, the loop checks the condition count <= 5. If the
condition is true, the loop continues to execute. If it's false, the loop terminates.
• The loop continues to execute until count reaches 6. After count becomes 6, the
condition count <= 5 becomes false, and the loop terminates.
Example 2:
#include <stdio.h>
int main()
{
int c=5;
do
{
printf(“Hello”);
c++;
} while(c<5);
return 0;
}
Output:
Hello
The statements within the loop are executed at least once.
For
A loop formed by using the for statement is generally called a determinate or definite loop
because the programmer knows exactly how many times it will repeat. The number of
repetitions can be determined mathematically by manually checking the logic of the loop.
Syntax:
for (initialization; condition; update) {
// Code to be executed repeatedly as long as the condition is true
}

Figure 4: For Loop Syntax

Initialization: This part of the loop is the first to be executed. The statement(s) of this part
are executed only once. This statement involves a loop control variable.
TestExpr TestExpr represents a test expression that must be true for the loop to continue
execution.
stmT stmT is a single or block of statements.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 59/168


I Year I Sem Computational Thinking and Programming

Updating: The statements contained here are executed every time through the loop before
the loop condition is tested. This statement also involves a loop control variable.

Figure 5: For Loop Construct Flowchart

Example 1:
int main(void)
{
int i;
for(i = 0; i < 10; i++)
printf(“%d”,i);
return 0’
}
Output:
0123456789

Explanation:

Figure 6: For Loop Example Explanation

The above for loop operates as follows:

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 60/168


I Year I Sem Computational Thinking and Programming

1. Set i equal to 0.
2. If i is less than 10, execute the body of the loop, that is, ‘printf’ and go to step 3;
otherwise, go to the next instruction after the for loop and continue.
3. Increment i.
4. Go to step 2.
Example 2:
#include <stdio.h>
int main()
{
int c, s=0, n;
printf(“\n Enter the No. of terms”);
scanf(“%d”, &n);
for(c=1; c<=n; c++)
s+=c;
printf(“\n Sum is %d”, s);
return 0;
}
Output:
Enter the No. of terms: 7
Sum is :28
Explanation:
• #include <stdio.h>: This line includes the standard input/output library, which is
necessary to use functions like printf and scanf.
• int main(): This is the main function where the execution of the program begins.
• int c, s=0, n;: Here, three integer variables are declared:
o c: It is used as a loop counter.
o s: This variable will store the sum of the natural numbers.
o n: It will store the user's input for the number of terms to calculate the sum
for.
• printf("\n Enter the No. of terms: ");: This line displays a message asking the user to
enter the number of terms they want to find the sum for. The \n adds a newline
character for better formatting.
• scanf("%d", &n);: This line reads an integer input from the user and stores it in the
variable n. The %d in scanf is a format specifier for integers, and &n is used to store
the entered value in the variable n.
• for(c=1; c<=n; c++): This line starts a for loop that will run 'n' times, from 1 to 'n'. The
loop counter is 'c'.
• s += c;: Inside the loop, this statement adds the current value of 'c' to the 's' variable.
It accumulates the sum of natural numbers.
• printf("\n Sum is :%d", s);: After the for loop finishes, this line prints the sum of the
first 'n' natural numbers using printf. The %d is a format specifier for integers, and it
is replaced with the value of the 's' variable.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 61/168


I Year I Sem Computational Thinking and Programming

• return 0;: This line indicates that the program has finished executing and returns a
status code of 0 to the operating system, indicating a successful execution.

Break
Break statement is used to exit a loop prematurely. It is often used within for, while, or do-
while loops to break out of the loop based on a certain condition.
Program:
#include <stdio.h>
int main() {
int i;

// Use a for loop to print numbers from 1 to 10


for (i = 1; i <= 10; i++) {
printf("%d ", i);

// Break out of the loop when i is equal to 5


if (i == 5) {
printf("\nBreak out of the loop at i = %d\n", i);
break; // Exit the loop
}
}
printf("Loop finished\n");
return 0;
}
Output:
12345
Break out of the loop at i = 5
Loop finished
Explanation:
• We include the <stdio.h> header for input and output functions.
• We declare an integer variable i to use as a loop counter.
• We use a for loop to iterate from i = 1 to i = 10.
• Inside the loop, we use printf to print the value of i.
• We check if i is equal to 5 using the if statement:
o If i is equal to 5, we print a message indicating that we're breaking out of the
loop, and then we use the break statement to exit the loop prematurely.
• After exiting the loop, we print "Loop finished."

Continue
In C programming, the continue statement is used to skip the remaining code within the
current iteration of a loop and proceed to the next iteration. It is typically used within loops
like for or while to control the flow of execution.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 62/168


I Year I Sem Computational Thinking and Programming

Program:
#include <stdio.h>
int main() {
// Print even numbers from 1 to 10 using a for loop
printf("Even numbers from 1 to 10:\n");
for (int i = 1; i <= 10; i++) {
// Check if the number is odd; if so, skip to the next iteration
if (i % 2 != 0) {
continue;
}
printf("%d\n", i);
}
return 0;
}
Output:
Even numbers from 1 to 10:
2
4
6
8
10
• The program starts a for loop from 1 to 10.
• For each value of i, it checks if i is an odd number using the condition i % 2 != 0.
• If i is odd, the continue statement is executed, and the program skips the printf
statement.
• If i is even, the printf statement is executed, and the even number is printed.
• This process repeats for each value of i from 1 to 10, resulting in the display of even
numbers only.

Explanation:
• We include the <stdio.h> header for input and output functions.
• In the main function, we use a for loop to iterate from 1 to 10 using the loop variable
i.
• Within the loop, we check if i is an odd number using the condition if (i % 2 != 0). If i
is odd (i.e., the remainder when dividing by 2 is not 0), we execute the continue
statement.
• The continue statement causes the program to skip the remaining code within the
current iteration of the loop and move on to the next iteration.
• If i is an even number, we print it using printf("%d\n", i);. However, if i is odd, the
continue statement will skip this printf statement.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 63/168


I Year I Sem Computational Thinking and Programming

Standard Problems
Greatest Common Divisor
The GCD of two numbers, let's call them 'a' and 'b', is the largest positive integer that divides
both 'a' and 'b' without leaving a remainder. There are several methods to calculate the GCD,
but one of the most common and efficient ones is the Euclidean algorithm.

The Euclidean algorithm is based on the principle that the GCD of 'a' and 'b' is the same as
the GCD of 'b' and the remainder of 'a' divided by 'b'.
GCD(a, b) = GCD(b, a % b)
This process is repeated until 'b' becomes zero. When 'b' becomes zero, the GCD is found,
and it is equal to the value of 'a' at that point.
Program:

#include <stdio.h>

// Function to calculate GCD


int gcd(int a, int b) {
if (b == 0) {
return a; // GCD is found when b becomes zero
}
return gcd(b, a % b); // Recursive call with (b, a % b)
}

int main() {
int num1, num2;

// Input two numbers


printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);

// Calculate and display the GCD


int result = gcd(num1, num2);
printf("GCD of %d and %d is %d\n", num1, num2, result);

return 0;
}

Output:
Enter two numbers: 36 48
GCD of 36 and 48 is 12

Explanation:

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 64/168


I Year I Sem Computational Thinking and Programming

• We include the standard input-output library (stdio.h) to use functions like printf and
scanf.
• We define a function gcd that calculates the GCD of two integers 'a' and 'b' using the
Euclidean algorithm. If 'b' is zero, it returns 'a'; otherwise, it calls itself recursively
with 'b' and 'a % b'.
• In the main function:
o We declare two integer variables num1 and num2 to store the input numbers.
o We prompt the user to input two numbers using printf and read them using
scanf.
• We calculate the GCD by calling the gcd function with num1 and num2 as arguments
and store the result in the result variable.
• Finally, we display the GCD using printf.

Finding Roots
A quadratic equation is an equation of the form:
ax2 + bx + c = 0
Where 'a', 'b', and 'c' are constants, and 'x' is the variable we want to solve for. The solutions
for 'x', also known as roots, can be found using the quadratic formula:
−𝑏 ± √𝑏 2 − 4𝑎𝑐
𝑥=
2𝑎
Working Strategy:
• Calculate the discriminant, which is the value inside the square root: D = b^2 - 4ac.
• If the discriminant is positive (D > 0), there are two real roots.
• If the discriminant is zero (D = 0), there is one real root (a repeated root).
• If the discriminant is negative (D < 0), there are no real roots (two complex roots).

Program:
#include <stdio.h>
#include <math.h>

// Function to calculate roots


void findRoots(float a, float b, float c) {
// Calculate the discriminant
float discriminant = b * b - 4 * a * c;

// Check the value of the discriminant


if (discriminant > 0) {
// Two real roots
float root1 = (-b + sqrt(discriminant)) / (2 * a);
float root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Two real roots: %f and %f\n", root1, root2);
} else if (discriminant == 0) {
// One real root (repeated)

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 65/168


I Year I Sem Computational Thinking and Programming

float root = -b / (2 * a);


printf("One real root (repeated): %f\n", root);
} else {
// No real roots (complex roots)
float realPart = -b / (2 * a);
float imaginaryPart = sqrt(-discriminant) / (2 * a);
printf("No real roots. Complex roots: %f + %fi and %f - %fi\n", realPart, imaginaryPart,
realPart, imaginaryPart);
}
}

int main() {
float a, b, c;

// Input coefficients a, b, and c


printf("Enter coefficients a, b, and c: ");
scanf("%f %f %f", &a, &b, &c);

// Call the function to find roots


findRoots(a, b, c);

return 0;
}
Output:
Enter coefficients a, b, and c: 1 -3 2
Two real roots: 2.000000 and 1.000000

Explanation:
• We include the standard input-output library (stdio.h) and the math library (math.h)
to use mathematical functions like sqrt.
• We define a function findRoots that takes the coefficients 'a', 'b', and 'c' as input and
calculates the roots based on the discriminant.
• In the main function:
o We declare three floating-point variables a, b, and c to store the coefficients of
the quadratic equation.
o We prompt the user to input these coefficients using printf and read them
using scanf.
• We call the findRoots function with the input coefficients 'a', 'b', and 'c' to calculate
and display the roots based on the discriminant.
Generating Factors of a Number
Factors of a number are the integers that evenly divide that number without leaving a
remainder. In other words, if 'a' and 'b' are integers, and 'a' divides 'b' without a remainder,
then 'a' is a factor of 'b'.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 66/168


I Year I Sem Computational Thinking and Programming

To find the factors of a number 'n', you can start by iterating from 1 up to 'n', and for each
number 'i' in that range, check if 'n' is divisible by 'i'. If it is, then 'i' is a factor of 'n'.

If n % i == 0, then 'i' is a factor of 'n'.


Program:
#include <stdio.h>

// Function to find and display factors


void findFactors(int num) {
printf("Factors of %d are: ", num);
for (int i = 1; i <= num; i++) {
if (num % i == 0) {
printf("%d ", i);
}
}
printf("\n");
}

int main() {
int number;

// Input a number
printf("Enter a number: ");
scanf("%d", &number);

// Call the function to find factors


findFactors(number);

return 0;
}
Output:
Enter a number: 12
Factors of 12 are: 1 2 3 4 6 12
Explanation:
• We include the standard input-output library (stdio.h) to use functions like printf and
scanf.
• We define a function findFactors that takes an integer num as input and finds and
displays its factors using a for loop. It iterates from 1 to num and checks if each
number in that range is a factor of num.
• In the main function:
o We declare an integer variable number to store the input number.
o We prompt the user to input a number using printf and read it using scanf.
• We call the findFactors function with the input number to find and display its factors.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 67/168


I Year I Sem Computational Thinking and Programming

Checking for Leap Year


A leap year is a year that has 366 days instead of the usual 365 days. Leap years are needed
to keep our calendar in sync with the Earth's revolutions around the sun. To determine if a
year is a leap year, we follow these rules:
• If a year is divisible by 4, it could be a leap year.
• However, if the year is also divisible by 100, it is not a leap year unless...
• It is divisible by 400. In that case, it is a leap year.

If (year % 4 == 0) and (year % 100 != 0) OR (year % 400 == 0), then it's a leap year.
Program:
#include <stdio.h>

// Function to check for a leap year


int isLeapYear(int year) {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return 1; // It's a leap year
} else {
return 0; // It's not a leap year
}
}

int main() {
int year;

// Input a year
printf("Enter a year: ");
scanf("%d", &year);

// Call the function to check for a leap year


if (isLeapYear(year)) {
printf("%d is a leap year.\n", year);
} else {
printf("%d is not a leap year.\n", year);
}

return 0;
}

Output:
Enter a year: 2024
2024 is a leap year.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 68/168


I Year I Sem Computational Thinking and Programming

Explanation:
• We include the standard input-output library (stdio.h) to use functions like printf and
scanf.
• We define a function isLeapYear that takes an integer year as input and returns 1 if
it's a leap year and 0 if it's not. The function follows the leap year rules explained
earlier.
• In the main function:
o We declare an integer variable year to store the input year.
o We prompt the user to input a year using printf and read it using scanf.
• We call the isLeapYear function with the input year and check its return value. If it's
1, we print that the year is a leap year; otherwise, we print that it's not a leap year.

Number Series Problems


Fibonacci Series
Generating the Fibonacci series involves mathematics based on recurrence relations. The
Fibonacci series is a sequence of numbers in which each number is the sum of the two
preceding ones, usually starting with 0 and 1. Mathematically, it can be defined as follows:

F(0) = 0
F(1) = 1
F(n) = F(n-1) + F(n-2) for n >= 2

F(0) and F(1) are defined as the first two Fibonacci numbers.
To calculate the nth Fibonacci number (where n >= 2), you add the (n-1)th and (n-2)th
Fibonacci numbers.

Example:
To calculate the 6th Fibonacci number (F(6)), you would use the formula:
F(6) = F(5) + F(4)
Now, F(5) can be calculated as:
F(5) = F(4) + F(3)
Continue this process until you reach the base cases (F(0) and F(1)), and you can calculate
F(6) as follows:
F(6) = F(5) + F(4)
F(6) = (F(4) + F(3)) + (F(3) + F(2))
F(6) = ((F(3) + F(2)) + (F(2) + F(1))) + ((F(2) + F(1)) + F(1))
F(6) = (((F(2) + F(1)) + (F(1) + F(0))) + ((F(1) + F(0)) + F(1))) + ((F(1) + F(0)) + F(1))

Now, substitute the base cases:


F(0) = 0

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 69/168


I Year I Sem Computational Thinking and Programming

F(1) = 1

F(6) = (((1 + 0) + (1 + 0)) + ((1 + 0) + 1)) + ((1 + 0) + 1)


F(6) = (2 + 2) + (1 + 1) + 2
F(6) = 4 + 2 + 2
F(6) = 8

So, F(6) is 8.

Program:
#include <stdio.h>

// Function to calculate the nth Fibonacci number


int fibonacci(int n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}

int main() {
int n;
printf("Enter the number of terms for Fibonacci series: ");
scanf("%d", &n);

printf("Fibonacci Series up to %d terms:\n", n);


for (int i = 0; i < n; i++) {
printf("%d ", fibonacci(i));
}

return 0;
}

Output:
Enter the number of terms for Fibonacci series: 10
Fibonacci Series up to 10 terms:
0 1 1 2 3 5 8 13 21 34

Explanation:
• We include the standard input/output library, <stdio.h>, to handle input and output
operations.
• We define a function fibonacci(int n) that calculates the nth Fibonacci number
recursively. The base case checks if n is less than or equal to 1, in which case it

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 70/168


I Year I Sem Computational Thinking and Programming

returns n. Otherwise, it recursively calls itself to calculate fibonacci(n - 1) + fibonacci(n


- 2).
• In the main() function:
o We declare a variable n to store the number of terms the user wants in the
Fibonacci series.
o We prompt the user to enter the number of terms.
o We then iterate from 0 to n-1 and print each Fibonacci number in the series.
• The program calculates and prints the Fibonacci series up to the specified number of
terms.

Natural Numbers
Generating natural numbers within a specified range involves basic arithmetic and iteration.
Natural numbers are positive integers starting from 1 and continuing indefinitely. To generate
natural numbers within a given range, you need to specify the starting and ending points and
then iterate through them to obtain each natural number within that range.

Specify the starting point (lower bound) and ending point (upper bound) for the range of
natural numbers you want to generate.
Use a loop to iterate from the starting point to the ending point.
At each iteration, you obtain a natural number within the specified range.
Example:
Suppose you want to generate natural numbers within the range from 5 to 10.
Mathematically, the range can be defined as follows:
Natural numbers between 5 and 10: {5, 6, 7, 8, 9, 10}

You can achieve this by iterating from 5 to 10 and obtaining each natural number in the
range.

Program:
#include <stdio.h>

// Function to generate natural numbers in a range


void generateNaturalNumbers(int start, int end) {
printf("Natural numbers in the range %d to %d:\n", start, end);
for (int i = start; i <= end; i++) {
printf("%d ", i);
}
printf("\n");
}

int main() {
int lowerBound, upperBound;

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 71/168


I Year I Sem Computational Thinking and Programming

printf("Enter the lower bound of the range: ");


scanf("%d", &lowerBound);

printf("Enter the upper bound of the range: ");


scanf("%d", &upperBound);

generateNaturalNumbers(lowerBound, upperBound);

return 0;
}
Output:
Enter the lower bound of the range: 5
Enter the upper bound of the range: 10
Natural numbers in the range 5 to 10:
5 6 7 8 9 10

Explanation:
• We include the standard input/output library, <stdio.h>, to handle input and output
operations.
• We define a function generateNaturalNumbers(int start, int end) that generates and
prints natural numbers in the specified range. It takes two arguments, start (the lower
bound) and end (the upper bound), and uses a for loop to iterate from start to end,
printing each number in the range.
• In the main() function:
o We declare variables lowerBound and upperBound to store the lower and
upper bounds of the range.
o We prompt the user to enter the lower and upper bounds.
• We call the generateNaturalNumbers function with the provided lower and upper
bounds to generate and print the natural numbers within the specified range.

Even Numbers
Determining even numbers within a specified range involves basic arithmetic and the concept
of divisibility by 2. An even number is an integer that is exactly divisible by 2, meaning there
is no remainder when it is divided by 2. To determine even numbers within a given range,
you need to specify the starting and ending points and then check each number in that range
to see if it's even or not.
• Specify the starting point (lower bound) and ending point (upper bound) for the range
of numbers you want to check for evenness.
• Use a loop to iterate from the starting point to the ending point.
• For each number in the range, check if it is divisible by 2 (i.e., there is no remainder
when divided by 2).
• If a number meets the condition, it is an even number.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 72/168


I Year I Sem Computational Thinking and Programming

Example:
Suppose you want to determine even numbers within the range from 4 to 10.

Mathematically, the range can be defined as follows:


Even numbers between 4 and 10: {4, 6, 8, 10}

You can achieve this by iterating from 4 to 10 and checking if each number is divisible by 2.
Program:
#include <stdio.h>

// Function to determine even numbers in a range


void determineEvenNumbers(int start, int end) {
printf("Even numbers in the range %d to %d:\n", start, end);
for (int i = start; i <= end; i++) {
if (i % 2 == 0) {
printf("%d ", i);
}
}
printf("\n");
}

int main() {
int lowerBound, upperBound;

printf("Enter the lower bound of the range: ");


scanf("%d", &lowerBound);

printf("Enter the upper bound of the range: ");


scanf("%d", &upperBound);

determineEvenNumbers(lowerBound, upperBound);

return 0;
}

Output:
Enter the lower bound of the range: 4
Enter the upper bound of the range: 10
Even numbers in the range 4 to 10:
4 6 8 10

Explanation:

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 73/168


I Year I Sem Computational Thinking and Programming

• We include the standard input/output library, <stdio.h>, to handle input and output
operations.
• We define a function determineEvenNumbers(int start, int end) that determines and
prints even numbers in the specified range. It takes two arguments, start (the lower
bound) and end (the upper bound), and uses a for loop to iterate from start to end.
Inside the loop, it checks if each number is even (i.e., divisible by 2) using the modulo
operator (%).
• In the main() function:
o We declare variables lowerBound and upperBound to store the lower and
upper bounds of the range.
o We prompt the user to enter the lower and upper bounds.
• We call the determineEvenNumbers function with the provided lower and upper
bounds to determine and print even numbers within the specified range.
Prime Numbers
Determining whether a given number is prime involves basic arithmetic and divisibility
checks. A prime number is a positive integer greater than 1 that has exactly two distinct
positive divisors: 1 and itself. To determine if a number is prime, you need to check if it is
divisible by any integer other than 1 and itself.
If the given number is less than or equal to 1, it cannot be prime because prime numbers
start from 2.
For a number n greater than 1, check if it is divisible by any integer from 2 to the square root
of n. If n is divisible by any of these integers, it is not prime. Otherwise, it is prime.
If the number is not divisible by any integer other than 1 and itself, it is considered prime.

Example:
Suppose you want to determine whether the number 17 is prime.
• Start with the number 17.
• Check if it is less than or equal to 1. Since 17 is greater than 1, continue to the next
step.
• Check if 17 is divisible by any integer from 2 to the square root of 17, which is
approximately 4.12 (rounded up to 5 for simplicity). We check divisibility by 2, 3, 4,
and 5.
o 17 is not divisible by 2 (17 % 2 = 1).
o 17 is not divisible by 3 (17 % 3 = 2).
o 17 is not divisible by 4 (17 % 4 = 1).
o 17 is not divisible by 5 (17 % 5 = 2).
• Since 17 is not divisible by any integer other than 1 and itself, it is a prime number.
Program:
#include <stdio.h>
#include <stdbool.h> // Include the standard library for Boolean data type

// Function to determine whether a number is prime


bool isPrime(int n) {

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 74/168


I Year I Sem Computational Thinking and Programming

if (n <= 1) {
return false; // Numbers less than or equal to 1 are not prime
}
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false; // If n is divisible by any integer from 2 to sqrt(n), it's not prime
}
}
return true; // If no divisors are found, n is prime
}

int main() {
int num;

printf("Enter a positive integer: ");


scanf("%d", &num);

if (isPrime(num)) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}

return 0;
}
This program takes a positive integer input from the user, determines whether it is prime
using the isPrime function, and then prints the result. In the example, the number 17 is
determined to be prime.

Output:
Enter a positive integer: 17
17 is a prime number.
Explanation:
• We include the standard input/output library, <stdio.h>, and the stdbool.h library to
use the Boolean data type (bool).
• We define a function isPrime(int n) that takes an integer n as input and returns a
Boolean value (true if n is prime, false otherwise).
o If n is less than or equal to 1, it returns false because numbers less than or
equal to 1 are not prime.
o We use a for loop to iterate from 2 up to the square root of n (rounded up to
the nearest integer). If n is divisible by any integer in this range, it returns
false. Otherwise, it returns true.
• In the main() function:

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 75/168


I Year I Sem Computational Thinking and Programming

o We declare a variable num to store the user's input.


o We prompt the user to enter a positive integer.
• We call the isPrime function with the provided number and print whether the number
is prime or not based on the Boolean value returned by the function.

To Print All the Prime Numbers in a Range.


Program:
#include <stdio.h>

// Function to determine even numbers in a range


void determineEvenNumbers(int start, int end)
{
printf("Even numbers in the range %d to %d:\n", start, end);
for (int i = start; i <= end; i++) {
if (i % 2 == 0) {
printf("%d ", i);
}
}
printf("\n");
}

int main() {
int lowerBound, upperBound;

printf("Enter the lower bound of the range: ");


scanf("%d", &lowerBound);

printf("Enter the upper bound of the range: ");


scanf("%d", &upperBound);

determineEvenNumbers(lowerBound, upperBound);

return 0;
}

Output:
Enter the lower bound of the range: 4
Enter the upper bound of the range: 10
Even numbers in the range 4 to 10:
4 6 8 10

Explanation:

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 76/168


I Year I Sem Computational Thinking and Programming

• We include the standard input/output library, <stdio.h>, to handle input and output
operations.
• We define a function determineEvenNumbers(int start, int end) that determines and
prints even numbers in the specified range. It takes two arguments, start (the lower
bound) and end (the upper bound), and uses a for loop to iterate from start to end.
Inside the loop, it checks if each number is even (i.e., divisible by 2) using the modulo
operator (%).
• In the main() function:
o We declare variables lowerBound and upperBound to store the lower and
upper bounds of the range.
o We prompt the user to enter the lower and upper bounds.
• We call the determineEvenNumbers function with the provided lower and upper
bounds to determine and print even numbers within the specified range.

Multiplication Table

Program:
#include <stdio.h>

// Function to display the multiplication table for a given number


void displayMultiplicationTable(int number, int limit) {
printf("Multiplication table for %d up to %d:\n", number, limit);
for (int i = 1; i <= limit; i++) {
printf("%d x %d = %d\n", number, i, number * i);
}
}

int main() {
int num, limit;

printf("Enter a number: ");


scanf("%d", &num);

printf("Enter the limit: ");


scanf("%d", &limit);

displayMultiplicationTable(num, limit);

return 0;
}
This program takes user input for a number and a limit, and then it displays the
multiplication table for the specified number up to the specified limit. In the example
provided, it displays the multiplication table for 5 up to 10.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 77/168


I Year I Sem Computational Thinking and Programming

Output:
Enter a number: 5
Enter the limit: 10
Multiplication table for 5 up to 10:
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

Explanation:
• We include the standard input/output library, <stdio.h>, to handle input and output
operations.
• We define a function displayMultiplicationTable(int number, int limit) that displays
the multiplication table for a given number up to a specified limit. It takes two
arguments, number (the number for which the table is generated) and limit (the
highest value up to which the table is displayed). Inside the function, we use a for
loop to iterate from 1 to limit and print each multiplication expression along with the
result.
• In the main() function:
o We declare variables num and limit to store the user's input for the number
and the limit of the multiplication table.
o We prompt the user to enter a number and a limit.
• We call the displayMultiplicationTable function with the provided number and limit
to display the multiplication table.
Palindrome Numbers
To determine whether a given number is a palindrome, you need to understand the concept
of palindromes and perform some mathematical operations. A palindrome is a number or a
sequence of characters (such as a word) that reads the same forward and backward. For
numbers, this means that when you reverse the digits, the number remains the same.
• Extract the individual digits from the given number.
• Reverse the order of the digits.
• Compare the reversed number with the original number.
• If the reversed number is the same as the original number, it's a palindrome;
otherwise, it's not.

Example:

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 78/168


I Year I Sem Computational Thinking and Programming

Suppose you want to identify whether the number 121 is a palindrome.

Extract the individual digits: 1, 2, and 1.


Reverse the order of the digits: 1, 2, and 1.
Compare the reversed number (121) with the original number (121).
Since the reversed number is the same as the original number, 121 is a palindrome.
Program:
#include <stdio.h>

// Function to identify whether a number is a palindrome


int isPalindrome(int num) {
int reversedNum = 0, originalNum = num;

while (num > 0) {


int digit = num % 10;
reversedNum = reversedNum * 10 + digit;
num /= 10;
}

return reversedNum == originalNum;


}

int main() {
int number;

printf("Enter a number: ");


scanf("%d", &number);

if (isPalindrome(number)) {
printf("%d is a palindrome.\n", number);
} else {
printf("%d is not a palindrome.\n", number);
}

return 0;
}
This program takes a number as input from the user, checks whether it is a palindrome using
the isPalindrome function, and then prints the result. In the example provided, it correctly
identifies 121 as a palindrome.

Output:
Enter a number: 121
121 is a palindrome.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 79/168


I Year I Sem Computational Thinking and Programming

Explanation:
• We include the standard input/output library, <stdio.h>, to handle input and output
operations.
• We define a function isPalindrome(int num) that takes an integer num as input and
returns an integer:
o Inside the function, we declare two variables, reversedNum and originalNum,
to store the reversed number and the original number, respectively.
o We use a while loop to extract the individual digits from the original number,
reverse them, and store the result in reversedNum.
o We compare reversedNum with originalNum and return the result as 1 if they
are equal (indicating a palindrome) or 0 if they are not.
• In the main() function:
o We declare a variable number to store the user's input.
o We prompt the user to enter a number.
o We call the isPalindrome function with the provided number and print whether
the number is a palindrome or not based on the integer value returned by the
function

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 80/168


I Year I Sem Computational Thinking and Programming

Unit III
Topics to be Discussed:
Arrays: One- and Two-Dimensional Arrays, Creating, Accessing and Manipulating Elements
of Arrays, Row/Column Major Formats, Standard Problems (Addition and Multiplication of
Two Matrixes, Mean, Variance, SD, Maximum and Minimum Elements using Arrays).
Functions: Declaring a Function, Signature of a Function, Passing Parameters to Functions,
Passing Arrays to Functions, Recursion, Standard Problems [Factorial of a Given Number,
Sum of Natural Numbers using Recursion), External, Auto, Local, Static, Register Variables.

Arrays
The variables used so far have all had a common characteristic: each variable can only be
used to store a single value at a time. For example, each of the variables ch, n, and price
declared in the statements
char ch;
int n;
float price;
are of different data types and each variable can only store one value of the declared data
type. These types of variables are called scalar variables. A scalar variable is a single variable
whose stored value is an atomic type. This means that the value cannot be further subdivided
or separated into a legitimate data type.
Memory Allocation to Identifiers

In contrast to atomic types, such as integer, floating point, and double precision data, there
are aggregate types. An aggregate type, which is referred to as both a structured type and a
data structure, is any type whose values can be decomposed and are related by some defined
structure. Additionally, operations must be available for retrieving and updating individual
values in the data structure. Such a derived data type is an array.
Why We Need Arrays?
Consider a brand-new problem: a program that can print its input in reverse order. If there
are two values, this is easy and the program is given below:

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 81/168


I Year I Sem Computational Thinking and Programming

#include <stdio.h>
int main()
{
int v1, v2;
printf(“Enter two values:”);
scanf(“%i %i”, &v1, &v2);
printf(“%i\n%i\n”, v2, v1);
return 0;
}
If there are three values, this is still relatively easy and the program is given below:
#include <stdio.h>
int main()
{
int v1, v2, v3;
printf(“Enter three values: ”);
scanf(“%d %d %d”, &v1, &v2, &v3);
printf(“%d\n %d\n %d \n”, v3, v2, v1);
return 0;
}
But what if there are ten or twenty or one hundred values? Then it is not so easy.
Besides that, the solutions work only if the number of inputs exactly matches with those
expected by the user. Consider another problem: the average of n integer numbers given by
the user can easily be computed as follows.
#include <stdio.h>
int main()
{
int count,s=0, n, num;
float avg;
printf(“\n How many numbers?”);
scanf(“%d”, &n);
for(count=1;count<=n;++count)
{
printf(“\n Enter the Number:”);
scanf(“%d”, &num);
s+=num;
}
avg=(float)s/n;
printf(“Average is %f”, avg);
return 0;
}
Now if the problem is given as ‘Print the numbers that are greater than the average’, then one
solution is to read the numbers twice, i.e.,
• Read in all the numbers and calculate the average.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 82/168


I Year I Sem Computational Thinking and Programming

• Read in all the numbers again, this time checking each as it is read against a
previously calculated average.

If input is from the keyboard, then the user has to enter each number twice and accurately,
with no mistakes. This is not a viable solution. Because, for 25 numbers entered, the user
has to remember all the numbers. But what if there are 50 or 100 numbers? Then, it is not
so easy. To solve this problem, an array is required. It is a collection of numbered elements.
An array is a fundamental data structure that enables the storing and manipulation of
potentially huge quantities of data. An array stores an ordered sequence of homogeneous
values.
Homogeneous means that all the values are of the same data type. The order of the values
are also preserved, i.e., the integer array {1, 2, 3, 4} is different from {1, 4,
3, 2}.
An array can be defined as a data structure consisting of an ordered set of data values of the
homogeneous (same) type.

An array is a collection of individual data elements that are


• Ordered—one can count off the elements 0, 1, 2, 3, ...
• Fixed in size
• Homogeneous—all elements have to be of the same type,
e.g., int, float, char, etc.

In C, each array has two fundamental properties: the data type and the size. Individual array
elements are identified by an integer index. In C, the index begins at zero and is always
written inside square bracket.
Syntax and Array Visualization:
data_type array_name[array_size];
• data_type is the type of data that the array will hold, such as int, float, char, etc.
• array_name is the name you give to the array.
• array_size is the number of elements the array can hold. It must be a positive integer.

Sample Program:
#include <stdio.h>

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 83/168


I Year I Sem Computational Thinking and Programming

int main() {
// Declare an integer array with 5 elements
int numbers[5];

// Initialize the elements of the array


numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;

// Print the elements of the array


printf("Elements of the array:\n");
printf("numbers[0]: %d\n", numbers[0]);
printf("numbers[1]: %d\n", numbers[1]);
printf("numbers[2]: %d\n", numbers[2]);
printf("numbers[3]: %d\n", numbers[3]);
printf("numbers[4]: %d\n", numbers[4]);

return 0;
}
Explanation:
• We include the <stdio.h> header for input and output functions.
• We declare an integer array numbers with a size of 5. This means it can hold 5 integer
values.
• We initialize each element of the array with values 10, 20, 30, 40, and 50, respectively.
• We use printf to print the elements of the array. The %d format specifier is used to
print integers.
• Finally, we return 0 from the main function to indicate successful program execution.
• When you run this program, it will display the values of the array elements on the
screen.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 84/168


I Year I Sem Computational Thinking and Programming

Array Initialization with Declaration

Sample Program:
#include <stdio.h>
int main() {
// Declare and initialize an integer array
int numbers[] = {5, 10, 15, 20, 25};

// Calculate the length of the array


int length = sizeof(numbers) / sizeof(numbers[0]);

// Print the elements of the array


printf("Elements of the array:\n");

for (int i = 0; i < length; i++) {


printf("numbers[%d]: %d\n", i, numbers[i]);
}
return 0;
}
Output:
Elements of the array:
numbers[0]: 5
numbers[1]: 10
numbers[2]: 15
numbers[3]: 20
numbers[4]: 25
Explanation:
• We include the <stdio.h> header for input and output functions.
• We declare and initialize an integer array numbers using the following syntax:
int numbers[] = {5, 10, 15, 20, 25};

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 85/168


I Year I Sem Computational Thinking and Programming

Here, we omit specifying the size of the array explicitly. The compiler automatically
determines the size based on the number of elements provided within the curly braces
{}.
• To find the length of the array, we use the formula:
int length = sizeof(numbers) / sizeof(numbers[0]);
sizeof(numbers) gives the total size (in bytes) of the array.
sizeof(numbers[0]) gives the size (in bytes) of a single element in the array.
Dividing the total size by the size of a single element gives us the length of the array.
• We then use a for loop to iterate through the elements of the array:
for (int i = 0; i < length; i++) {
printf("numbers[%d]: %d\n", i, numbers[i]);
}
• The loop starts from i = 0 and continues until i is less than the length of the array.
• Inside the loop, we use printf to print each element along with its index.

Array Initialization After Declaration (Using Loops)


for (int i = 0; i < N; i++)
{
array_name[i] = valuei;
}
Sample Program:
#include <stdio.h>
int main()
{
//Array Initialization using List of Values
int arr[5] = { 1, 4, 7, 3, 9 };
//Array Initialization using Initializer List Without Specifying Size
int arr1[] = { 1, 4, 7, 3, 9 };
//Array Initialization Using For Loop
float arr2[5];
for (int i = 0; i < 5; i++)
{
arr2[i] = (float)i * 2.1;
}
return 0;
}
Explanation:
• Array Initialization using List of Values (arr):
o In this section, you declare and initialize an integer array arr with a size of 5.
o The array is initialized with the values {1, 4, 7, 3, 9}.
• Array Initialization using Initializer List Without Specifying Size (arr1):
o Here, you declare and initialize another integer array arr1.
o The size of arr1 is not explicitly specified, but it is determined automatically
based on the number of elements in the initializer list.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 86/168


I Year I Sem Computational Thinking and Programming

o arr1 is also initialized with the values {1, 4, 7, 3, 9}.


• Array Initialization Using For Loop (arr2):
o In this section, you declare a floating-point array arr2 with a size of 5.
o Then, you use a for loop to initialize the elements of arr2 in a loop.
o Each element of arr2 is set to a floating-point value based on the loop index
multiplied by 2.1.

One Dimensional Arrays – Creation of Arrays


Sample Program:
#include <stdio.h>
int main() {
// Declare and initialize a one-dimensional integer array
int numbers[5] = { 10, 20, 30, 40, 50 };

// Print the elements of the array


printf("Elements of the array:\n");

for (int i = 0; i < 5; i++) {


printf("numbers[%d]: %d\n", i, numbers[i]);
}

return 0;
}
Output:
Elements of the array:
numbers[0]: 10
numbers[1]: 20
numbers[2]: 30
numbers[3]: 40
numbers[4]: 50
Explanation:
• We include the <stdio.h> header for input and output functions.
• We declare and initialize a one-dimensional integer array numbers with a size of 5.
o The array is initialized with the values {10, 20, 30, 40, 50}.
• We use a for loop to iterate through the elements of the array:
o The loop variable i is used as an index to access each element.
o Inside the loop, we use printf to print each element along with its index.

The program creates a one-dimensional integer array named numbers, initializes it with the
values 10, 20, 30, 40, and 50, and then prints each element along with its index. This
demonstrates the creation of a one-dimensional array in C and how to access and print its
elements.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 87/168


I Year I Sem Computational Thinking and Programming

Accessing and Manipulating Elements of Arrays


Accessing Elements of Arrays
Accessing Any Element of an Array using Array Subscript Operator [ ] and Index Value i of
the Element.
Array_Name[Index];
Indexing in the Array Always Starts With 0, i.e., First Element is at Index 0 and Last Element
is at N – 1, where N is the Number of Elements in the Array.

You can access elements of an array using square brackets [] and the index of the element
you want to access. The index is a non-negative integer that specifies the position of the
element within the array. Array indices in C start from 0.
int myArray[5] = {10, 20, 30, 40, 50};
int element = myArray[2]; // Accessing the third element (30) at index 2
In this example, myArray[2] accesses the third element (30) because C arrays are zero-based.

Sample Program:
#include <stdio.h>
int main()
{
//Array Declaration and Initialization
int arr[5] = { 5, 2, 3, 8, 51 };

//Accessing Element at Index 2 i.e 3rd Element


printf("Element at arr[2]: %d\n", arr[2]);

//Accessing Element at Index 4 i.e Last Element


printf("Element at arr[4]: %d\n", arr[4]);

//Accessing Element at Index 0 i.e First Element


printf("Element at arr[0]: %d", arr[0]);
return 0;
}
Output:
Element at arr[2]: 3
Element at arr[4]: 51
Element at arr[0]: 5

Manipulating Elements of Arrays


Updating the Value of an Element at Any Given Index i is Possible in a Similar Way to
Assigning/Accessing an Element by Using the Array Subscript Operator [ ] and Assignment
Operator =.
Array_Name[i] = New_Value;

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 88/168


I Year I Sem Computational Thinking and Programming

You can also manipulate (change) the values of array elements by using the same indexing
approach:
int myArray[5] = {10, 20, 30, 40, 50};
myArray[1] = 25; // Changing the value of the second element to 25

After this operation, myArray will be {10, 25, 30, 40, 50}.
You can use loops like for or while to iterate through the elements of an array and apply
operations to each element. For example, you can double all elements of an array:
int myArray[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) {
myArray[i] *= 2; // Double each element
}
After this operation, myArray will be {20, 40, 60, 80, 100}.
Keep in mind that when you manipulate array elements, you need to ensure that the index
you use falls within the valid range of the array (i.e., between 0 and the size of the array
minus one) to avoid accessing memory outside the array boundaries, which can lead to
undefined behavior.

Array Traversal using Loops


for (int i = 0; i < N; i++)
{
printf(arr[i]);
}

Array of Characters:
Similar to the Way that a Numeric Array, a Character Array be Created
Sample Program:
char arr[5] = {‘a’, ‘e’, ‘i’, ‘o’, ‘u’}
Here the arr is Declared to be of Character Datatype and Size = 5.
It Can Be Observed that Character Values are Enclosed in Single Quotes.
#include <stdio.h>
int main()
{

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 89/168


I Year I Sem Computational Thinking and Programming

// creating array of character


char arr[5] = { ‘a', 'e', ‘i', ‘o', ‘u'};

// printing string
int i = 0;
while (arr[i])
{
printf("%c", arr[i++]);
}
return 0;
}
Output:
aeiou
Here, The While Loop Checks for the Truth Value of arr[i], i.e., If There Exists a Value for arr[i]
then it will be True.
Two-Dimensional Array
A two-dimensional array is an array of arrays, where each element is itself an array.

Syntax:
data_type array_name[rows][columns];
• data_type is the type of elements that the array will hold (e.g., int, float, char, etc.).
• array_name is the name you give to the array.
• rows is the number of rows in the array.
• columns is the number of columns in the array.

Sample Program:
#include <stdio.h>

int main() {
// Declare and initialize a 2D integer array
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

// Print the elements of the 2D array


printf("Elements of the 2D array:\n");

for (int i = 0; i < 3; i++) {


for (int j = 0; j < 3; j++) {
printf("matrix[%d][%d]: %d\n", i, j, matrix[i][j]);
}

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 90/168


I Year I Sem Computational Thinking and Programming

return 0;
}
Output:
Elements of the 2D array:
matrix[0][0]: 1
matrix[0][1]: 2
matrix[0][2]: 3
matrix[1][0]: 4
matrix[1][1]: 5
matrix[1][2]: 6
matrix[2][0]: 7
matrix[2][1]: 8
matrix[2][2]: 9
Explanation:
The program declares, initializes, and prints the elements of the 2D array matrix. It
demonstrates how to work with a two-dimensional array, iterating through rows and columns
to access and print its elements.
• We include the <stdio.h> header for input and output functions.
• We declare and initialize a 2D integer array matrix with 3 rows and 3 columns.
o The array is initialized with values in a row format:
1 2 3
4 5 6
7 8 9
• We use nested for loops to iterate through the elements of the 2D array.
o The outer loop (i) iterates through the rows.
o The inner loop (j) iterates through the columns.
• Inside the nested loops, we use printf to print each element along with its row and
column indices.

Row/Column Major Formats


Row-major and column-major are two different memory layout formats used to store multi-
dimensional arrays in memory. These formats determine the order in which elements of the
array are stored in memory and affect how data is accessed efficiently.
Row-Major Formats
In row-major order, elements of a multi-dimensional array are stored row by row in memory.
In a 2D array, this means that all elements of the first row are stored sequentially, followed
by all elements of the second row, and so on.
For example, consider a 2D array:
1 2 3
4 5 6
7 8 9
In row-major format, it would be stored in memory like this:

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 91/168


I Year I Sem Computational Thinking and Programming

1 2 3 4 5 6 7 8 9

Row-major is the default format for most programming languages, including C and C++.
When you access elements of a row-major array, you typically iterate through rows first and
then columns.

Column-Major Formats
In column-major order, elements of a multi-dimensional array are stored column by column
in memory. In a 2D array, this means that all elements of the first column are stored
sequentially, followed by all elements of the second column, and so on.
Using the same 2D array as above:
1 2 3
4 5 6
7 8 9
In column-major format, it would be stored in memory like this:
1 4 7 2 5 8 3 6 9

Standard Problems
Addition of Two Matrices
#include <stdio.h>
// Function to read a matrix
void readMatrix(int matrix[][100], int m, int n)
{
printf("Enter elements of the %d x %d matrix:\n", m, n);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}
}

// Function to perform matrix addition


void addMatrices(int A[][100], int B[][100], int m, int n, int C[][100]) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
C[i][j] = A[i][j] + B[i][j];
}
}
}

// Function to display a matrix


void displayMatrix(int matrix[][100], int m, int n) {
printf("Resultant %d x %d matrix:\n", m, n);
for (int i = 0; i < m; i++) {

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 92/168


I Year I Sem Computational Thinking and Programming

for (int j = 0; j < n; j++) {


printf("%d ", matrix[i][j]);
}
printf("\n");
}
}

int main() {
int m, n;

// Read dimensions of the matrices


printf("Enter the number of rows: ");
scanf("%d", &m);
printf("Enter the number of columns: ");
scanf("%d", &n);

int A[100][100], B[100][100], C[100][100];

// Read the first matrix


printf("Enter the elements of the first matrix:\n");
readMatrix(A, m, n);

// Read the second matrix


printf("Enter the elements of the second matrix:\n");
readMatrix(B, m, n);

// Perform matrix addition


addMatrices(A, B, m, n, C);

// Display the resultant matrix


displayMatrix(C, m, n);

return 0;
}
Output:
Enter the number of rows: 3
Enter the number of columns: 3
Enter the elements of the first matrix:
Enter elements of the 3 x 3 matrix:
1 13 3 4 2 5 12 1 1
Enter the elements of the second matrix:
Enter elements of the 3 x 3 matrix:
123456789

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 93/168


I Year I Sem Computational Thinking and Programming

Resultant 3 x 3 matrix:
2 15 6
8 7 11
19 9 10
Explanation:
// Function to read a matrix
void readMatrix(int matrix[][100], int m, int n) {
// Function body
}

// Function to perform matrix addition


void addMatrices(int A[][100], int B[][100], int m, int n, int C[][100]) {
// Function body
}

// Function to display a matrix


void displayMatrix(int matrix[][100], int m, int n) {
// Function body
}
• These functions are defined to perform the following tasks:
o readMatrix: Reads the elements of a matrix from the user.
o addMatrices: Performs matrix addition.
o displayMatrix: Displays the elements of a matrix.
• In the main function:
o We declare integer variables m and n to store the number of rows and columns
of the matrices.
o We use printf and scanf to read the dimensions (number of rows and columns)
of the matrices from the user.

This program allows the user to input the dimensions and elements of two matrices, performs
their addition if they are compatible, and then displays the resultant matrix. It's a structured
way to perform matrix addition in C with separate functions for each part of the operation.
Multiplication of Two Matries
Sample Program:
#include <stdio.h>
#define MAX_ROWS 10
#define MAX_COLS 10
// Function to read a matrix from the user
void readMatrix(int mat[MAX_ROWS][MAX_COLS], int rows, int cols) {
printf("Enter elements of the matrix (%d x %d):\n", rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &mat[i][j]);
}

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 94/168


I Year I Sem Computational Thinking and Programming

}
}

// Function to multiply two matrices and store the result in 'result'


void multiplyMatrices(int mat1[MAX_ROWS][MAX_COLS], int rows1, int cols1,
int mat2[MAX_ROWS][MAX_COLS], int rows2, int cols2,
int result[MAX_ROWS][MAX_COLS]) {
// Check if matrices can be multiplied (cols1 must equal rows2)
if (cols1 != rows2) {
printf("Matrix multiplication is not possible due to incompatible dimensions.\n");
return;
}

// Initialize the result matrix with zeros


for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
result[i][j] = 0;
}
}

// Perform matrix multiplication


for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
for (int k = 0; k < cols1; k++) {
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
}

// Function to display a matrix


void displayMatrix(int mat[MAX_ROWS][MAX_COLS], int rows, int cols) {
printf("Matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
}

int main() {
int mat1[MAX_ROWS][MAX_COLS];

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 95/168


I Year I Sem Computational Thinking and Programming

int mat2[MAX_ROWS][MAX_COLS];
int result[MAX_ROWS][MAX_COLS];
int rows1, cols1, rows2, cols2;

// Input for the first matrix


printf("Enter the number of rows and columns for the first matrix (m x n): ");
scanf("%d %d", &rows1, &cols1);
readMatrix(mat1, rows1, cols1);

// Input for the second matrix


printf("Enter the number of rows and columns for the second matrix (p x q): ");
scanf("%d %d", &rows2, &cols2);
readMatrix(mat2, rows2, cols2);

// Perform matrix multiplication


multiplyMatrices(mat1, rows1, cols1, mat2, rows2, cols2, result);

// Display the result matrix


if (rows1 > 0 && cols2 > 0) {
displayMatrix(result, rows1, cols2);
}

return 0;
}
Output:
Enter the number of rows and columns for the first matrix (m x n): 2 2
Enter elements of the matrix (2 x 2):
1234
Enter the number of rows and columns for the second matrix (p x q): 2 3
Enter elements of the matrix (2 x 3):
123456
Matrix:
9 12 15
19 26 33
Explanation:
• We include the <stdio.h> header for input and output functions.
• We define macros MAX_ROWS and MAX_COLS to specify the maximum dimensions
of matrices.
• We define three functions:
o readMatrix: Reads a matrix from the user.
o multiplyMatrices: Multiplies two matrices and stores the result in result.
o displayMatrix: Displays a matrix.
• In the main function:

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 96/168


I Year I Sem Computational Thinking and Programming

o We declare arrays for the two input matrices (mat1 and mat2) and the result
matrix (result).
o We take input for the dimensions and elements of both input matrices using
the readMatrix function.
• We call the multiplyMatrices function to perform matrix multiplication. If the matrices
cannot be multiplied due to incompatible dimensions, an error message is displayed.
• Finally, we display the result matrix using the displayMatrix function.

Mean
∑ 𝐸𝑙𝑒𝑚𝑒𝑛𝑡𝑠
𝑀𝑒𝑎𝑛 =
𝑁𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝐸𝑙𝑒𝑚𝑒𝑛𝑡𝑠
Program:
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
int sum = 0;

for(int i = 0; i < n; i++) {


sum += arr[i];
}

float mean = (float)sum / n;


printf("Mean: %.2f\n", mean);

return 0;
}
Output:
Mean: 3.00

Explanation:
This program calculates the mean (average) of the elements in an array.
sum is used to accumulate the total of the array elements, which is then divided by the
number of elements (n) to get the mean.

Variance
∑(𝑥𝑖2 )
𝑉𝑎𝑟𝑖𝑎𝑛𝑐𝑒 = − 𝑚𝑒𝑎𝑛2 , where xi is Each Element
𝑛

Program:
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 97/168


I Year I Sem Computational Thinking and Programming

int sum = 0, sumOfSquares = 0;

for(int i = 0; i < n; i++) {


sum += arr[i];
sumOfSquares += arr[i] * arr[i];
}

float mean = (float)sum / n;


float variance = (float)sumOfSquares / n - mean * mean;
printf("Variance: %.2f\n", variance);

return 0;
}
Output:
Variance: 2.00

Explanation:
This program calculates the variance of the array elements.

Standard Deviation
𝑆𝑡𝑎𝑛𝑑𝑎𝑟𝑑 𝐷𝑒𝑣𝑖𝑎𝑡𝑖𝑜𝑛 = √𝑉𝑎𝑟𝑖𝑎𝑛𝑐𝑒
Program:
#include <stdio.h>
#include <math.h>

int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
int sum = 0, sumOfSquares = 0;

for(int i = 0; i < n; i++) {


sum += arr[i];
sumOfSquares += arr[i] * arr[i];
}

float mean = (float)sum / n;


float variance = (float)sumOfSquares / n - mean * mean;
float stdDeviation = sqrt(variance);
printf("Standard Deviation: %.2f\n", stdDeviation);

return 0;
}

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 98/168


I Year I Sem Computational Thinking and Programming

Output:
Standard Deviation: 1.41
Explanation:
This program calculates the standard deviation based on the variance.
The sqrt function from math.h is used to compute the square root.

Maximum and Minimum Elements using Arrays


Program:
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
int max = arr[0], min = arr[0];

for(int i = 1; i < n; i++) {


if(arr[i] > max) {
max = arr[i];
}
if(arr[i] < min) {
min = arr[i];
}
}

printf("Maximum: %d\n", max);


printf("Minimum: %d\n", min);

return 0;
}
Output:
Maximum: 5
Minimum: 1

Explanation:
This program finds the maximum and minimum elements in an array.
It iterates through the array, updating max and min as it finds larger or smaller elements,
respectively.

Mean, Variance, Standard Deviation, Minimum and Maximum Element


of An Array
Mean: The average of all elements.
Variance: Measures how far each number in the set is from the mean.
Standard Deviation: The square root of the variance.
Maximum and Minimum Elements: The highest and lowest values in the array.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 99/168


I Year I Sem Computational Thinking and Programming

Program:
#include <stdio.h>
#include <math.h>

int main() {
int arr[] = {1, 2, 3, 4, 5}; // Example array
int n = sizeof(arr) / sizeof(arr[0]); // Number of elements in the array

// Variables for calculations


int sum = 0, max = arr[0], min = arr[0];
double mean, variance = 0, stdDeviation;

// Calculating sum, max, and min


for (int i = 0; i < n; i++) {
sum += arr[i];
if (arr[i] > max) max = arr[i];
if (arr[i] < min) min = arr[i];
}

// Calculating mean
mean = (double)sum / n;

// Calculating variance
for (int i = 0; i < n; i++) {
variance += pow(arr[i] - mean, 2);
}
variance /= n;

// Calculating standard deviation


stdDeviation = sqrt(variance);

// Printing results
printf("Mean = %f\n", mean);
printf("Variance = %f\n", variance);
printf("Standard Deviation = %f\n", stdDeviation);
printf("Maximum Element = %d\n", max);
printf("Minimum Element = %d\n", min);

return 0;
}
Output:
Mean = 3.000000
Variance = 2.000000

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 100/168


I Year I Sem Computational Thinking and Programming

Standard Deviation = 1.414214


Maximum Element = 5
Minimum Element = 1

Explanation:
Array and Its Size:
• The array arr contains the elements for which calculations are performed.
• n stores the number of elements in the array.

Calculating Sum, Max, and Min:


• A loop iterates through the array to calculate the sum of all elements, and
simultaneously finds the maximum and minimum elements.

Mean Calculation:
• The mean (average) is calculated by dividing the sum of all elements by the number
of elements.

Variance Calculation:
• Another loop calculates the variance. It sums the squared differences between each
element and the mean, then divides by the number of elements.

Standard Deviation Calculation:


• The standard deviation is the square root of the variance.

Printing Results:
• Finally, the program prints the calculated mean, variance, standard deviation,
maximum element, and minimum element.

Functions
Functions in C are blocks of code that perform a specific task. They enhance modularity and
code reuse, allowing for a more structured and manageable approach to programming.
Definition and Purpose
A function in C is a self-contained block of code that performs a specific operation. It is a
fundamental concept in structured programming, allowing developers to break down complex
problems into smaller, more manageable tasks.
Purpose: The primary purpose of functions is to segment a large program into smaller, logical
sections. Each function is designed to perform a particular task and can be used repeatedly
throughout a program.
Characteristics of Functions
• Modularity: Functions promote modularity in programming. By dividing a program
into smaller functions, the overall structure becomes more organized and
manageable. This modularity helps in simplifying the development process and
enhancing code readability.
• Code Reuse: Functions enable code reuse, a significant advantage in programming.
Once a function is written to perform a task, it can be reused in different parts of the
program, or even in different programs, without needing to rewrite the code.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 101/168


I Year I Sem Computational Thinking and Programming

• Abstraction: Functions abstract the implementation details. When using a function,


one does not need to understand how it works internally. This abstraction allows
focusing on what the function does rather than how it does it, thus simplifying the
programming process.

Why Shall we Study Functions?


• Modularity: Functions allow a program to be broken down into smaller, manageable
segments. This modularity makes the code more organized, readable, and easier to
debug.
• Code Reuse: Once a function is written, it can be reused multiple times throughout
a program. This reuse eliminates the need for repetitive code, reducing the likelihood
of errors and saving time.

• Abstraction: Functions abstract the implementation details. A user of the function


does not need to understand the inner workings of the function to utilize it. This
abstraction simplifies the programming process.
• Maintainability: With functions, if a change needs to be made in a repeated piece of
logic, it can be done in one place (within the function) rather than at every place the
logic is used. This centralization makes programs easier to maintain and update.
• Scalability: Functions allow a program to be scaled with less effort. As the program
grows, functions can be added or modified independently, without impacting other
parts of the program significantly.
• Testing and Debugging: Functions isolate functionality, making it easier to test
individual parts of the program. Debugging is also more straightforward because you
can test each function independently.
• Parameter Passing: Functions enable the passing of data (parameters), allowing
flexibility in what a function does and how it behaves with different inputs. This
parameter passing is a cornerstone of efficient and flexible programming.
• Recursion: Functions enable the use of recursion, a powerful programming technique
where a function calls itself to solve a problem. Recursion can simplify the code and
reduce memory usage for certain types of problems.

Declaring a Function
A function declaration (or prototype) in C provides the function's name, return type, and
parameters without its actual body. It serves as an interface to the function. The function
definition includes the actual body of the function. It contains the set of instructions that are
executed when the function is called.
A function declaration tells the compiler about a function's name, return type, and
parameters. It's also known as a function prototype.
Syntax for Function Declation:
returnType functionName(parameterType1 parameter1, parameterType2 parameter2, ...);
• returnType: This specifies the type of value the function will return. If the function
does not return a value, this is specified as void.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 102/168


I Year I Sem Computational Thinking and Programming

• functionName: This is the identifier for the function. It's the name used to call the
function from other parts of the program.
• parameterType1 parameter1, parameterType2 parameter2, ...: These are the
parameters that the function accepts. Each parameter is specified by a type
(parameterType) and a name (parameter). The function can accept multiple
parameters, separated by commas. If the function takes no parameters, this can be
left empty or specified with void in some cases.

Function with No Return and No Parameters


void greet();
Declares a function named greet that takes no parameters and does not return any value.

Function with Return Value and Parameters


int add(int num1, int num2);
Declares a function named add that takes two parameters (both of type int) and returns an
integer value.

Function with Return Value but No Parameters


double getCurrentTemperature();
Declares a function named getCurrentTemperature that takes no parameters and returns a
double value.

Function with Void Parameters (Explicitly No Parameters)


void displayMessage(void);
Declares a function named displayMessage that explicitly takes no parameters and does not
return any value. The use of void in the parameter list is optional and more common in C
programming to explicitly indicate that the function takes no arguments.

Example 1:
int sum(int a, int b);
Declares a function named sum that takes two int parameters and returns an int.
No function body is provided here; it's just a declaration.
Example 2:
void displayMessage();
Declares a function displayMessage that takes no parameters and returns nothing (void).

Signature of a Function
A function signature includes the function name and its parameter list. It's used to uniquely
identify the function. It specifies how the function can be used in terms of its inputs and
output. Understanding the function signature is essential for both the correct use of existing
functions and the design of new functions.
The signature of a function comprises the function's name and its parameter list. It defines
the function's interface without considering its return type. In many programming contexts,
however, the return type is also informally considered part of the signature.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 103/168


I Year I Sem Computational Thinking and Programming

Components of a Function Signature


• Function Name: This is the identifier by which the function is called. The name,
combined with the parameter list, uniquely identifies the function within its scope.
• Parameter List: This includes the types and, optionally, the names of the parameters
that the function accepts. The order, number, and types of parameters are crucial as
they determine how a function is called and what arguments it expects.

Example 1:
int add(int num1, int num2);
Signature: add(int, int)
The signature consists of the function name add and the parameter list (int, int), indicating
that this function takes two integers as input.
In this context, the return type (int) is not considered part of the signature, though informally
it's often included when discussing the function's prototype.

Example 2:
int multiply(int x, int y);
Signature: multiply(int, int)
Identifies a function multiply with two int parameters.

Example 3:
float calculateArea(float radius);
Signature: calculateArea(float)
Identifies a function calculateArea with a single float parameter.

Passing Parameters to Functions


Parameters are variables used to pass data into functions. In C, parameters are passed by
value, meaning a copy of the data is passed.

Example 1:
void printAge(int age)
{
printf("Age is: %d\n", age);
}
age is a parameter passed to printAge.
Example 2:
int add(int num1, int num2)
{
return num1 + num2;
}
num1 and num2 are parameters passed to add.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 104/168


I Year I Sem Computational Thinking and Programming

Passing Arrays to Functions


In C programming, arrays are often passed to functions to perform specific operations like
searching, sorting, or modifying array elements.
Unlike individual variables, arrays are passed by reference to functions. This means the
function works directly on the original array, and any modifications made within the function
are reflected in the original array.
When an array is passed to a function, only the address of the first element (base address) is
actually passed.
The function receiving the array will not create a new copy of the array elements. Instead, it
will use the same memory location of the actual array.
Arrays can be passed to functions by specifying the array name without brackets.
Syntax:
void processArray(int arr[], int size);
Inside the function, array elements can be accessed and manipulated using their indexes,
just like in the main part of the program.

Example 1: Calculating the Sum of Array Elements


void sumArray(int arr[], int size)
{
int sum = 0;
for (int i = 0; i < size; i++)
{
sum += arr[i];
}
printf("Sum of array elements: %d\n", sum);
}
This function calculates the sum of the elements in an integer array.
• Function Signature
o void sumArray(int arr[], int size): This defines a function named sumArray that
takes two parameters:
o int arr[]: An integer array. In C, when an array is passed to a function, what's
actually passed is a pointer to the first element of the array. The [] notation
signifies that arr is expected to be an array (or a pointer to the first element of
an array).
o int size: The size of the array. This is necessary because the size of an array is
not known just from the pointer, so it must be explicitly provided.
• Variable Initialization
o int sum = 0;: Initializes a variable sum to store the cumulative total of the
array elements.
o Looping Through the Array
o for (int i = 0; i < size; i++) { ... }: A for loop iterates over each element of the
array. The loop starts with i = 0 and continues until i is less than size,

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 105/168


I Year I Sem Computational Thinking and Programming

incrementing i in each iteration. This ensures that every element of the array
is accessed.
• Calculating the Sum
o sum += arr[i];: Inside the loop, each element of the array (arr[i]) is added to
sum. This statement is equivalent to sum = sum + arr[i]; and adds the value
of the current array element to the running total stored in sum.
• Printing the Result
o printf("Sum of array elements: %d\n", sum);: After the loop completes, the total
sum of the array elements is printed to the console. %d is a format specifier
for integers, and sum is the variable holding the calculated total.

Example 2:
void printArray(int arr[], int size)
{
for(int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
arr is an array passed to printArray.

Example 3:
int findMax(int nums[], int n)
{
int max = nums[0];
for(int i = 1; i < n; i++)
{
if(nums[i] > max)
{
max = nums[i];
}
}
return max;
}
nums is an array passed to findMax.

Example 4: Modifying Array Elements


void doubleArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
arr[i] *= 2;

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 106/168


I Year I Sem Computational Thinking and Programming

}
}
This function doubles each element in the given array. Modifications made to the array
elements will be reflected in the original array.
Recursion
Recursion in programming refers to a technique where a function calls itself directly or
indirectly to solve a problem. It's a method of solving complex problems by breaking them
down into simpler sub-problems of the same type. Recursion is particularly useful in
situations where the solution can be naturally expressed in terms of smaller instances of the
same problem.
Characteristics of Recursion
• Base Case: Every recursive function must have a base case, which is a condition that
stops the recursion. Without a base case, recursion would continue indefinitely,
leading to a stack overflow.
• Recursive Case: This is where the function calls itself with a simpler or smaller
argument.
• Simplicity and Elegance: Recursive solutions are often more straightforward and
elegant than their iterative counterparts for certain types of problems.
• Memory Utilization: Each recursive call uses some amount of memory (stack space),
so it's important to ensure that the recursion does not go too deep.

Example 1: Calculating Factorial


The factorial of a number n (denoted as n!) is the product of all positive integers less than or
equal to n. Factorial is a classic example of a problem that can be solved using recursion.
int factorial(int n)
{
if (n == 0) return 1; // Base case
return n * factorial(n - 1); // Recursive case
}
Base Case: If n is 0, the function returns 1. This is because the factorial of 0 is defined as 1.
Recursive Case: The function calls itself with n - 1.
Each call reduces the problem size until it reaches the base case.

Example 2: Fibonacci Series


int fibonacci(int n)
{
if (n <= 1) return n; // Base case
return fibonacci(n - 1) + fibonacci(n - 2); // Recursive case
}
Base Case: For n <= 1, the function simply returns n. This covers the first two numbers of
the Fibonacci series, 0 and 1.
Recursive Case: The function calls itself twice, once with n - 1 and once with n - 2.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 107/168


I Year I Sem Computational Thinking and Programming

This recursive approach calculates the Fibonacci number by summing the two preceding
Fibonacci numbers, aligning with the definition of the Fibonacci series.

Standard Problems
Factorial of a Given Number
Factorial of a Given Number Without Recursion
#include <stdio.h>

// Iterative function to calculate factorial


long factorial(int n) {
long fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}

int main() {
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);

if (num < 0) {
printf("Factorial of a negative number doesn't exist.\n");
} else {
printf("Factorial of %d = %ld\n", num, factorial(num));
}

return 0;
}
Output:
Enter a positive integer: 5
Factorial of 5 = 120

Explanation:
Function Signature:
• long factorial(int n): Declares a function named factorial that takes an integer n as
input and returns a long integer.

Variable Initialization:
• long fact = 1;: Initializes a long integer variable fact to store the factorial. It's initialized
to 1 because the factorial is calculated as a product.

Iterative Loop:

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 108/168


I Year I Sem Computational Thinking and Programming

• for (int i = 1; i <= n; i++) { ... }: A for loop that iterates from 1 to n.
• Within the loop, fact *= i; updates fact by multiplying it with the current loop index i.
This operation calculates the factorial by multiplying all integers from 1 up to n.

Return Value:
• return fact;: After completing the loop, the function returns the calculated factorial.

Reading User Input:


• scanf("%d", &num);: Reads an integer from the user and stores it in num.

Handling Negative Input:


• if (num < 0) { ... }: Checks if the input number is negative. Factorials are not defined
for negative numbers, so it prints an error message if num is negative.

Calculating and Printing the Factorial:


• printf("Factorial of %d = %ld\n", num, factorial(num));: If num is not negative, it calls
the factorial function with num as an argument and prints the result. The %ld format
specifier is used for printing long integers.

Program Exit:
• return 0;: The main function returns 0, indicating successful execution.

Factorial of a Given Number With Recursion


#include <stdio.h>

// Recursive function to calculate factorial


long factorial(int n) {
if (n == 0) return 1; // Base case
return n * factorial(n - 1); // Recursive case
}

int main() {
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);

if (num < 0) {
printf("Factorial of a negative number doesn't exist.\n");
} else {
printf("Factorial of %d = %ld\n", num, factorial(num));
}

return 0;
}
Output:

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 109/168


I Year I Sem Computational Thinking and Programming

Enter a positive integer: 5


Factorial of 5 = 120

Explanation:
Function Signature:
• long factorial(int n): Declares a function named factorial that takes an integer n as
input and returns a long integer. The function is designed to compute the factorial of
n.

Base Case:
• if (n == 0) return 1;: The base case of the recursion. When n is 0, the function returns
1. This is because the factorial of 0 is defined as 1. The base case is essential to stop
the recursion from continuing indefinitely.

Recursive Case:
• return n * factorial(n - 1);: The recursive case where the function calls itself. Here, the
function multiplies n with the factorial of n - 1. Each recursive call reduces n by 1,
gradually approaching the base case.

Reading User Input:


• scanf("%d", &num);: Reads an integer from the user and stores it in num.

Handling Negative Input:


• if (num < 0) { ... }: Checks if the input number is negative. Factorials are not defined
for negative numbers, so it prints an error message if num is negative.

Calculating and Printing the Factorial:


• printf("Factorial of %d = %ld\n", num, factorial(num));: If num is not negative, it calls
the factorial function with num as an argument and prints the result. The %ld format
specifier is used for printing long integers.

Program Exit:
• return 0;: The main function returns 0, indicating successful execution.

Sum of Natural Numbers using Recursion


Sum of Natural Numbers using Recursion Without Recursion
#include <stdio.h>

// Iterative function to calculate the sum of natural numbers


int sumOfNaturalNumbers(int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
return sum;
}

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 110/168


I Year I Sem Computational Thinking and Programming

int main() {
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);

if (num < 0) {
printf("Sum of natural numbers for a negative number doesn't make sense.\n");
} else {
printf("Sum of the first %d natural numbers is: %d\n", num,
sumOfNaturalNumbers(num));
}

return 0;
}
Output:
Enter a positive integer: 5
Sum of the first 5 natural numbers is: 15

Explanation:
Function Signature:
int sumOfNaturalNumbers(int n): Declares a function named sumOfNaturalNumbers that
takes an integer n as input and returns an integer. The function is designed to compute the
sum of the first n natural numbers.
Variable Initialization:
• int sum = 0;: Initializes an integer variable sum to store the cumulative total of natural
numbers. It starts from 0 because it will accumulate the sum in the loop.

Iterative Loop:
• for (int i = 1; i <= n; i++) { ... }: A for loop iterates from 1 to n. The loop index i represents
each natural number from 1 up to n.
• Within the loop, sum += i; adds the value of i to sum in each iteration. This operation
computes the cumulative sum of natural numbers.

Return Value:
• return sum;: After completing the loop, the function returns the total sum of the
natural numbers from 1 to n.

Reading User Input:


• scanf("%d", &num);: Reads an integer from the user and stores it in num.

Handling Negative Input:


• if (num < 0) { ... }: Checks if the input number is negative. Since the sum of natural
numbers is not defined for negative numbers, it prints an error message if num is
negative.

Calculating and Printing the Sum:

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 111/168


I Year I Sem Computational Thinking and Programming

• printf("Sum of the first %d natural numbers is: %d\n", num,


sumOfNaturalNumbers(num));: If num is not negative, it calls the
sumOfNaturalNumbers function with num as an argument and prints the result. The
%d format specifier is used for printing integers.

Program Exit:
• return 0;: The main function returns 0, indicating successful execution.

Sum of Natural Numbers using Recursion With Recursion


#include <stdio.h>
// Recursive function to calculate the sum of natural numbers
int sumOfNaturalNumbers(int n) {
if (n == 0) return 0; // Base case
return n + sumOfNaturalNumbers(n - 1); // Recursive case
}
int main() {
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
if (num < 0) {
printf("Sum of natural numbers for a negative number doesn't make sense.\n");
} else {
printf("Sum of the first %d natural numbers is: %d\n", num,
sumOfNaturalNumbers(num));
}

return 0;
}
Output:
Enter a positive integer: 5
Sum of the first 5 natural numbers is: 15

Explanation:
Function Signature:
• int sumOfNaturalNumbers(int n): Declares a function named sumOfNaturalNumbers
that takes an integer n as input and returns an integer. The function is designed to
compute the sum of the first n natural numbers using recursion.

Base Case:
• if (n == 0) return 0;: The base case of the recursion. When n is 0, the function returns
0. This is because the sum of natural numbers up to 0 is 0. The base case is essential
to stop the recursion from continuing indefinitely.

Recursive Case:

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 112/168


I Year I Sem Computational Thinking and Programming

• return n + sumOfNaturalNumbers(n - 1);: The recursive case where the function calls
itself. Here, the function returns the sum of the current number n and the result of
sumOfNaturalNumbers(n - 1). Each recursive call reduces n by 1, gradually
approaching the base case.

Reading User Input:


• scanf("%d", &num);: Reads an integer from the user and stores it in num.

Handling Negative Input:


• if (num < 0) { ... }: Checks if the input number is negative. Since the sum of natural
numbers is not defined for negative numbers, it prints an error message if num is
negative.

Calculating and Printing the Sum:


• printf("Sum of the first %d natural numbers is: %d\n", num,
sumOfNaturalNumbers(num));: If num is not negative, it calls the
sumOfNaturalNumbers function with num as an argument and prints the result.

Program Exit:
• return 0;: The main function returns 0, indicating successful execution.

External
• Storage Duration: External variables have static storage duration. They exist
throughout the program's execution.
• Scope: They have file scope, meaning they can be accessed across multiple functions
within the same source file.
• Declaration: External variables are declared outside of any function

Example:
// External variable declared outside any function
int globalVar = 10;
void function1()
{
// Access globalVar
printf("globalVar inside function1: %d\n", globalVar);
}
void function2()
{
// Access globalVar
printf("globalVar inside function2: %d\n", globalVar);
}

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 113/168


I Year I Sem Computational Thinking and Programming

Explanation:
• An external variable globalVar is declared and initialized outside of any function. This
variable has global scope, which means it can be accessed from any function within
the same source file. Its initial value is set to 10.
• The code defines two functions: function1 and function2.
• Inside function1, the globalVar variable is accessed using its name. This is possible
because globalVar has global scope and can be seen and used from within any
function in the same source file.
• printf is used to print the value of globalVar inside function1, resulting in the output:
"globalVar inside function1: 10". The value is retrieved from the global variable.
• Similarly, inside function2, the globalVar variable is accessed and printed using
printf. The output in this case is: "globalVar inside function2: 10".
• Both function1 and function2 are independent of each other, but they can access and
modify the same globalVar variable since it has global scope.

Auto
• Storage Duration: Auto variables have automatic storage duration. They are created
when a function is called and destroyed when the function exits.
• Scope: They have block scope, meaning they are accessible only within the block or
function where they are declared.
• Declaration: Variables declared inside a function without any storage class specifier
are auto by default.

Example:
void function()
{
// Auto variable
int localVar = 5;
printf("localVar: %d\n", localVar);
}
Explanation:
• The code defines a function named function().
• Inside the function(), a variable named localVar is declared and initialized with the
value 5. This variable is an auto variable by default, as it is declared without any
storage class specifier. Auto variables have automatic storage duration, meaning they
are created when the function is called and destroyed when the function exits.
• The printf function is used to print the value of localVar using the format specifier
%d. This line of code displays the value of localVar on the console.
• The function function() is now defined and ready to be called.
• When this function is called, it will execute the code within its body. In this case, it
will print the value of localVar, which is 5, to the console.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 114/168


I Year I Sem Computational Thinking and Programming

• After printing the value, the function will exit, and the auto variable localVar will be
automatically destroyed because it has automatic storage duration. It will no longer
exist in memory after the function exits.

Local
• Storage Duration: Local variables have automatic storage duration, just like auto
variables.
• Scope: They have block scope, similar to auto variables.
• Declaration: Local variables are declared inside a block or function.

Example:
void function()
{
// Local variable
int localVar = 5;
printf("localVar: %d\n", localVar);
}
Explanation:
• The code defines a function named function(). This function doesn't take any
parameters.
• Inside the function, a variable named localVar is declared and initialized with the
value 5. This variable is a local variable because it is declared within the function's
scope. Local variables have block scope, meaning they are only accessible within the
block (in this case, the function) where they are declared.
• The printf function is used to print the value of localVar to the console. The format
specifier %d is used to format and print an integer.
• The printf statement will print the value of localVar, which is 5, to the console when
the function() is called.
• This function definition is now complete and can be called from other parts of the
program.
• When the function() is called, it will execute its code. It will print the value of localVar
(which is 5) to the console and then complete its execution.
• After the function completes its execution, the local variable localVar goes out of scope
and is automatically destroyed. It does not persist beyond the execution of the
function.

Static
• Storage Duration: Static variables have static storage duration. They are created once
when the program starts and persist until the program ends.
• Scope: They have block scope like auto and local variables but retain their values
between function calls.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 115/168


I Year I Sem Computational Thinking and Programming

• Declaration: Variables declared with the static keyword inside a function become
static variables.

Example:
void function()
{
// Static variable
static int staticVar = 0;
staticVar++;
printf("staticVar: %d\n", staticVar);
}
int main()
{
function(); // Output: staticVar: 1
function(); // Output: staticVar: 2
return 0;
}
Explanation:
• The code defines a function named function().
• Inside the function(), there is a static variable named staticVar declared and initialized
to 0. The static keyword is used to declare this variable as static. Static variables have
static storage duration, which means they are created once when the program starts
and retain their values between function calls. In this case, staticVar is initialized to
0 when the program starts.
• The code then increments the staticVar by 1 using the staticVar++ statement. This
means that each time the function() is called, staticVar will retain its value from the
previous call and increment it by 1.
• The printf function is used to print the current value of staticVar to the console using
the format specifier %d.
• The main() function is defined, which serves as the program's entry point.
• Inside main(), the function() is called for the first time using function();.
• When function() is called for the first time, it increments staticVar from 0 to 1 and
prints "staticVar: 1" to the console.
• Then, function() is called for the second time using function(); from main(). This time,
staticVar is incremented from 1 to 2, and "staticVar: 2" is printed.
• Finally, the main() function returns 0, indicating successful program execution.

Register Variables
• Storage Duration: Register variables have automatic storage duration, like auto
variables.
• Scope: They have block scope, similar to auto and local variables.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 116/168


I Year I Sem Computational Thinking and Programming

• Declaration: Variables declared with the register keyword inside a function are
register variables. The keyword suggests that the variable should be stored in a CPU
register for faster access.

Example:
void function()
{
// Register variable
register int regVar = 10;
printf("regVar: %d\n", regVar);
}
Explanation:
• In the function, a static variable staticVar is declared and initialized to 0. It has static
storage duration.
• The variable is incremented by 1, and its value is printed. Since it's static, it retains
its value between function calls.
• In main, the function is called twice.
• The first call increments staticVar to 1 and prints it.
• The second call increments staticVar to 2 and prints it. The variable's value persists
between calls.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 117/168


I Year I Sem Computational Thinking and Programming

Unit IV
Topics to be Discussed:
Pointers: Pointer, Pointer to Pointer, Pointer and Functions (Call by Reference) Arguments,
Pointers and Arrays, Address Arithmetic, Characters and Pointers, Dynamic Memory
Allocation Methods.
Strings: Declaring, Initializing, String Input and Output Functions, String Handling
Functions.

Pointers
Why Shall We Study Pointers / Why Pointers are Necessary to Study After Functions?
• Memory Management: Pointers provide direct access to memory and help in efficient
memory management.
• Performance Optimization: They allow manipulation of data directly in memory,
which can optimize program performance.
• Dynamic Memory Allocation: Pointers are essential for dynamic memory allocation,
which is crucial for creating flexible data structures like linked lists, trees, and graphs.
• Function Arguments: Understanding pointers is critical for passing data to functions
more efficiently, especially when dealing with large structures or arrays.

Definition:
A pointer in C is a variable that stores the memory address of another variable. This allows
direct access and manipulation of the value stored at that memory location.

Mathematical Foundations:
Pointers are based on the concept of indirect referencing. Mathematically, if x is a variable
and &x is its address, a pointer p can store &x.
Then, *p (dereferencing) gives access to the value of x.

Syntax:
Declaration: datatype *pointer_name;
Initialization: pointer_name = &variable;
Dereferencing: *pointer_name; to access the value at the pointed location.
Example 1: Simple Example Demonstrating the Use of Pointers
#include <stdio.h>
int main() {
int var = 10; // A variable
int *ptr; // A pointer variable declaration
ptr = &var; // Store address of var in pointer variable

printf("Value of var = %d\n", var);


printf("Address of var = %p\n", &var);
printf("Value of ptr = %p\n", ptr);
printf("Value at *ptr = %d\n", *ptr);

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 118/168


I Year I Sem Computational Thinking and Programming

return 0;
}
Output:
Value of var = 10
Address of var = 0x7fffb077b624
Value of ptr = 0x7fffb077b624
Value at *ptr = 10

Explanation:
• Include Directive:
#include <stdio.h>: This line includes the standard input-output library in the
program, which is necessary for using the printf function.
• Function Declaration:
int main() {: This is the start of the main function, which is the entry point of every C
program. The program execution starts here.
• Variable Declaration and Initialization:
int var = 10;: A variable var of type int (integer) is declared and initialized with the
value 10.
• Pointer Declaration:
int *ptr;: This line declares a pointer variable ptr that is intended to point to an integer.
At this point, ptr doesn't have a defined value.
• Assigning Address to Pointer:
ptr = &var;: The address of var is assigned to ptr. The operator & is used to get the
address of var. Now, ptr contains the memory address of var.
• Printing Value of Variable:
printf("Value of var = %d\n", var);: This prints the value of var, which is 10.
• Printing Address of Variable:
printf("Address of var = %p\n", &var);: This prints the memory address of var. %p is
used as the format specifier for an address.
• Printing Value of Pointer:
printf("Value of ptr = %p\n", ptr);: This prints the value stored in ptr, which is the
address of var. Notice this should match the address printed in the previous step.
• Printing Value at the Address Pointed by Pointer:
• printf("Value at *ptr = %d\n", *ptr);: Dereferencing the pointer ptr with *ptr gives the
value at the memory address stored in ptr, which is the value of var. This line prints
10, the value of var.

Example 2: Using Pointers with Arrays


#include <stdio.h>
int main() {
int arr[3] = {10, 20, 30};
int *ptr = arr; // Pointer to the first element of the array

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 119/168


I Year I Sem Computational Thinking and Programming

for(int i = 0; i < 3; i++) {


printf("Value at arr[%d] = %d\n", i, *ptr);
ptr++; // Moving the pointer to the next array element
}

return 0;
}
Output:
Value at arr[0] = 10
Value at arr[1] = 20
Value at arr[2] = 30

Explanation
• Declaration and Initialization: An array arr of integers is declared and initialized
with three values. A pointer ptr is declared and initialized to point to the first element
of the array (arr[0]).
• Looping through Array: A for loop is used to iterate through the array elements.
• Accessing Elements: Within the loop, *ptr dereferences the current value pointed to
by ptr (initially arr[0]), which is then printed.
• Pointer Increment: After each iteration, ptr++ increments the pointer so that it points
to the next array element.

Example 3: Pointers and Functions (Call by Reference)


#include <stdio.h>
void swap(int *x, int *y)
{
int temp;
temp = *x; // Save the value at address x
*x = *y; // Put y into x
*y = temp; // Put x into y
}

int main()
{
int a = 100, b = 200;
printf("Before swap, a = %d and b = %d\n", a, b);
swap(&a, &b);
printf("After swap, a = %d and b = %d\n", a, b);
return 0;
}
Output:
Before swap, a = 100 and b = 200
After swap, a = 200 and b = 100

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 120/168


I Year I Sem Computational Thinking and Programming

Explanation:
• Function swap: A function swap is defined to swap the values of two integers. It takes
two pointers as arguments.
• Pointer Dereferencing in Function: Inside the swap function, *x and *y are used to
access and swap the values of the variables pointed by x and y.
• Main Function: Two integers a and b are declared and initialized. Before and after
calling swap, their values are printed to show the effect of the swap.
• Calling swap: swap is called with the addresses of a and b (&a and &b). This is known
as call by reference, where the function can modify the actual values of the arguments.
• Result: The values of a and b are swapped as a result of the swap function call,
demonstrating how pointers can be used to alter function arguments directly.

Value of Operator in Pointers


The value of operator in pointers, denoted by *, is used for dereferencing. Dereferencing a
pointer means accessing the value at the memory address stored in the pointer.
Syntax: *pointer_variable
Example:
#include <stdio.h>
int main() {
int num = 50;
int *ptr = &num;

printf("Value of num: %d\n", num);


printf("Value at pointer ptr: %d\n", *ptr);

return 0;
}
Output:
Value of num: 50
Value at pointer ptr: 50

Explanation:
• Variable Declaration: int num = 50; initializes an integer variable num with the value
50.
• Pointer Initialization: int *ptr = &num; declares a pointer ptr and initializes it with
the address of num.
• Dereferencing Pointer: printf("Value at pointer ptr: %d\n", *ptr); prints the value at
the address stored in ptr. Dereferencing ptr with *ptr yields the value of num, which
is 50.

Pointer Assignment
Pointer assignment involves assigning the address of a variable to a pointer.
Syntax: pointer_variable = &variable;
After assignment, the pointer points to the address of the assigned variable.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 121/168


I Year I Sem Computational Thinking and Programming

Example
#include <stdio.h>
int main() {
int a = 10, b = 20;
int *ptr;
ptr = &a; // Pointer points to a
printf("Value pointed to by ptr: %d\n", *ptr);
ptr = &b; // Now pointer points to b
printf("Value pointed to by ptr: %d\n", *ptr);
return 0;
}
Output:
Value pointed to by ptr: 10
Value pointed to by ptr: 20

Explanation
• Variable Declaration: Two integers a and b are declared and initialized.
• Pointer Declaration: A pointer ptr is declared.
• Assigning Pointer to a: ptr = &a; assigns the address of a to ptr. *ptr now equals 10.
• Printing Value Pointed by ptr: The value at the address stored in ptr is printed,
showing 10.
• Reassigning Pointer to b: ptr = &b; changes the address stored in ptr to that of b.
*ptr now equals 20.
• Printing New Value Pointed by ptr: The new value at the address stored in ptr is
printed, showing 20.

Program to Find the Minimum and Maximum Element in an Array


Program
#include <stdio.h>
void findMinMax(int arr[], int len, int *min, int *max)
{
*min = *max = arr[0];
for (int i = 1; i < len; i++)
{
if (arr[i] < *min)
{
*min = arr[i];
}
if (arr[i] > *max)
{
*max = arr[i];
}
}
}

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 122/168


I Year I Sem Computational Thinking and Programming

int main()
{
int arr[] = {5, 2, 8, 1, 3};
int min, max;

findMinMax(arr, 5, &min, &max);

printf("Minimum element = %d\n", min);


printf("Maximum element = %d\n", max);

return 0;
}
Output:
Minimum element = 1
Maximum element = 8

Explanation:
• Function Declaration: The findMinMax function takes an array, its length, and
pointers to store minimum and maximum values.
• Initializing Min and Max: *min = *max = arr[0]; initializes both min and max to the
first element of the array.
• Looping Through Array: The loop iterates over the array elements starting from the
second element.
• Updating Min and Max: Inside the loop, *min and *max are updated if a smaller or
larger element, respectively, is found.
• Main Function: The main function defines an array, calls findMinMax, and prints the
minimum and maximum elements.
• Output: The program prints the smallest and largest elements in the array.

Returning Pointers
Purpose of Returning Pointers
• Access to Data Structures: Functions can return pointers to complex data structures
(e.g., structs, arrays) that would be inefficient to return by value.
• Dynamic Memory Allocation: Often used with functions that allocate memory
dynamically (using malloc, calloc, etc.) so that the allocated memory can be accessed
outside the function.

Syntax
The function declaration specifies that it returns a pointer, typically datatype*
function_name().
Example: int* getArray() { ... }
Important Considerations

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 123/168


I Year I Sem Computational Thinking and Programming

• Lifetime of Data: Ensure the data being pointed to remains valid after the function
returns. Do not return pointers to local (automatic) variables declared within the
function.
• Memory Management: If the pointer refers to dynamically allocated memory, the
calling function is usually responsible for freeing that memory when it's no longer
needed to avoid memory leaks.
• Null Pointer Checks: The calling code should check if the returned pointer is NULL,
especially if the function uses dynamic memory allocation.

Example
#include <stdio.h>
#include <stdlib.h>
int* createArray(int size)
{
int *arr = malloc(size * sizeof(int));
if (arr != NULL) {
// Initialize array
for (int i = 0; i < size; i++) {
arr[i] = i;
}
}
return arr;
}
int main()
{
int *myArray = createArray(5);
if (myArray != NULL) {
// Use the array
for (int i = 0; i < 5; i++) {
printf("%d ", myArray[i]);
}
free(myArray); // Free the allocated memory
} else {
printf("Memory allocation failed.");
}
return 0;
}
Output:
01234
Explanation
• Function createArray: This function dynamically allocates memory for an array and
returns a pointer to the array.
• Memory Allocation: malloc is used to allocate memory. It's crucial to check if malloc
successfully allocated memory by verifying arr is not NULL.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 124/168


I Year I Sem Computational Thinking and Programming

• Initializing Array: The array elements are initialized within the function.
• Returning Pointer: The function returns a pointer to the allocated array.
• In main Function: The returned pointer is received in myArray. A check ensures it's
not NULL before using it.
• Freeing Memory: It's crucial to free the allocated memory using free(myArray) to
avoid memory leaks.

Pointer to Pointer
A pointer to a pointer is a form of multiple indirection or a chain of pointers. It is essentially
a pointer that holds the address of another pointer.
Definition
A pointer to a pointer is a variable that stores the address of another pointer variable.
Syntax
datatype **pointer_to_pointer;
Usage
Used in dynamic memory allocation.
Helpful in complex data structures like trees, graphs, and linked lists.
Common in handling arrays of strings.
Example
#include <stdio.h>
int main()
{
int var = 300;
int *ptr;
int **pptr;

ptr = &var; // Assign address of var to ptr


pptr = &ptr; // Assign address of ptr to pptr

printf("Value of var = %d\n", var);


printf("Value available at *ptr = %d\n", *ptr);
printf("Value available at **pptr = %d\n", **pptr);

return 0;
}
Output:
Value of var = 300
Value available at *ptr = 300
Value available at **pptr = 300

Explanation
• Variable Declaration: int var = 300; declares an integer variable var and initializes it
with the value 300.
• Pointer Declaration: int *ptr; declares a pointer ptr to an integer.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 125/168


I Year I Sem Computational Thinking and Programming

• Pointer to Pointer Declaration: int **pptr; declares a pointer to a pointer pptr.


• Assigning Address to Pointer: ptr = &var; makes ptr point to the address of var.
• Assigning Address to Pointer to Pointer: pptr = &ptr; assigns the address of ptr to
pptr. Now, pptr points to ptr.
• Accessing Value of var via ptr: *ptr dereferences ptr to get the value of var, which is
300.
• Accessing Value of var via pptr: **pptr involves two levels of dereferencing. First,
*pptr gets the value in pptr (which is the address of ptr), and then **pptr dereferences
that address to get the value at ptr, which is the value of var (300).
• Printing Values: The program prints the value of var, the value obtained by
dereferencing ptr, and the value obtained by double dereferencing pptr. All these
should output 300, demonstrating how the data is accessed through multiple levels
of indirection.

Pointer and Functions (Call by Reference) Arguments


It is Possible to pass variables to functions by value or by reference. When you pass by
reference, you are essentially passing a pointer to the variable, allowing the function to modify
the original value. This is often referred to as "call by reference."

Passing by Value vs. Passing by Reference


• Pass by Value: A copy of the variable's value is passed to the function. Modifications
within the function do not affect the original variable.
• Pass by Reference: A pointer to the variable's memory location is passed to the
function. Modifications within the function affect the original variable.

Syntax for Call by Reference


To pass a variable by reference, you need to pass a pointer to the variable. The function
parameter should be a pointer type.
Example: void modifyValue(int *ptr) { ... }

Allows functions to modify the original variable's value.


More memory-efficient than pass by value for large data structures.

Example 1: Modify Integer Value


Program
#include <stdio.h>
void modifyValue(int *ptr)
{
(*ptr)++; // Increment the value at the address pointed by ptr
}
int main()
{

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 126/168


I Year I Sem Computational Thinking and Programming

int num = 5;
modifyValue(&num); // Pass num by reference
printf("Modified value: %d\n", num); // Outputs 6
return 0;
}
Output:
Modified value: 6

Explanation
• A variable num is declared and initialized with the value 5.
• The modifyValue function takes a pointer as a parameter, which is used to pass num
by reference.
• Inside modifyValue, the value pointed to by ptr (i.e., num) is incremented.
• In the main function, modifyValue is called with &num to pass a pointer to num.
• After the function call, the modified value of num is printed, which is 6.

Example 2: Swap Two Integers


Program
#include <stdio.h>
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
int main()
{
int a = 10, b = 20;
swap(&a, &b); // Pass a and b by reference
printf("a after swap: %d\n", a); // Outputs 20
printf("b after swap: %d\n", b); // Outputs 10
return 0;
}
Output:
a after swap: 20
b after swap: 10

Explanation
• Two integer variables a and b are declared and initialized with values 10 and 20,
respectively.
• The swap function takes two pointers as parameters, allowing it to swap the values of
the variables they point to.
• Inside swap, the values pointed to by x and y are swapped using a temporary variable.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 127/168


I Year I Sem Computational Thinking and Programming

• In the main function, swap is called with &a and &b to pass both a and b by reference.
• After the function call, the values of a and b are printed, demonstrating that they have
been swapped.

Pointers and Arrays


Array Name as a Pointer
An array name, when used in an expression, is treated as a pointer to the first element of the
array.
Example: int arr[5]; int *ptr = arr; - arr and ptr both point to the first element of arr.

Pointer arithmetic Can be Used to access other elements of the array.


Example: ptr[i] is equivalent to arr[i], where i is an index.

Advantages
Efficient array manipulation using pointers.
Allows for dynamic memory allocation using malloc and arrays of unknown size using pointer
arithmetic.
Example 1: Traverse and Print Array
Program
#include <stdio.h>
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr; // arr is a pointer to the first element
for (int i = 0; i < 5; i++)
{
printf("%d ", *ptr); // Print the value at the pointer
ptr++; // Move the pointer to the next element
}
return 0;
}
Output:
12345
Explanation
• An integer array arr is declared and initialized with values 1 to 5.
• A pointer ptr is declared and initialized with arr. This makes ptr point to the first
element of the array.
• A for loop is used to iterate through the elements of the array.
• Inside the loop, *ptr is used to access and print the value pointed to by ptr.
• ptr++ is used to increment the pointer, effectively moving it to the next element of the
array.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 128/168


I Year I Sem Computational Thinking and Programming

Example 2: Sum of Array Elements


Program
#include <stdio.h>
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr;
int sum = 0;
for (int i = 0; i < 5; i++)
{
sum += *ptr;
ptr++;
}
printf("Sum of array elements: %d\n", sum);
return 0;
}
Output:
Sum of array elements: 15

Explanation
• An integer array arr is declared and initialized with values 1 to 5.
• A pointer ptr is declared and initialized with arr, making it point to the first element.
• An integer variable sum is initialized to 0 to store the sum of array elements.
• A for loop is used to iterate through the elements of the array.
• Inside the loop, *ptr is used to access and add the value pointed to by ptr to the sum.
• ptr++ is used to increment the pointer, moving it to the next element.
• After the loop, the sum of array elements is printed.

Address Arithmetic
Pointer address arithmetic involves performing arithmetic operations on pointers, such as
addition and subtraction, to navigate and manipulate memory addresses. It's a powerful
feature in C that allows for efficient traversal of arrays, data structures, and memory
allocation.
Pointer Arithmetic Operations
• Addition: Adding an integer to a pointer increments the pointer's address by the
product of the integer and the size of the pointed-to data type.
• Subtraction: Subtracting an integer from a pointer decrements the pointer's address
by the product of the integer and the size of the pointed-to data type.

Syntax
• ptr + n: Moves n elements forward.
• ptr - n: Moves n elements backward.
• ptr1 - ptr2: Computes the difference in addresses between two pointers.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 129/168


I Year I Sem Computational Thinking and Programming

Advantages
Efficient traversal of arrays and data structures.
Useful for dynamic memory allocation, pointer arithmetic, and pointer-based data
manipulation.
Example 1: Pointer Arithmetic – Addition
Pointer arithmetic addition involves adding an integer value to a pointer, which increments
the pointer's address by the product of the integer and the size of the pointed-to data type.
Program
#include <stdio.h>
int main()
{
int arr[] = {10, 20, 30, 40, 50};
int *ptr = arr; // Pointer points to the first element

// Use pointer arithmetic to access elements


printf("Element at index 0: %d\n", *ptr);

// Move the pointer two elements forward


ptr = ptr + 2;

// Use pointer arithmetic to access the new element


printf("Element at index 2: %d\n", *ptr);

return 0;
}
Output:
Element at index 0: 10
Element at index 2: 30

Explanation
• An integer array arr is declared and initialized with values 10 to 50.
• A pointer ptr is declared and initialized with arr, making it point to the first element
of the array.
• The program prints the value at the first index (index 0) using *ptr, which is 10.
• Pointer arithmetic is used to move the pointer two elements forward. ptr = ptr + 2;
increments the pointer's address by 2 * sizeof(int) bytes, effectively pointing to the
element at index 2.
• The program prints the value at index 2 using *ptr, which is 30.

Example 2: Pointer Arithmetic – Subtraction


Pointer arithmetic subtraction involves subtracting an integer value from a pointer, which
decrements the pointer's address by the product of the integer and the size of the pointed-to
data type.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 130/168


I Year I Sem Computational Thinking and Programming

Program
#include <stdio.h>
int main()
{
int arr[] = {10, 20, 30, 40, 50};
int *ptr = &arr[4]; // Pointer points to the last element

// Use pointer arithmetic to access elements


printf("Element at index 4: %d\n", *ptr);

// Move the pointer two elements backward


ptr = ptr - 2;

// Use pointer arithmetic to access the new element


printf("Element at index 2: %d\n", *ptr);

return 0;
}
Output:
Element at index 4: 50
Element at index 2: 30

Explanation
• An integer array arr is declared and initialized with values 10 to 50.
• A pointer ptr is declared and initialized with the address of the last element of the
array (&arr[4]), making it point to the last element.
• The program prints the value at the last index (index 4) using *ptr, which is 50.
• Pointer arithmetic is used to move the pointer two elements backward. ptr = ptr - 2;
decrements the pointer's address by 2 * sizeof(int) bytes, effectively pointing to the
element at index 2.
• The program prints the value at index 2 using *ptr, which is 30.

Characters and Pointers


In C, characters and pointers are closely related, and pointers are often used to manipulate
and process strings (sequences of characters).
Characters as Pointers
• In C, characters are represented as individual bytes, and strings are arrays of
characters.
• A pointer to the first character of a string is often used to manipulate and process the
entire string.

Character Arrays
• Strings in C are typically represented as character arrays (e.g., char str[] = "Hello";).

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 131/168


I Year I Sem Computational Thinking and Programming

• The null-terminated character ('\0') marks the end of a string.

Pointer to Characters
• Pointers to characters (char *) are commonly used to access and modify individual
characters in a string.

Example 1: String Length


Program
#include <stdio.h>
int main()
{
char str[] = "Hello, World!";
char *ptr = str; // Pointer to the first character of the string
int length = 0;
// Calculate the length of the string using pointer arithmetic
while (*ptr != '\0')
{
length++;
ptr++; // Move to the next character
}
printf("Length of the string: %d\n", length);
return 0;
}
Output:
Length of the string: 13

Explanation
• A character array str is declared and initialized with the string "Hello, World!".
• A pointer ptr is declared and initialized with str, making it point to the first character
of the string.
• A variable length is initialized to 0 to store the length of the string.
• A while loop is used to iterate through the characters of the string.
• Inside the loop, *ptr is used to access the current character. If it's not '\0' (null
terminator), length is incremented.
• ptr++ is used to move the pointer to the next character in the string.
• After the loop, the length of the string is printed.

Example 2: String Copy


Program
#include <stdio.h>
void stringCopy(char *dest, const char *src)
{
while (*src != '\0')
{

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 132/168


I Year I Sem Computational Thinking and Programming

*dest = *src; // Copy the character


dest++; // Move to the next character in destination
src++; // Move to the next character in source
}
*dest = '\0'; // Add null terminator to the destination
}
int main()
{
char source[] = "Hello";
char destination[10]; // Initialize an empty character array

// Copy the source string to the destination using a function


stringCopy(destination, source);
printf("Copied string: %s\n", destination);
return 0;
}
Output:
Copied string: Hello

Explanation
• A character array source is declared and initialized with the string "Hello".
• An empty character array destination of size 10 is initialized.
• The stringCopy function is defined to copy one string to another.
• Inside the function, a while loop is used to iterate through the characters of the source
string.
• *dest = *src; copies the character from the source to the destination.
• dest++ and src++ are used to move to the next characters in both source and
destination.
• After copying all characters, *dest = '\0'; adds a null terminator to the destination,
ensuring it's a valid C string.
• In the main function, the stringCopy function is called to copy the source string to the
destination.
• The copied string is printed.

Dynamic Memory Allocation Methods


Dynamic memory allocation in C allows you to allocate and deallocate memory during
program execution. This is useful when you need to create data structures of variable size or
when you want to manage memory efficiently. Two common methods for dynamic memory
allocation in C are malloc and free.
malloc (Memory Allocation)
Syntax:
void *malloc(size_t size);
Syntax Explanation:

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 133/168


I Year I Sem Computational Thinking and Programming

size_t is an unsigned integer type representing the size of memory to allocate in bytes.
malloc returns a void pointer (void *) to the first byte of the allocated memory block.
It allocates a block of memory of the specified size and returns a pointer to the beginning of
that block.
free (Memory Deallocation)
Syntax:
void free(void *ptr);
Syntax Explanation:
ptr is a pointer to the memory block that was previously allocated using malloc.
free deallocates the memory block pointed to by ptr.
After calling free, the pointer ptr becomes invalid, and the memory can be reused for other
purposes.

Example 1: Allocate and Free Memory


Program
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *arr;
int size = 5;
// Allocate memory for an integer array
arr = (int *)malloc(size * sizeof(int));

if (arr == NULL)
{
printf("Memory allocation failed.\n");
return 1; // Exit with an error code
}
// Initialize the array
for (int i = 0; i < size; i++)
{
arr[i] = i * 2;
}
// Use the array
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
// Free the allocated memory
free(arr);
return 0;
}
Output:

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 134/168


I Year I Sem Computational Thinking and Programming

02468

Explanation
• An integer pointer arr is declared to store the address of dynamically allocated
memory.
• An integer size is set to 5, indicating the size of the dynamically allocated integer array.
• malloc is used to allocate memory for an integer array of size size. The cast (int *) is
used to convert the result of malloc to the appropriate pointer type.
• A check is performed to ensure that memory allocation was successful. If arr is NULL,
it means that memory allocation failed, and an error message is printed before exiting
the program with an error code.
• A for loop is used to initialize the elements of the array with even numbers.
• Another for loop is used to print the values of the array.
• After using the array, free is called to release the dynamically allocated memory to
avoid memory leaks.

Example 2: Allocate and Free Memory for Strings


Program
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *name;
// Allocate memory for a string
name = (char *)malloc(20 * sizeof(char));
if (name == NULL)
{
printf("Memory allocation failed.\n");
return 1; // Exit with an error code
}
// Initialize the string
strcpy(name, "John Doe");
// Print the string
printf("Name: %s\n", name);
// Free the allocated memory
free(name);
return 0;
}
Output:
Name: John Doe
Explanation
• A character pointer name is declared to store the address of dynamically allocated
memory for a string.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 135/168


I Year I Sem Computational Thinking and Programming

• malloc is used to allocate memory for a character array (string) of size 20. The cast
(char *) is used to convert the result of malloc to the appropriate pointer type.
• A check is performed to ensure that memory allocation was successful. If name is
NULL, it means that memory allocation failed, and an error message is printed before
exiting the program with an error code.
• The strcpy function is used to initialize the name string with the value "John Doe".
• The program prints the contents of the name string.
• After using the string, free is called to release the dynamically allocated memory to
avoid memory leaks.

Strings
Strings in C are arrays of characters terminated by a null character '\0'. They are used to
store text.

Declaring
Syntax:
char str[50];
This declares str as a string capable of holding up to 49 characters plus the null character.
Initializing
Initialization can be done at the time of declaration
char str[] = "Hello, World!";

or
by assigning values to the array:
char str[50];
str[0] = 'H';
str[1] = 'i';
str[2] = '\0'; // null character to terminate the string

String Input and Output Functions


gets and puts
• gets: Used to read a line from the standard input (keyboard) into a string.
• puts: Used to write a string to the standard output (screen).

Example:
char str[50];
• gets(str); // Reads a string from the user.
• puts(str); // Outputs the string.

scanf and printf


• scanf: Reads formatted input from the standard input.
• printf: Prints formatted output to the standard output.

Example:
char str[50];

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 136/168


I Year I Sem Computational Thinking and Programming

scanf("%s", str); // Reads a string from the user


printf("%s", str); // Prints the string
String Handling Functions
1. strcpy and strncpy
• strcpy: Copies a string to another.
• strncpy: Copies a specified number of characters from one string to another.

Example:
#include <stdio.h>
#include <string.h>
int main()
{
char source[] = "Hello, World!";
char destination[50];

strcpy(destination, source);

printf("Copied String: %s\n", destination);


return 0;
}
Output:
Copied String: Hello, World!

Explanation:
• source string is initialized with "Hello, World!".
• destination string is declared with enough space.
• strcpy function copies source to destination.
• The copied string is printed.

Example:
#include <stdio.h>
#include <string.h>
int main()
{
char source[] = "Hello, World!";
char destination[50];
strncpy(destination, source, 5);
destination[5] = '\0'; // Null-terminate the string

printf("Copied String: %s\n", destination);


return 0;
}
Output:
Copied String: Hello

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 137/168


I Year I Sem Computational Thinking and Programming

Explanation
• source string contains "Hello, World!".
• destination string is declared.
• strncpy function copies first 5 characters from source to destination.
• destination is null-terminated.
• The result "Hello" is printed.

2. strlen
• Returns the length of the string (excluding the null character).

Example:
#include <stdio.h>
#include <string.h>

int main() {
char str[] = "Hello, World!";
int length = strlen(str);

printf("Length of the string: %d\n", length);


return 0;
}
Output:
Length of the string: 13

Explanation:
• str is initialized with "Hello, World!".
• strlen calculates the length of str, excluding the null character.
• Length (13) is printed.

Example
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "C Programming";
int i;

for (i = 0; i < strlen(str); i++) {


printf("%c\n", str[i]);
}
return 0;
}
Output:
C

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 138/168


I Year I Sem Computational Thinking and Programming

P
r
o
g
r
a
m
m
i
n
g
Explanation
• str is initialized with "C Programming".
• A loop runs from 0 to the length of str.
• Each character of str is printed on a new line.

3. strcat and strncat


• strcat: Concatenates two strings.
• strncat: Concatenates a specified number of characters of one string to
another.

Example:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = "Hello, ";
char str2[] = "World!";

strcat(str1, str2);

printf("Concatenated String: %s\n", str1);


return 0;
}
Output:
Concatenated String: Hello, World!

Explanation:
• str1 and str2 are initialized.
• strcat concatenates str2 to str1.
• The concatenated string "Hello, World!" is printed.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 139/168


I Year I Sem Computational Thinking and Programming

Example:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = "Hello, ";
char str2[] = "World!";

strncat(str1, str2, 3);

printf("Concatenated String: %s\n", str1);


return 0;
}
Output:
Concatenated String: Hello, Wor

Explanation:
• str1 and str2 are initialized.
• strncat concatenates the first 3 characters of str2 to str1.
• The result "Hello, Wor" is printed.

4. strcmp and strncmp


• strcmp: Compares two strings.
• strncmp: Compares the first n characters of two strings.

Example:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "Hello";
char str2[] = "World";

int result = strcmp(str1, str2);

if (result == 0) {
printf("Strings are equal\n");
} else {
printf("Strings are not equal\n");
}
return 0;
}
Output:

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 140/168


I Year I Sem Computational Thinking and Programming

Strings are not equal

Explanation
• str1 and str2 are initialized.
• strcmp compares str1 and str2.
• The result indicates they are not

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 141/168


I Year I Sem Computational Thinking and Programming

Unit V
Topics to be Discussed:
Structures: Defining Structures, Initializing Structures, Structures and Functions, Array of
Structures.
File Management: Creating, Reading, Writing Text and Binary Files, Random Access Using
FSeek, FTell and Rewind Functions.

Structures
Structures in C are a way to group different types of variables under a single name. These
variables can be of different types and are known as members of the structure. Structures
are useful for representing complex data items that have multiple attributes.
In the C programming language, structures provide a way to group together variables of
different data types under a single name. A structure is a user-defined data type that allows
you to create complex and custom data structures to represent real-world entities or complex
data records.
Key Points:
• Grouping Data: Structures allow you to group together variables of various data types,
including integers, floating-point numbers, characters, and even other structures,
into a single unit.
• User-Defined: Unlike basic data types (int, float, char, etc.), structures are user-
defined data types. This means you can define your own structures to suit the specific
needs of your program.
• Members or Fields: Variables inside a structure are referred to as members or fields.
Each member can have a unique data type and a meaningful name.
• Organization: Structures are useful for organizing related data elements. For example,
you can use a structure to represent a student with fields like student ID, name, and
GPA.
• Modularity: Structures promote modularity in code. You can define a structure once
and then reuse it throughout your program, making your code more organized and
maintainable.
• Accessing Members: To access members of a structure, you use the dot (.) operator
followed by the member's name. For example, if you have a structure representing a
point with members x and y, you can access them as point.x and point.y.

Example:
// Define a structure for representing a point in 2D space
struct Point
{
int x; // x-coordinate
int y; // y-coordinate
};

int main()

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 142/168


I Year I Sem Computational Thinking and Programming

{
// Declare a structure variable
struct Point p1;
// Initialize structure members
p1.x = 3;
p1.y = 5;
// Access and use structure members
printf("Coordinates: (%d, %d)\n", p1.x, p1.y);
return 0;
}
In this example, we define a structure Point to represent 2D points with x and y coordinates.
We then declare a structure variable p1, initialize its members, and access them to print the
coordinates.
Output:
Coordinates: (3, 5)
Explanation:
Structure Definition:
// Define a structure for representing a point in 2D space
struct Point
{
int x; // x-coordinate
int y; // y-coordinate
};

In this step, a structure named Point is defined. This structure is used to represent a point
in 2D space and has two members: x (representing the x-coordinate) and y (representing the
y-coordinate).

Main Function:
int main() {

The main() function serves as the entry point of the program.

Structure Variable Declaration:


// Declare a structure variable
struct Point p1;

Inside the main() function, a structure variable p1 of type struct Point is declared. This
variable will be used to store the coordinates of a point.

Initializing Structure Members:


// Initialize structure members
p1.x = 3;

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 143/168


I Year I Sem Computational Thinking and Programming

p1.y = 5;

The x and y members of the p1 structure variable are initialized with values 3 and 5,
respectively. This sets the coordinates of the point represented by p1 to (3, 5).

Accessing and Printing Structure Members:


// Access and use structure members
printf("Coordinates: (%d, %d)\n", p1.x, p1.y);

The printf function is used to display the coordinates of the point stored in the p1 structure
variable. It prints "Coordinates: (3, 5)" to the console. This line demonstrates how to access
structure members using the dot (.) operator.

Return Statement and Program Termination:


return 0;
}

The main() function ends with a return 0; statement, indicating successful program
execution. The program then terminates, and the operating system typically receives a return
code of 0 to signify that the program ran without errors.

Why Shall We Study Structures / Why Structures Shall be Studied?


• Data Management: Structures help in organizing complex data in a more readable
and manageable format.
• Modularity: They allow for the grouping of related data, making code more modular
and easier to understand.
• Ease of Handling: Structures simplify the process of handling multiple data types,
especially when passing them to functions.

Defining Structures
A structure in C programming is a user-defined data type that allows grouping together
different data types under a single unit. This unit, or structure, can contain variables of
various data types, such as int, float, char, etc. These variables are referred to as "members"
or "fields" of the structure.
Syntax:
struct StructureName
{
dataType member1;
dataType member2;
...
};
Key Points:
• Keyword struct: This keyword is used to define a structure.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 144/168


I Year I Sem Computational Thinking and Programming

• StructureName: This is a label for the structure type and follows the same naming
conventions as variables in C.
• Members: Inside the structure, members are declared, each with its own data type.
• Semicolon: A semicolon (;) is required at the end of the structure definition.

Explanation:
• Custom Data Type: Structures create a new data type that can be used to declare
variables. Once a structure is defined, it can be used throughout the program to create
instances of that structure type.
• Grouping Data: Structures are particularly useful when there is a need to group
related data elements, which may be of different types, into a single logical unit. For
example, if a program needs to handle information about books, a structure can be
created with members like title, author, price, etc.
• Modularity and Readability: Using structures increases the modularity of the
program, making it easier to understand, maintain, and modify.
• Use Cases: Structures are widely used in various programming scenarios such as
data modeling, representing complex entities (like students, employees, etc.), and in
systems programming.

Example 1:
#include <stdio.h>
struct Student
{
char name[50];
int age;
float marks;
};
int main()
{
struct Student student1;
// Use student1...
return 0;
}
Explanation:
• struct Student is defined with members: name, age, and marks.
• An instance student1 of struct Student is declared.

Example 2:
#include <stdio.h>
struct Date
{
int day, month, year;
};
struct Student
{

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 145/168


I Year I Sem Computational Thinking and Programming

char name[50];
struct Date dob;
float marks;
};
int main()
{
struct Student student1;
// Use student1...
return 0;
}
Explanation:
• Two structures are defined: struct Date and struct Student.
• struct Student contains a nested structure struct Date.

Example 3:
struct Book
{
char title[50];
char author[50];
float price;
int pages;
};
int main()
{
struct Book book1;
// book1 can now hold information about a book
return 0;
}
In this example, a struct Book is defined with four members: title, author, price, and pages.
book1 is then declared as an instance of struct Book, ready to store information about a
specific book.
Explanation:
Defining the Structure
struct Book
{
char title[50];
char author[50];
float price;
int pages;
};
• struct Book { ... };:
o This line begins the definition of a structure named Book.
• Inside the curly braces { ... }, the members (or fields) of the structure are declared:

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 146/168


I Year I Sem Computational Thinking and Programming

o char title[50];: A character array named title, which can hold a string up to 49
characters long (plus the null terminator \0 that signifies the end of the string)
is declared. This is intended to store the title of the book.
o char author[50];: Similarly, this is a character array to store the author's
name, following the same logic as title.
o float price;: A float variable to store the price of the book.
o int pages;: An integer variable to store the number of pages in the book.
• The semicolon ; at the end of the structure definition is crucial as it marks the end of
the structure definition in C.

Declaring a Structure Variable


int main() {
struct Book book1;
// book1 can now hold information about a book
return 0;
}
• int main() { ... }:
o This is the main function of the C program, where the execution begins.
• struct Book book1;:
o Here, a variable book1 of the structure type Book is declared.
o This means book1 is an instance of the Book structure and can store
information about a book, including its title, author, price, and number of
pages.
• return 0;:
o This line indicates the successful completion of the program. In C, returning
0 from the main function signifies that the program executed successfully.

Additional Point:
For Example:
strcpy(book1.title, "C Programming Language");
strcpy(book1.author, "Brian W. Kernighan and Dennis M. Ritchie");
book1.price = 45.99;
book1.pages = 279;

After declaring book1, you can assign values to its members, as Stated in the Above Example.

Each member of book1 can be accessed using the dot operator ., as shown in the example
above.

Initializing Structures
Initialization of structures in C involves setting initial values to the members of the structure
at the time of its declaration. This can be done in several ways:

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 147/168


I Year I Sem Computational Thinking and Programming

• List Initialization: Assigning values to each member in the order they are declared
in the structure.
• Designated Initialization: (Available in C99 and later) Assigning values to specific
members using their names.
• Zero Initialization: Initializing all members to zero or null by default.

Example:
Let's consider a structure Book and demonstrate different ways of initializing it.
#include <stdio.h>
struct Book
{
char title[50];
char author[50];
float price;
int pages;
};

int main() {
// List Initialization
struct Book book1 = {"The C Programming Language", "Kernighan and Ritchie", 45.50,
279};

// Designated Initialization
struct Book book2 = {.title = "1984", .author = "George Orwell", .price = 9.99, .pages = 328};

// Print details of book1


printf("Book 1:\nTitle: %s\nAuthor: %s\nPrice: %.2f\nPages: %d\n\n", book1.title,
book1.author, book1.price, book1.pages);

// Print details of book2


printf("Book 2:\nTitle: %s\nAuthor: %s\nPrice: %.2f\nPages: %d\n", book2.title,
book2.author, book2.price, book2.pages);

return 0;
}

Output:
Book 1:
Title: The C Programming Language
Author: Kernighan and Ritchie
Price: 45.50
Pages: 279

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 148/168


I Year I Sem Computational Thinking and Programming

Book 2:
Title: 1984
Author: George Orwell
Price: 9.99
Pages: 328
Explanation:
Structure Definition:
A structure Book with members title, author, price, and pages is defined.
List Initialization (book1):
• book1 is initialized using list initialization. The values are assigned in the order the
members are defined in the structure.
• "The C Programming Language", "Kernighan and Ritchie", 45.50, and 279 are
assigned to title, author, price, and pages of book1, respectively.

Designated Initialization (book2):


• book2 is initialized using designated initialization, where each member is assigned a
value using its name.
• This method is more readable and does not depend on the order of members in the
structure definition.
• .title = "1984", .author = "George Orwell", .price = 9.99, and .pages = 328 explicitly
initialize each corresponding member of book2.

Printing Structure Details:


• The program then prints the details of both book1 and book2 using printf statements.
• It accesses each member of the structures using the dot operator.

This example illustrates how to initialize structures in C and how to access and use their
members. The initialization ensures that the structure variables book1 and book2 hold
specific data right from their declaration, making them ready for use in the program.

Structures and Functions


In C programming, initializing structures involves assigning initial values to the members of
a structure. Once initialized, these structures can be passed to functions. This allows for
more modular code and makes it easier to manipulate complex data types.
Structure Initialization
Initialization can be done using:
• Direct Assignment: Assign values directly to each member.
• Using an Initializer List: List values in the same order as members in the structure.

Using Structures with Functions


Functions can:
• Receive Structures as Arguments: Either by value (copy of the structure) or by pointer
(address of the structure).
• Return Structures: Functions can return a new structure or a modified one.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 149/168


I Year I Sem Computational Thinking and Programming

Example:
Consider a Book structure and a function that modifies and prints its contents.
#include <stdio.h>

struct Book {
char title[50];
char author[50];
float price;
int pages;
};

// Function to modify and print a book's details


void updateBook(struct Book *b, float newPrice) {
b->price = newPrice; // Updating the price of the book
printf("Updated Book:\nTitle: %s\nAuthor: %s\nPrice: %.2f\nPages: %d\n", b->title, b-
>author, b->price, b->pages);
}

int main() {
struct Book book1 = {"1984", "George Orwell", 15.99, 328};

printf("Original Book:\nTitle: %s\nAuthor: %s\nPrice: %.2f\nPages: %d\n\n", book1.title,


book1.author, book1.price, book1.pages);

// Updating the book's price


updateBook(&book1, 12.99);

return 0;
}

Output:
Original Book:
Title: 1984
Author: George Orwell
Price: 15.99
Pages: 328

Updated Book:
Title: 1984
Author: George Orwell
Price: 12.99
Pages: 328

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 150/168


I Year I Sem Computational Thinking and Programming

Explanation:
Structure Definition and Initialization:
• The Book structure is defined with members title, author, price, and pages.
• book1 is initialized with an initializer list. The values "1984", "George Orwell", 15.99,
and 328 are assigned to the respective members.

Function Definition - updateBook:


• updateBook is designed to modify the price of a book. It takes two arguments: a
pointer to struct Book and the new price (float).
• The book's price is updated using the pointer dereference operator ->.
• The function then prints the updated details of the book.

Function Call:
• In main, the function updateBook is called, passing the address of book1 and the new
price 12.99.
• The function modifies book1's price and prints the updated details.

This example demonstrates the interaction between structures and functions in C. The
structure is used to group related data, and the function manipulates this data, showcasing
a common pattern in structured programming. The use of a pointer to pass the structure to
the function allows for efficient memory usage and direct modification of the original
structure.
Array of Structures
An array of structures in C is a powerful way to store and manipulate a collection of
structured data. Initializing an array of structures involves setting initial values for each
structure in the array.
Methods of Initialization
• Using an Initializer List: Similar to initializing individual structures, but repeated
for each element in the array.
• Designated Initializers: (Available in C99 and later) Allows initializing specific
members in each structure of the array.
• Manual Initialization: Individually setting values for each member of each structure
in the array, often done inside a loop or a function.

Example:
Consider an example where an array of a Book structure is initialized and used.
#include <stdio.h>
struct Book
{
char title[50];
char author[50];
float price;
int pages;
};

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 151/168


I Year I Sem Computational Thinking and Programming

int main()
{
// Array of structures initialization
struct Book library[2] =
{
{"1984", "George Orwell", 9.99, 328},
{"The C Programming Language", "Kernighan and Ritchie", 45.50, 279}
};

// Printing details of all books in the library


for (int i = 0; i < 2; i++)
{
printf("Book %d:\nTitle: %s\nAuthor: %s\nPrice: %.2f\nPages: %d\n\n", i + 1,
library[i].title, library[i].author, library[i].price, library[i].pages);
}
return 0;
}
Output:
Book 1:
Title: 1984
Author: George Orwell
Price: 9.99
Pages: 328

Book 2:
Title: The C Programming Language
Author: Kernighan and Ritchie
Price: 45.50
Pages: 279
Explanation:
Structure Definition:
• The Book structure is defined with members title, author, price, and pages.

Initializing an Array of Structures:


• library is an array of two Book structures.
• Each element of the array is initialized with an initializer list, specifying values for
title, author, price, and pages.

Looping Through the Array:


• A for loop iterates over each element in the library array.
• Within the loop, the details of each book are printed using printf. The loop index i is
used to access each element of the array.

Accessing Structure Members:

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 152/168


I Year I Sem Computational Thinking and Programming

• The members of each Book structure in the array are accessed using the dot operator
. (e.g., library[i].title).

Case Study:
Create A Structure (to Store Marks of 4 Courses English, Mathematics, Chemistry and CTP,
Student ID). The Structure Shall Store Marks of N Students for Each Course. Marks can be
floating point values, Student ID Could be Both Numbers and Characters (for Example S1)

Using Different Functions to Read Marks, Find Minimum, Maximum and Average Marks for
Each Course, Display Minimum, Maximum and Average Marks for each course. While
Displaying Minimum and Maximum Marks Also Display the Student ID Obtained them.

Program:
#include <stdio.h>
#include <string.h>

#define MAX_STUDENTS 190


#define NUM_COURSES 4

// Structure to store marks and student ID


struct Student
{
char id[10];
float marks[NUM_COURSES]; // 0: English, 1: Mathematics, 2: Chemistry, 3: CTP
};

void readMarks(struct Student students[], int n)


{
for (int i = 0; i < n; i++)
{
printf("Enter ID for student %d: ", i + 1);
scanf("%s", students[i].id);
printf("Enter marks for English, Mathematics, Chemistry, CTP: ");
for (int j = 0; j < NUM_COURSES; j++)
{
scanf("%f", &students[i].marks[j]);
}
}
}

void findMinimum(struct Student students[], int n, float min[], char minId[][10])


{
for (int j = 0; j < NUM_COURSES; j++)
{

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 153/168


I Year I Sem Computational Thinking and Programming

min[j] = students[0].marks[j];
strcpy(minId[j], students[0].id);
for (int i = 1; i < n; i++)
{
if (students[i].marks[j] < min[j])
{
min[j] = students[i].marks[j];
strcpy(minId[j], students[i].id);
}
}
}
}

void findMaximum(struct Student students[], int n, float max[], char maxId[][10])


{
for (int j = 0; j < NUM_COURSES; j++)
{
max[j] = students[0].marks[j];
strcpy(maxId[j], students[0].id);
for (int i = 1; i < n; i++)
{
if (students[i].marks[j] > max[j])
{
max[j] = students[i].marks[j];
strcpy(maxId[j], students[i].id);
}
}
}
}

void calculateAverage(struct Student students[], int n, float avg[])


{
for (int j = 0; j < NUM_COURSES; j++)
{
float sum = 0;
for (int i = 0; i < n; i++)
{
sum += students[i].marks[j];
}
avg[j] = sum / n;
}
}

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 154/168


I Year I Sem Computational Thinking and Programming

void displayResults(float min[], float max[], float avg[], char minId[][10], char maxId[][10])
{
const char *courses[] = {"English", "Mathematics", "Chemistry", "CTP"};
for (int i = 0; i < NUM_COURSES; i++)
{
printf("%s:\n", courses[i]);
printf("Minimum: %.2f (Student ID: %s)\n", min[i], minId[i]);
printf("Maximum: %.2f (Student ID: %s)\n", max[i], maxId[i]);
printf("Average: %.2f\n\n", avg[i]);
}
}

int main()
{
struct Student students[MAX_STUDENTS];
int n;

printf("Enter the number of students: ");


scanf("%d", &n);

readMarks(students, n);

float min[NUM_COURSES], max[NUM_COURSES], avg[NUM_COURSES];


char minId[NUM_COURSES][10], maxId[NUM_COURSES][10];

findMinimum(students, n, min, minId);


findMaximum(students, n, max, maxId);
calculateAverage(students, n, avg);

displayResults(min, max, avg, minId, maxId);

return 0;
}
Output:
Enter the number of students: 2
Enter ID for student 1: S1
Enter marks for English, Mathematics, Chemistry, CTP: 15 16 7 9
Enter ID for student 2: S2
Enter marks for English, Mathematics, Chemistry, CTP: 14 18 6 7
English:
Minimum: 14.00 (Student ID: S2)
Maximum: 15.00 (Student ID: S1)
Average: 14.50

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 155/168


I Year I Sem Computational Thinking and Programming

Mathematics:
Minimum: 16.00 (Student ID: S1)
Maximum: 18.00 (Student ID: S2)
Average: 17.00

Chemistry:
Minimum: 6.00 (Student ID: S2)
Maximum: 7.00 (Student ID: S1)
Average: 6.50

CTP:
Minimum: 7.00 (Student ID: S2)
Maximum: 9.00 (Student ID: S1)
Average: 8.00
Explanation:
Structure Definition and Constants
#define MAX_STUDENTS 100
#define NUM_COURSES 4

struct Student {
char id[10];
float marks[NUM_COURSES]; // 0: English, 1: Mathematics, 2: Chemistry, 3: CTP
};
• #define MAX_STUDENTS 100: This line defines a preprocessor macro
MAX_STUDENTS with a value of 100, which sets the maximum number of students
that can be handled by the program.
• #define NUM_COURSES 4: Similar to MAX_STUDENTS, this line defines
NUM_COURSES as 4, indicating the number of courses (English, Mathematics,
Chemistry, and CTP).
• struct Student { ... };: This defines a structure Student with two members: id (an array
of 10 chars for storing the student ID) and marks (an array of 4 floats to store the
marks for each of the four courses).

Function readMarks()
void readMarks(struct Student students[], int n)
{
for (int i = 0; i < n; i++)
{
printf("Enter ID for student %d: ", i + 1);
scanf("%s", students[i].id);
printf("Enter marks for English, Mathematics, Chemistry, CTP: ");
for (int j = 0; j < NUM_COURSES; j++)
{

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 156/168


I Year I Sem Computational Thinking and Programming

scanf("%f", &students[i].marks[j]);
}
}
}
• This function reads the marks and IDs for n students.
• for (int i = 0; i < n; i++): Loops through each student.
• scanf("%s", students[i].id);: Reads the student ID.
• for (int j = 0; j < NUM_COURSES; j++): Nested loop to read marks for each course.

Function findMinimum()
void findMinimum(struct Student students[], int n, float min[], char minId[][10])
{
// ... (similar logic as findMaximum)
}
• Finds the minimum marks for each course.
• min[] and minId[] are used to store the minimum marks and the corresponding
student IDs.

Function findMaximum()
void findMaximum(struct Student students[], int n, float max[], char maxId[][10])
{
// ... (similar logic as findMinimum)
}
• Finds the maximum marks for each course.
• max[] and maxId[] are used to store the maximum marks and the corresponding
student IDs.

Function calculateAverage()
void calculateAverage(struct Student students[], int n, float avg[])
{
// ...
}
• Calculates the average marks for each course.
• avg[] is used to store the calculated averages.

Function displayResults()
void displayResults(float min[], float max[], float avg[], char minId[][10], char maxId[][10])
{
// ...
}
• Displays the minimum, maximum, and average marks for each course, along with the
student IDs of those who scored the minimum and maximum marks.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 157/168


I Year I Sem Computational Thinking and Programming

Main Function()
int main() {
struct Student students[MAX_STUDENTS];
int n;

printf("Enter the number of students: ");


scanf("%d", &n);

readMarks(students, n);

float min[NUM_COURSES], max[NUM_COURSES], avg[NUM_COURSES];


char minId[NUM_COURSES][10], maxId[NUM_COURSES][10];

findMinimum(students, n, min, minId);


findMaximum(students, n, max, maxId);
calculateAverage(students, n, avg);

displayResults(min, max, avg, minId, maxId);

return 0;
}
• struct Student students[MAX_STUDENTS];: Declares an array students of Student
structures to store data for up to MAX_STUDENTS.
• int n;: Declares an integer n to store the actual number of students.
• scanf("%d", &n);: Reads the number of students.
• readMarks(students, n);: Calls readMarks to input student IDs and marks.
• The arrays min, max, avg, minId, and maxId are declared for storing the results.
• findMinimum, findMaximum, and calculateAverage functions are called to process the
data.
• displayResults is called to display the outcomes.
• return 0;: Indicates successful program termination.

Each function in the program is dedicated to handling a specific part of the process, such as
reading data, finding minimum or maximum marks, calculating averages, or displaying
results. This modular approach enhances the readability and maintainability of the code.

File – Definition
A file in computing is a collection of data or information that is identified by a name and
stored on a storage device, like a hard disk. In programming, files are critical for storing data
permanently, enabling information to persist beyond the runtime of the program.
A file is a container in a computer system for storing data. Files can store a wide range of
data, from written text to binary information like images or executable programs. In

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 158/168


I Year I Sem Computational Thinking and Programming

programming, files are essential for data persistence, enabling data to be stored permanently,
as opposed to volatile memory storage, which is lost when a program terminates or the
computer is turned off.

Introduction to Files
• Persistence: Files provide a way to store data persistently. Files allow programs
written in C to store data permanently, as opposed to variables and arrays which are
temporary and are lost when the program ends.
• Data Access: They enable both sequential and random access to data.
• Types of Files: Typically, files are categorized as text files (human-readable) and
binary files (computer-readable). Mainly, there are two types of files – text files
(readable by humans and computers) and binary files (primarily readable by
computers).

File Operations in C Programming


• Opening a File: Establishes a connection between the file and the program.
• Reading from a File: Involves accessing data from the file.
• Writing to a File: Involves adding or modifying data in the file.
• Closing a File: Ends the connection between the file and the program, ensuring that
all data is properly saved.

File Management

Creating
Use of fopen: The fopen function is used to create and open files. If the file does not exist, it
can create a new file, depending on the mode specified.
Function Used: fopen()
Modes: "w" for writing (creates a new file), "wb" for writing in binary mode.

Example 1: Creating a Text File


#include <stdio.h>
int main()
{
FILE *file = fopen("example.txt", "w");
if (file == NULL)
{
printf("Error opening file.\n");
return 1;
}
fprintf(file, "Hello, World!\n");
fclose(file);
return 0;
}

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 159/168


I Year I Sem Computational Thinking and Programming

Explanation:
Opening a File for Writing
FILE *file = fopen("example.txt", "w");
This line attempts to open (or create if it doesn't exist) a file named example.txt in write mode
("w").
fopen is used for this task. If successful, it returns a pointer to a FILE object, which is stored
in the variable file.

Checking for File Opening Error


if (file == NULL)
{
printf("Error opening file.\n");
return 1;
}
The program checks whether the file was successfully opened. If file is NULL, it indicates an
error occurred in opening the file, possibly because of insufficient permissions or other issues.
An error message is printed, and the program returns 1, signaling an unsuccessful operation.

Writing to the File


fprintf(file, "Hello, World!\n");
This line writes the string "Hello, World!\n" to the opened file.
fprintf functions similarly to printf but directs the output to a file instead of standard output.

Closing the File


fclose(file);
After writing, the file is closed using fclose. This step is crucial as it ensures that all data is
properly written to the file and the associated resources are released.
Example 2: Creating a Binary File
#include <stdio.h>
int main()
{
FILE *file = fopen("example.bin", "wb");
if (file == NULL)
{
printf("Error opening file.\n");
return 1;
}
int numbers[] = {1, 2, 3, 4, 5};
fwrite(numbers, sizeof(int), 5, file);
fclose(file);
return 0;
}

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 160/168


I Year I Sem Computational Thinking and Programming

Output:
Explanation:
Opening a File for Writing in Binary Mode
FILE *file = fopen("example.bin", "wb");
The program begins by trying to open (or create if it doesn't exist) a file named example.bin
in binary write mode ("wb").
fopen is used for this purpose, and if successful, it returns a pointer to a FILE object, which
is assigned to the variable file.

Checking for File Opening Error


if (file == NULL)
{
printf("Error opening file.\n");
return 1;
}
This condition checks if the file was successfully opened. If file is NULL, it indicates an error
in opening the file. This could be due to reasons like lack of permissions or an issue with the
file system.
It prints an error message and exits the program with a return code 1, indicating failure.

Writing an Array of Integers to the File


int numbers[] = {1, 2, 3, 4, 5};
fwrite(numbers, sizeof(int), 5, file);
An integer array numbers is initialized with the values {1, 2, 3, 4, 5}.
fwrite is then used to write this array to the open binary file.
The parameters of fwrite specify what to write (numbers), the size of each element to write
(sizeof(int)), the total number of elements to write (5, the length of the array), and the file
pointer (file).

Closing the File


fclose(file);
The file is closed using fclose. This is an important step as it ensures that all data is properly
flushed to the file and that system resources associated with the file are released.

Reading
Reading Methods: Methods include fscanf, fgets, and direct character reading using fgetc.
Function Used: fscanf() for formatted reading, fgets() for string reading, fread() for binary
reading.
Example 1: Reading a Text File
#include <stdio.h>
int main()
{
FILE *file = fopen("example.txt", "r");

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 161/168


I Year I Sem Computational Thinking and Programming

char buffer[100];
if (file == NULL)
{
printf("Error opening file.\n");
return 1;
}
while (fgets(buffer, 100, file) != NULL)
{
printf("%s", buffer);
}
fclose(file);
return 0;
}
Output:
Explanation:
Opening the File
FILE *file = fopen("example.txt", "r");
This line attempts to open a file named example.txt in read mode ("r"). The fopen function is
used for this purpose.
If successful, fopen returns a pointer to a FILE object, which is stored in the variable file.

Error Check for File Opening


if (file == NULL)
{
printf("Error opening file.\n");
return 1;
}
This conditional checks if the file was successfully opened. If file is NULL, it indicates an error
in opening the file, possibly because it doesn't exist or due to a lack of required permissions.
An error message is printed, and the program returns 1, indicating an unsuccessful
operation.

Reading and Printing the File Content


while (fgets(buffer, 100, file) != NULL)
{
printf("%s", buffer);
}
• The while loop uses the fgets function to read from the file.
• fgets reads up to one less than the number of characters specified by 100 from the
file and stores them in buffer. It stops reading either after 99 characters, when it
encounters a newline character, or at the end of the file, whichever comes first.
• Each string read into buffer is then printed to the standard output using printf.
• The loop continues until fgets returns NULL, indicating the end of the file.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 162/168


I Year I Sem Computational Thinking and Programming

Closing the File


fclose(file);
• After completing the reading process, the file is closed using fclose. This step is
important for resource management and to ensure that any buffered data is properly
handled.

Reading and Printing the File Content


Example 2: Reading a Binary File
#include <stdio.h>
int main()
{
FILE *file = fopen("example.bin", "rb");
int number;
if (file == NULL)
{
printf("Error opening file.\n");
return 1;
}
while (fread(&number, sizeof(int), 1, file))
{
printf("%d\n", number);
}
fclose(file);
return 0;
}
Output:
Explanation:
Opening the File
FILE *file = fopen("example.bin", "rb");
• The program starts by attempting to open a file named example.bin in binary read
mode ("rb"). The fopen function is used for this purpose.
• If the file is successfully opened, fopen returns a pointer to a FILE object; this pointer
is stored in the variable file.

Checking for File Opening Error


if (file == NULL)
{
printf("Error opening file.\n");
return 1;
}
The program checks if the file was successfully opened by verifying if file is NULL. If file is
NULL, it means the file could not be opened (possibly because it does not exist or there are
insufficient permissions), and an error message is printed.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 163/168


I Year I Sem Computational Thinking and Programming

The program then returns 1, indicating an error has occurred.

Reading from the File


while (fread(&number, sizeof(int), 1, file))
{
printf("%d\n", number);
}
• The while loop uses fread to read from the file. fread is designed to read blocks of data
from a binary file.
• &number is the memory address where the read data will be stored.
• sizeof(int) specifies the size of each element to be read (in this case, the size of an
integer).
• The third argument, 1, indicates the number of elements to read in each call to fread.
• The loop continues as long as fread successfully reads an element (in this case, an
integer).
• Each read integer is stored in number and then printed to the standard output.

Closing the File


fclose(file);
• After finishing the reading process, the file is closed using fclose. This is important for
freeing up resources and ensuring that all buffered data is properly written to the file.

Writing Text and Binary Files


Text Files: Writing to text files can be done using fprintf or fputs.
Binary Files: Binary file writing involves the fwrite function.
Function Used: fprintf() for formatted writing, fputs() for string writing, fwrite() for binary
writing.

Example 1:
#include <stdio.h>
int main()
{
FILE *file = fopen("textfile.txt", "w");
if (file == NULL) {
printf("Error opening file.\n");
return 1;
}

fprintf(file, "Hello, World!\n");


fprintf(file, "This is a sample text file.\n");

fclose(file);

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 164/168


I Year I Sem Computational Thinking and Programming

printf("Text file written successfully.\n");


return 0;
}

Explanation:
• Open the File for Writing:
o FILE *file = fopen("textfile.txt", "w");: Opens (or creates) textfile.txt in write
mode. If the file already exists, it's truncated to zero length.
• Error Handling:
o if (file == NULL) { ... }: Checks if the file was opened successfully. If not (file ==
NULL), it prints an error message and exits with a return code of 1.
• Writing to the File:
o fprintf(file, "Hello, World!\n"); and the next fprintf: Writes two lines of text into
the file.
• Closing the File:
o fclose(file);: Closes the file, which is important to ensure that the data is
properly saved.
• Success Message:
o printf("Text file written successfully.\n");: Prints a message to the console
indicating successful file writing.
• Program Exit:
o return 0;: Indicates that the program has executed successfully.

Example 2:
#include <stdio.h>
int main()
{
int numbers[] = {1, 2, 3, 4, 5};
FILE *file = fopen("binaryfile.bin", "wb");
if (file == NULL) {
printf("Error opening file.\n");
return 1;
}

fwrite(numbers, sizeof(numbers), 1, file);

fclose(file);

printf("Binary file written successfully.\n");


return 0;
}
Output:
Explanation:
• Open the File for Binary Writing:

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 165/168


I Year I Sem Computational Thinking and Programming

o FILE *file = fopen("binaryfile.bin", "wb");: Opens (or creates) binaryfile.bin in


binary write mode.
• Error Handling:
o Similar to the first program, it checks if the file was successfully opened and
handles the error if not.
• Writing to the Binary File:
o fwrite(numbers, sizeof(numbers), 1, file);: Writes the array numbers to the file.
sizeof(numbers) calculates the total size of the numbers array.
• Closing the File:
o Same as the first program, it closes the file after writing.
• Success Message:
o Indicates successful writing to the binary file.
• Program Exit:
o Again, return 0; indicates successful program execution.

Random Access Using FSeek, FTell and Rewind Functions


Functions Used:
• fseek(): Move the file pointer to a specific location.
• ftell(): Tell the current position of the file pointer.
• rewind(): Reset the file pointer to the beginning of the file.

Example 1: Using fseek and ftell


#include <stdio.h>
int main()
{
FILE *file = fopen("example.txt", "r+");
if (file == NULL)
{
printf("Error opening file.\n");
return 1;
}
fseek(file, 0, SEEK_END);
long size = ftell(file);
printf("Size of file: %ld bytes\n", size);
fclose(file);
return 0;
}
Output:
Size of file: 1024 bytes
Explanation:
Open a File

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 166/168


I Year I Sem Computational Thinking and Programming

FILE *file = fopen("example.txt", "r+");


• This line attempts to open a file named example.txt in read-plus mode ("r+"). The read-
plus mode enables both reading and writing to the file.
• fopen returns a pointer to FILE. This pointer is stored in file.

Check for File Opening Error


if (file == NULL)
{
printf("Error opening file.\n");
return 1;
}
• If fopen fails to open the file (e.g., if the file doesn't exist), it returns NULL. This
condition checks if the file was opened successfully.
• If the file isn't opened, it prints an error message and returns 1, indicating an error.

Move File Pointer to the End


fseek(file, 0, SEEK_END);
• fseek is used to move the file pointer associated with file.
• It is set to move 0 bytes from the position SEEK_END, which is the end of the file.
This effectively places the file pointer at the very end of the file.

Get the Current Position in the File


long size = ftell(file);
• ftell returns the current position of the file pointer.
• Since the file pointer is at the end of the file, ftell gives the total number of bytes from
the beginning to the end of the file, which is the size of the file in bytes.

Print the Size of the File


printf("Size of file: %ld bytes\n", size);
• This line prints the size of the file (in bytes) obtained from ftell.

Close the File


fclose(file);
• The file is closed using fclose, which is good practice to release resources.

Example 2: Using rewind


#include <stdio.h>
int main()
{
FILE *file = fopen("example.txt", "r+");
fseek(file, 10, SEEK_SET); // Move 10 bytes from start

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 167/168


I Year I Sem Computational Thinking and Programming

rewind(file); // Rewind to the beginning


char ch = fgetc(file);
printf("First character: %c\n", ch);
fclose(file);
return 0;
}
Output:
First character: H

Explanation:
Step 1: Open the File
FILE *file = fopen("example.txt", "r+");

• fopen: This function opens a file. "example.txt" is the name of the file, and "r+" is the
mode. Here, "r+" means the file is opened for reading and writing.
• file: This is a pointer to FILE, a structure that stores information about the file.

Step 2: Move the File Pointer


fseek(file, 10, SEEK_SET);
• fseek: It moves the file pointer to a specified location.
• 10, SEEK_SET: Moves the file pointer 10 bytes from the beginning of the file.
SEEK_SET is the start of the file.

Step 3: Rewind the File Pointer


rewind(file);
• rewind: This function moves the file pointer back to the beginning of the file. It's
equivalent to fseek(file, 0, SEEK_SET).

Step 4: Read the First Character


char ch = fgetc(file);
• fgetc: Reads the next character from the file. Since the file pointer was just rewound,
it reads the first character of the file.

Step 5: Print the Character and Close the File


printf("First character: %c\n", ch);
fclose(file);
• printf: Prints the character read from the file.
• fclose: Closes the file, which is good practice to free up resources.

Department of CSE(Artificial Intelligence and Machine Learning) Page No: 168/168

You might also like