Lesson Plans of Computer Science 10th Class
1.
Lesson Plan: Reserved Words in C
Grade: 10th
Duration: 40 minutes
1.
Objectives
By the end of this lesson, students
will be able to:
- Define reserved words in C programming.
- Identify the purpose and significance of reserved
words.
- List examples of reserved words in C and understand
their usage.
- Differentiate between reserved words and user-defined
identifiers.
2.
Materials Required
- Computer or projector for presentation.
- Handouts/slides listing all reserved words in C.
- C compiler (e.g., Code::Blocks or Turbo C) for
practical demonstration.
- Whiteboard and markers.
3.
Teaching Methodology
A. Introduction (5 minutes)
- Begin with a question: "Have you ever
encountered a sign that says 'No Entry'? What happens if you try to go
in?"
- Explain that in programming, certain words (reserved
words) are like "No Entry" signs—they have predefined meanings
and cannot be used for anything else.
B. Theoretical Explanation (15
minutes)
- Definition of Reserved Words:
- Reserved words are predefined words in a programming
language with specific meanings.
- They are also known as keywords.
- Reserved words cannot be used as identifiers (e.g.,
variable names, function names).
- Purpose of Reserved Words:
- Serve as instructions or commands for the compiler.
- Help define the syntax and structure of the
programming language.
- Examples of Reserved Words in C:
- List some common reserved words categorized by their
use:
|
Category |
Examples |
|
Data Types |
int, float, char |
|
Control Statements |
if, else, switch |
|
Loops |
for, while, do |
|
Storage Classes |
static, extern, auto |
|
Others |
return, void, sizeof |
|
|
|
- Key Characteristics of Reserved Words:
- They are case-sensitive (e.g., int is
valid, but Int is not).
- Cannot be redefined or modified by the user.
- Difference Between Reserved Words and Identifiers:
|
Reserved
Words |
Identifiers |
|
Predefined and fixed in C |
User-defined names |
|
Cannot be used for variables |
Can be used for variables,
functions, etc. |
|
Examples: if, return |
Examples: age, sum, data |
C. Practical Demonstration (15
minutes)
- Step 1: Using Reserved Words in a Program
- Open a C compiler (e.g., Code::Blocks) and type the
following program:
o
#include
<stdio.h>
o
o
int
main() {
o
int age = 18; // 'int' is a reserved word
o
printf("My age is %d", age);
o
return 0; // 'return' is a reserved word
o
}
- Compile and run the program, showing the output.
- Step 2: Using a Reserved Word as an Identifier
(Intentional Error)
- Modify the program to:
o
#include
<stdio.h>
o
o
int
main() {
o
int return = 10; // Invalid use of a
reserved word
o
printf("Return value is %d",
return);
o
return 0;
o
}
- Compile the program to demonstrate the error message.
- Step 3: Correcting the Error
- Replace return with a valid identifier (e.g., value).
D. Conclusion (5 minutes)
- Recap the importance of reserved words in defining the
rules of a programming language.
- Reinforce the difference between reserved words and
identifiers.
- Emphasize the need to avoid using reserved words as
variable names.
4.
Assessment
Oral Questions:
- What are reserved words, and why are they important?
- Can you use int as a variable name? Why or why not?
- Name three reserved words used for control flow in C.
Activity:
- Provide students with a list of reserved words. Ask
them to identify which can be used as variable names and which cannot.
Homework:
- Write a short program that uses at least five reserved
words correctly.
- List 10 reserved words and explain their purpose.
2.
Lesson Plan: Compiler
Grade: 10th
Duration: 40 minutes
1.
Objectives
By the end of this lesson, students
will be able to:
- Define what a compiler is and understand its purpose in
programming.
- Explain the difference between a compiler and an
interpreter.
- Understand how the compilation process converts source
code into machine code.
- Demonstrate the use of a compiler to run a simple C
program.
2.
Materials Required
- Computer or projector for presentation.
- Installed C compiler (e.g., GCC, Turbo C, or
Code::Blocks).
- Handouts/slides summarizing the compilation process.
- Whiteboard and markers.
3.
Teaching Methodology
A. Introduction (5 minutes)
- Begin with a question: "When you give
instructions to a person who speaks a different language, what do you
need?"
- Relate this to programming: A compiler is like a translator
that converts human-readable code (source code) into machine language
(binary code) that a computer understands.
B. Theoretical Explanation (15
minutes)
- What is a Compiler?
- A software tool that translates high-level programming
code into machine code (binary).
- Purpose of a Compiler:
- Makes programs executable by converting them into a
format that computers can run.
- Compilation Process:
- Source Code:
The code written by the programmer.
- Lexical Analysis: Breaks the code into tokens.
- Syntax Analysis: Checks if the code follows language grammar rules.
- Semantic Analysis: Ensures the code has logical meaning.
- Code Generation: Produces machine code.
- Optimization:
Improves efficiency of the generated code.
- Difference Between Compiler and Interpreter:
|
Feature |
Compiler |
Interpreter |
|
Translation |
Converts entire code at once |
Converts code line by line |
|
Execution Speed |
Faster (once compiled) |
Slower |
|
Output File |
Produces an executable file |
Does not produce an executable
file |
- Examples of Compilers:
- GCC, Turbo C, Code::Blocks for C/C++.
- javac for Java.
C. Practical Demonstration (15
minutes)
- Step 1: Write a Simple Program
- Open a C compiler (e.g., Code::Blocks or Turbo C).
- Type the following program:
o
#include
<stdio.h>
o
int
main() {
o
printf("Hello, Compiler!");
o
return 0;
o
}
- Step 2: Compile the Program
- Save the file and click "Build" or
"Compile."
- Explain what happens during compilation (conversion to
machine code).
- Step 3: Execute the Program
- Run the compiled program to display the output.
- Point out how the process would fail if there were
syntax errors.
- Highlight Error Detection:
- Introduce an intentional error (e.g., remove a
semicolon) and recompile to show how the compiler provides error
messages.
D. Conclusion (5 minutes)
- Summarize the role of the compiler in the programming
process.
- Highlight the importance of understanding error
messages during compilation.
- Encourage students to practice compiling and running programs.
3.
Assessment
Oral Questions:
- What is the main purpose of a compiler?
- What is the difference between a compiler and an
interpreter?
- What happens if your code has syntax errors during
compilation?
Activity:
- Ask students to type and compile a program to display
their name and age.
·
#include
<stdio.h>
·
int
main() {
·
printf("My name is [Your Name]. I am
[Your Age] years old.");
·
return 0;
·
}
Homework:
- Research the benefits of code optimization during
compilation.
- Write a paragraph comparing GCC and Turbo C as
compilers.
3. Lesson Plan: Integrated Development
Environment (IDE)
Grade: 10th
Duration: 40 minutes
1.
Objectives
By the end of this lesson, students
will be able to:
- Define an Integrated Development Environment (IDE).
- Identify the main features and components of an IDE.
- Understand the purpose and benefits of using an IDE for
programming.
- Demonstrate basic usage of an IDE (e.g., Code::Blocks,
Turbo C).
2.
Materials Required
- Computer or projector for presentation.
- IDE installed on computers (e.g., Code::Blocks,
Dev-C++, Turbo C).
- Handouts/slides explaining the features of an IDE.
- Whiteboard and markers.
3.
Teaching Methodology
A. Introduction (5 minutes)
- Begin with a real-world analogy: "Imagine
trying to build a house with scattered tools versus using a toolbox where
everything is organized."
- Relate this to programming: An IDE is the "toolbox"
for programmers that integrates all necessary tools.
B. Theoretical Explanation (15
minutes)
- Definition of an IDE:
- An application that provides all the tools needed for
software development in one place.
- Key Components of an IDE:
- Editor:
Where code is written.
- Compiler/Interpreter: Converts source code into machine code.
- Debugger:
Helps identify and fix errors in the code.
- Build Automation Tools: Simplify the process of compiling and running code.
- Features of an IDE:
- Syntax highlighting.
- Auto-completion of code.
- Real-time error detection.
- Project management tools.
- Examples of Popular IDEs:
- For C/C++: Code::Blocks, Dev-C++, Turbo C.
- For other languages: Visual Studio, PyCharm, Eclipse.
- Benefits of Using an IDE:
- Saves time and reduces errors.
- Streamlines the programming process.
- Encourages better project organization.
C. Practical Demonstration (15
minutes)
- Open an IDE (e.g., Code::Blocks) on a computer or
projector.
- Tour of the IDE Interface:
- Show the editor window, menu bar, toolbar, output
console, and project explorer.
- Write and Run a Simple Program:
- Type a basic "Hello, World!" program in C:
o
#include
<stdio.h>
o
int
main() {
o
printf("Hello, World!");
o
return 0;
o
}
- Compile and run the program using the IDE.
- Show how errors are displayed if there’s a syntax
issue.
- Highlight features like syntax highlighting and error
detection in real time.
D. Conclusion (5 minutes)
- Summarize the importance of an IDE in simplifying the
development process.
- Reinforce the practical benefits of using IDEs over
plain text editors.
- Encourage students to explore the IDE at home.
4.
Assessment
Oral Questions:
- What is an IDE, and why is it important for
programmers?
- Name three key components of an IDE.
- How does an IDE help in debugging code?
Activity:
- Ask students to open the IDE, create a new project, and
type the "Hello, World!" program themselves.
- Guide them to compile and run the program while
troubleshooting any errors.
Homework:
- Research two IDEs other than Code::Blocks and list
their key features.
4.
Lesson Plan: Structure of a C Program
Grade: 10th
Duration: 40 minutes
1.
Objectives
By the end of this lesson, students
will be able to:
- Identify and describe the basic structure of a C
program.
- Understand the purpose of different components, such as
preprocessor directives, the main() function, and statements.
- Write and execute a simple C program following its
standard structure.
2.
Materials Required
- Computer or projector for presentation.
- C compiler (e.g., Code::Blocks, Turbo C, or GCC).
- Handouts or slides summarizing the structure of a C
program.
- Whiteboard and markers.
3.
Teaching Methodology
A. Introduction (5 minutes)
- Begin by asking: "Have you ever followed a
recipe to cook something?"
- Relate this to programming, where every program follows
a specific structure, like a recipe.
- Briefly explain the significance of having a consistent
structure in programming.
B. Theoretical Explanation (15
minutes)
- Introduce the basic structure of a C program
with an example:
·
#include
<stdio.h> // Preprocessor
directive
·
int
main() { // Main function
·
printf("Hello, World!"); // Statement
·
return 0; // Return statement
·
}
- Explain each component:
- Preprocessor Directives
- Start with #include.
- Used to include libraries
like stdio.h (for standard /output
functions).
- The main() Function
- The starting point of every C
program.
- Must always exist in a C
program.
- Statements
- Instructions executed by the
program (e.g., printf).
- Return Statement
- Indicates the end of the
program and its successful execution.
- Discuss syntax rules and the importance of proper
formatting.
C. Practical Demonstration (15
minutes)
- Open the C compiler (e.g., Code::Blocks or Turbo C).
- Create a new program and type the example provided
above.
- Compile and run the program to show the output.
- Break down the output and how each part of the code
contributes to the result.
- Modify the program by adding additional statements
(e.g., printing the student's name or displaying a simple calculation).
D. Conclusion (5 minutes)
- Recap the importance of the structure of a C program.
- Reinforce how understanding the structure makes
debugging and writing code easier.
- Invite questions from students.
4.
Assessment
Oral Questions:
- What is the role of #include
<stdio.h> in a C program?
- Why is the main() function essential in every C program?
- What is the purpose of the return 0;
statement?
Activity:
- Ask students to write and run a program that prints
their name and age, following the standard structure of a C program.
Homework:
- Write down the structure of a C program on paper and
annotate each part with its purpose.
Here’s the updated lesson plan using
C language for the practical demonstration instead of Python:
5. Lesson Plan: Introduction to Programming
Grade: 10th
Duration: 40 minutes
1.
Objectives
By the end of this lesson, students
will be able to:
- Understand the concept of programming and its
importance.
- Identify the steps involved in writing a program.
- Recognize the basic components of a programming
environment.
2.
Materials Required
- Computer or projector for presentation.
- A C compiler (e.g., Code::Blocks, Turbo C, or GCC).
- Handouts or slides summarizing key concepts.
- Whiteboard and markers.
3.
Teaching Methodology
A. Introduction (5 minutes)
- Begin by asking: "Have you ever thought about
how your phone apps or video games work?"
- Briefly explain that programming is a way to instruct
computers to perform specific tasks.
B. Theoretical Explanation (15
minutes)
- Definition of Programming: Explain programming as the process of creating
instructions for a computer.
- Purpose of Programming: Highlight real-world examples (e.g., apps,
automation).
- Basic Steps of Programming:
- Defining the problem.
- Designing a solution.
- Writing the code.
- Testing and debugging.
C. Practical Demonstration (15
minutes)
- Open a basic C compiler (e.g., Code::Blocks or Turbo
C).
- Write a simple "Hello, World!" program in C:
·
#include
<stdio.h>
·
int
main() {
·
printf("Hello, World!");
·
return 0;
·
}
- Explain the structure of the program:
- #include <stdio.h>: Preprocessor directive for standard input/output.
- int main():
Entry point of the program.
- printf():
Function to print text on the screen.
- return 0;
Indicates the program executed successfully.
- Compile and run the program to show how code translates
into output.
- Emphasize the importance of syntax, debugging, and
understanding errors.
D. Conclusion (5 minutes)
- Recap the importance of programming and the steps
involved.
- Encourage students to think about how programming can
solve real-life problems.
4.
Assessment
Oral Questions:
- What is programming, and why is it important?
- Name the basic steps involved in writing a program.
- Identify the purpose of the printf
function in C.
Activity:
- Provide students with a simple task, such as writing a
program to display their name in C:
·
#include
<stdio.h>
·
int
main() {
·
printf("My name is [Your Name]");
·
return 0;
·
}
Homework:
- Research and write about two features of the C
programming language.
Watch this video to learn about Programming in detail
6. Lesson Plan: Purpose and Syntax of Comments
in C Program
Grade: 10th
Duration: 40 minutes
1.
Objectives
By the end of this lesson, students
will be able to:
- Define what comments are in C programming.
- Understand the purpose and importance of comments in a
program.
- Identify and write the syntax of single-line and
multi-line comments in C.
- Use comments effectively in their own programs.
2.
Materials Required
- Computer or projector for presentation.
- C compiler (e.g., GCC, Turbo C, or Code::Blocks).
- Handouts/slides summarizing the syntax and use of
comments.
- Example C programs with and without comments.
- Whiteboard and markers.
3.
Teaching Methodology
A. Introduction (5 minutes)
- Begin with a question: "Have you ever left
notes or labels on something to remind yourself or explain it to
others?"
- Relate this to programming: In a program, comments
serve as notes for the programmer or anyone else reading the code.
- Define comments: Non-executable text in a
program meant to explain the code.
B. Theoretical Explanation (10
minutes)
- What Are Comments?
- Text included in a program to make it easier to
understand but ignored by the compiler.
- Purpose of Comments:
- Document the purpose and logic of code for better
readability.
- Explain complex sections of the code.
- Make the program maintainable for future developers.
- Temporarily disable code during debugging.
- Types of Comments in C:
- Single-Line Comments:
- Use // to
add a comment to a single line.
- Example:
§ int age = 18; // This variable
stores the user's age
- Multi-Line Comments:
- Use /* to
start and */ to end comments spanning
multiple lines.
- Example:
§ /* This program calculates the
§ sum of two numbers */
§ int sum = a + b;
- Syntax of Comments:
- Single-line comment: //
comment text
- Multi-line comment: /*
comment text */
- Best Practices for Comments:
- Keep comments concise and relevant.
- Avoid over-commenting obvious code.
- Use comments to explain why something is done,
not what is done (the code itself should convey the
"what").
C. Practical Demonstration (20
minutes)
- Step 1: Write a Program Without Comments
- Show the following program on the projector or board:
o
#include
<stdio.h>
o
int
main() {
o
int a = 5, b = 10;
o
int sum = a + b;
o
printf("The sum is %d", sum);
o
return 0;
o
}
- Ask students if they can easily understand what the
variables and logic represent.
- Step 2: Add Comments to the Same Program
- Modify the program as follows:
o
#include
<stdio.h>
o
int
main() {
o
// Declaring and initializing variables
o
int a = 5, b = 10;
o
o
// Calculating the sum of two numbers
o
int sum = a + b;
o
o
// Printing the result
o
printf("The sum is %d", sum);
o
return 0;
o
}
- Show how comments make the program more readable and
understandable.
- Step 3: Use Multi-Line Comments
- Demonstrate another example:
o
/*
This program demonstrates
o
the use of multi-line comments
o
in a C program. */
o
#include
<stdio.h>
o
int
main() {
o
printf("Multi-line comments
example.");
o
return 0;
o
}
- Step 4: Explain What Happens if Comments Are Removed
- Show the program without comments and highlight how it
becomes harder to interpret.
- Step 5: Debugging With Comments
- Demonstrate how to comment out lines of code
temporarily during debugging:
o
//
printf("This line is commented out");
o
printf("This
line is not commented out");
D. Conclusion (5 minutes)
- Recap the purpose of comments: improving code
readability, maintainability, and debugging.
- Reiterate the syntax of single-line and multi-line
comments.
- Encourage students to use comments in every program
they write.
4.
Assessment
Oral Questions:
- What is the purpose of comments in programming?
- How does the compiler treat comments in C?
- What is the difference between single-line and
multi-line comments?
Activity:
- Ask students to add appropriate comments to the
following code:
·
#include
<stdio.h>
·
int
main() {
·
int x = 10;
·
int y = 20;
·
int product = x * y;
·
printf("The product is %d",
product);
·
return 0;
·
}
Homework:
- Write a simple program (e.g., to calculate the area of
a rectangle) and include both single-line and multi-line comments.
- Research and list the benefits of commenting code in
large software projects.
7. Lesson Plan: Variable Declaration and
Initialization in C
Grade: 10th
Duration: 40 minutes
1.
Objectives
By the end of this lesson, students
will be able to:
- Understand what variables are and their role in
programming.
- Define and explain the concept of variable declaration
and initialization.
- Identify different data types used for declaring
variables in C.
- Write C programs that declare and initialize variables
correctly.
2.
Materials Required
- Computer or projector for presentation.
- C compiler (e.g., GCC, Turbo C, or Code::Blocks) for
demonstration.
- Handouts/slides explaining variable declaration and
initialization.
- Whiteboard and markers for explaining concepts.
3.
Teaching Methodology
A. Introduction (5 minutes)
- Start with a real-life analogy: "Imagine you
have labeled boxes to store specific items (e.g., one for pens, another
for books). Similarly, variables in programming are labeled storage
locations for data."
- Define variables: Variables are named memory
locations that store data during the execution of a program.
B. Theoretical Explanation (15
minutes)
- Variable Declaration
- A variable must be declared before it is used in a
program.
- Declaration specifies the data type and variable name.
- Syntax:
o
data_type
variable_name;
Example:
int
age;
float
height;
char
grade;
- Variable Initialization
- Assigning a value to a variable at the time of
declaration.
- Syntax:
o
data_type
variable_name = value;
Example:
int
age = 18;
float
height = 5.9;
char
grade = 'A';
- Data Types in C
- Explain common data types:
|
Data
Type |
Description |
Example |
|
int |
Stores integers |
int age = 18; |
|
float |
Stores decimal numbers |
float pi = 3.14; |
|
char |
Stores a single character |
char grade = 'A'; |
|
double |
Stores large decimal numbers |
double g = 9.81; |
- Rules for Declaring Variables
- Variable names must start with a letter or underscore
(_).
- Variable names cannot be reserved words (e.g., int, return).
- Variable names are case-sensitive.
C. Practical Demonstration (15
minutes)
- Step 1: Declaring Variables Without Initialization
- Open the compiler and write the following program:
o
#include
<stdio.h>
o
int
main() {
o
int age;
o
printf("The age is: %d", age);
o
return 0;
o
}
- Show the warning/error message when the variable is
used without initialization.
- Step 2: Declaring and Initializing Variables
- Modify the program:
o
#include
<stdio.h>
o
int
main() {
o
int age = 18;
o
printf("The age is: %d", age);
o
return 0;
o
}
- Compile and run the program to demonstrate proper
usage.
- Step 3: Multiple Variables Declaration and
Initialization
·
#include
<stdio.h>
·
·
int
main() {
·
int a = 5, b = 10;
·
float pi = 3.14;
·
char grade = 'A';
·
printf("a = %d, b = %d, pi = %.2f,
grade = %c", a, b, pi, grade);
·
return 0;
·
}
- Explain how multiple variables can be declared and
initialized together.
- Step 4: Variable Reassignment
- Demonstrate how values of variables can change during
program execution:
o
#include
<stdio.h>
o
o
int
main() {
o
int x = 10;
o
printf("Initial value of x:
%d\n", x);
o
x = 20; // Reassigning x
o
printf("Updated value of x: %d",
x);
o
return 0;
o
}
D. Conclusion (5 minutes)
- Summarize the importance of declaring and initializing
variables before using them.
- Emphasize the role of data types in determining the
kind of data a variable can store.
- Reinforce the syntax and rules for declaring variables.
4.
Assessment
Oral Questions:
- What is a variable, and why do we need it in a program?
- What is the difference between variable declaration and
initialization?
- Give an example of a valid variable declaration and
initialization in C.
Activity:
- Write a program that declares variables of different
data types and initializes them with values of your choice. Display the
values of all variables using printf().
Homework:
- Write a program that:
- Declares and initializes at least five variables of
different data types.
- Prints their values on the screen.
- Research and list 10 rules for naming variables in C.
Watch this video to learn how to declare and initialize variables in C
8. Lesson Plan: Input and Output Functions in C
Grade: 10th
Duration: 40 minutes
1.
Objectives
By the end of this lesson, students
will be able to:
- Understand the purpose of input and output functions in
C programming.
- Learn how to use printf() for output and scanf() for input.
- Write and execute simple programs using printf()
and scanf().
2.
Materials Required
- Computer or projector for presentation.
- C compiler (e.g., GCC, Turbo C, or Code::Blocks).
- Handouts/slides summarizing the syntax and usage of printf()
and scanf().
- Whiteboard and markers for explanation.
3.
Teaching Methodology
A. Introduction (5 minutes)
- Begin with a question: "How do we communicate
with a computer when writing programs? How do we get data into a program
and display results to the user?"
- Explain: Input functions allow the program to receive
data from the user, while output functions display information to the
user.
- State the two most commonly used input/output functions
in C:
- printf():
Displays output to the screen.
- scanf():
Reads input from the user.
B. Theoretical Explanation (10
minutes)
- printf()
(Output Function)
- Used to display text, numbers, and results on the
screen.
- Syntax:
o
printf("format
string", variable_list);
- Format specifiers:
|
Specifier |
Data
Type |
Example |
|
%d |
Integer |
printf("%d", 10); |
|
%f |
Float |
printf("%.2f", 3.14); |
|
%c |
Character |
printf("%c", 'A'); |
|
%s |
String |
printf("%s", "Hello"); |
- Example:
o
printf("The
value is: %d", 10);
- scanf()
(Input Function)
- Used to take input from the user during program
execution.
- Syntax:
o
scanf("format
string", &variable_list);
- Key points:
- Ampersand (&)
is used before variable names (except for strings).
- Format specifiers are the
same as in printf().
- Example:
o
int
age;
o
scanf("%d",
&age);
- Combined Example Using printf()
and scanf()
- Example program:
o
#include
<stdio.h>
o
int
main() {
o
int age;
o
printf("Enter your age: ");
o
scanf("%d", &age);
o
printf("Your age is: %d", age);
o
return 0;
o
}
- Common Mistakes:
- Forgetting to use & with scanf().
- Using incorrect format specifiers.
C. Practical Demonstration (20
minutes)
- Basic Output Using printf()
- Write a program to display a greeting message:
o
#include
<stdio.h>
o
int
main() {
o
printf("Hello, World!");
o
return 0;
o
}
- Using Variables with printf()
- Program to display the values of variables:
o
#include
<stdio.h>
o
int
main() {
o
int x = 5;
o
float pi = 3.14;
o
printf("x = %d, pi = %.2f", x,
pi);
o
return 0;
o
}
- Taking User Input Using scanf()
- Program to take input and display it:
o
#include
<stdio.h>
o
int
main() {
o
int num;
o
printf("Enter a number: ");
o
scanf("%d", &num);
o
printf("You entered: %d", num);
o
return 0;
o
}
- Program with Multiple Inputs and Outputs
- Write and run the following program:
o
#include
<stdio.h>
o
int
main() {
o
int a, b;
o
printf("Enter two numbers: ");
o
scanf("%d %d", &a, &b);
o
printf("You entered %d and %d",
a, b);
o
return 0;
o
}
- Error Demonstration
- Show what happens if & is missed in scanf().
- Correct the error and explain the solution.
D. Conclusion (5 minutes)
- Recap the purpose of printf() and scanf().
- Emphasize the importance of format specifiers and
proper syntax.
- Highlight common errors to avoid when using
input/output functions.
4.
Assessment
Oral Questions:
- What is the purpose of printf() in C programming?
- How does scanf() work, and why do we use the &
symbol?
- What happens if you use the wrong format specifier?
Activity:
- Ask students to write a program that:
- Takes the user's name, age, and height as input.
- Displays the entered information using printf().
Example output:
·
Enter
your name: John
·
Enter
your age: 20
·
Enter
your height: 5.9
·
Name:
John, Age: 20, Height: 5.9
Homework:
- Write a program that:
- Asks the user for two integers and a floating-point
number.
- Calculates their sum and displays the result.
- Research and list at least 5 format specifiers used in printf()
and scanf() with examples.
Watch the below videos to learn how Printf() and Scanf() functions work in C language
9. Lesson Plan: Format Specifiers in C
Grade: 10th
Duration: 40 minutes
1.
Objectives
By the end of this lesson, students
will be able to:
- Understand the purpose of format specifiers in C
programming.
- Learn and use different format specifiers in printf()
and scanf().
- Write programs demonstrating various data types and
their respective format specifiers.
2.
Materials Required
- Computer or projector for presentation.
- C compiler (e.g., GCC, Turbo C, or Code::Blocks).
- Handouts/slides listing format specifiers and their
usage.
- Whiteboard and markers for explanation.
3.
Teaching Methodology
A. Introduction (5 minutes)
- Ask students: "How does a program differentiate
between an integer, a floating-point number, or a string during
input/output?"
- Explain: Format specifiers are placeholders used in printf()
and scanf() to indicate the type of data being handled.
- Introduce the concept with a simple example:
·
printf("The
value is: %d", 10);
B. Theoretical Explanation (10
minutes)
- Definition of Format Specifiers
- Format specifiers are codes that define the type of
data to be printed or read.
- They start with a % symbol.
- Commonly Used Format Specifiers
- Integer (%d or %i)
Used for signed integers.
Example: printf("%d", 42); - Floating-point (%f)
Used for decimal numbers.
Example: printf("%.2f", 3.14159); - Character (%c)
Used for single characters.
Example: printf("%c", 'A'); - String (%s)
Used for strings of characters.
Example: printf("%s", "Hello"); - Octal (%o)
Used to display integers in octal format.
Example: printf("%o", 8); - Hexadecimal (%x or %X)
Used to display integers in hexadecimal format.
Example: printf("%x", 15); - Unsigned Integer (%u)
Used for non-negative integers.
Example: printf("%u", 100); - Using Format Specifiers in scanf()
- Syntax:
o
scanf("format
string", &variable);
- Example:
o
int
num;
o
scanf("%d",
&num);
- Special Features of Format Specifiers
- Precision Control:
Example: %.2f limits the floating-point output to 2 decimal places. - Width Control:
Example: %5d reserves 5 spaces for the output. - Common Errors to Avoid:
- Mismatched format specifiers and variables.
- Forgetting the & symbol in scanf().
C. Practical Demonstration (20
minutes)
- Displaying an Integer
- Example program:
o
#include
<stdio.h>
o
int
main() {
o
int num = 25;
o
printf("The value of num is: %d",
num);
o
return 0;
o
}
- Using Multiple Specifiers in a Single Statement
- Program to display multiple data types:
o
#include
<stdio.h>
o
int
main() {
o
int age = 20;
o
float height = 5.8;
o
char grade = 'A';
o
printf("Age: %d, Height: %.1f, Grade:
%c", age, height, grade);
o
return 0;
o
}
- Taking Input with scanf()
- Program for user input:
o
#include
<stdio.h>
o
int
main() {
o
int a;
o
float b;
o
printf("Enter an integer and a float:
");
o
scanf("%d %f", &a, &b);
o
printf("You entered: %d and
%.2f", a, b);
o
return 0;
o
}
- Using Hexadecimal and Octal
- Program to demonstrate %x and %o:
o
#include
<stdio.h>
o
int
main() {
o
int num = 15;
o
printf("Decimal: %d, Octal: %o,
Hexadecimal: %x", num, num, num);
o
return 0;
o
}
- Precision Example for Floating-point Numbers
- Example program:
o
#include
<stdio.h>
o
int
main() {
o
float pi = 3.14159265359;
o
printf("Pi to 2 decimal places:
%.2f", pi);
o
return 0;
o
}
D. Conclusion (5 minutes)
- Recap the purpose and importance of format specifiers
in input/output functions.
- Emphasize the need for proper matching of format
specifiers with data types.
- Highlight key points to avoid errors, such as using the
correct format specifier and including & in scanf().
4.
Assessment
Oral Questions:
- What is the purpose of %d in printf()?
- How does %f differ from %d?
- What happens if you use the wrong format specifier for
a variable?
Activity:
- Ask students to write a program that:
- Takes an integer, a floating-point number, and a
character as input.
- Displays these values in the output using their
respective format specifiers.
Homework:
- Write a program that:
- Reads two integers and a floating-point number from
the user.
- Displays the sum of the integers and the product of
all three numbers.
- Research and summarize any additional format specifiers
in C programming.
Watch this video to learn how Format Specifiers works in C
10. Lesson Plan: Escape Sequences in C
Grade: 10th
Duration: 40 minutes
1.
Objectives
By the end of this lesson, students
will be able to:
- Define escape sequences and understand their purpose in
C programming.
- Identify commonly used escape sequences and their
functions.
- Use escape sequences in programs to format output
effectively.
2.
Materials Required
- Computer or projector for presentation.
- C compiler (e.g., GCC, Turbo C, or Code::Blocks).
- Handouts/slides listing escape sequences with examples.
- Whiteboard and markers for explanations.
3.
Teaching Methodology
A. Introduction (5 minutes)
- Ask: "Have you ever tried to include a new line
or a tab in your program's output? How do you instruct the program to do
this?"
- Explain that escape sequences are special
characters used in programming to represent non-printable actions, like
creating new lines or adding tabs.
- Relate to real-life examples, such as pressing
"Enter" to create a new line.
B. Theoretical Explanation (10
minutes)
- What Are Escape Sequences?
- Escape sequences are combinations of a backslash (\)
followed by a character, which represent specific actions or symbols in
output.
- Used to control the formatting of text.
- Purpose of Escape Sequences:
- Format output text (e.g., new lines, tabs).
- Represent special characters (e.g., quotes,
backslash).
- Common Escape Sequences in C:
|
Escape
Sequence |
Purpose |
Example
Usage |
|
\n |
New line |
printf("Hello\nWorld"); |
|
\t |
Horizontal tab |
printf("Hello\tWorld"); |
|
\\ |
Backslash (\) |
printf("\\"); |
|
\" |
Double quote (") |
printf("\"Hello\""); |
|
\' |
Single quote (') |
printf("\'World\'"); |
|
\b |
Backspace |
printf("Hell\blo"); |
|
\r |
Carriage return |
printf("Hello\rWorld"); |
- Key Notes:
- Escape sequences must begin with a backslash (\).
- They are used within string literals in printf()
or puts().
- Improper use may result in errors or unexpected
output.
C. Practical Demonstration (20
minutes)
- Basic Usage of Escape Sequences
- New Line (\n):
o
#include
<stdio.h>
o
int
main() {
o
printf("Hello\nWorld");
o
return 0;
o
}
Output:
Hello
World
- Tab (\t):
o
#include
<stdio.h>
o
o
int
main() {
o
printf("Name\tAge\nAlice\t20\nBob\t25");
o
return 0;
o
}
Output:
Name Age
Alice 20
Bob 25
- Using Quotes and Backslash in Strings
- Double Quotes and Backslash:
o
#include
<stdio.h>
o
int
main() {
o
printf("This is a \"quoted\"
string with a backslash: \\");
o
return 0;
o
}
Output:
This
is a "quoted" string with a backslash: \
- Combining Multiple Escape Sequences
- Program with multiple formatting elements:
o
#include
<stdio.h>
o
o
int
main() {
o
printf("Line1\tColumn1\nLine2\tColumn2\n\"C
Programming\"");
o
return 0;
o
}
Output:
Line1 Column1
Line2 Column2
"C
Programming"
- Backspace (\b) Example
- Demonstrate how \b works:
o
#include
<stdio.h>
o
o
int
main() {
o
printf("Helllo\b World");
o
return 0;
o
}
Output:
Hello
World
- Carriage Return (\r) Example
- Show how \r replaces text after it:
o
#include
<stdio.h>
o
o
int
main() {
o
printf("Hello\rWorld");
o
return 0;
o
}
Output:
World
D. Conclusion (5 minutes)
- Recap the purpose of escape sequences in formatting
program output.
- Highlight commonly used sequences (\n, \t, \\, \") and
their importance.
- Encourage students to use escape sequences to improve
program readability.
4.
Assessment
Oral Questions:
- What is the purpose of \n in a printf() statement?
- How do you include a double quote inside a string in C?
- Which escape sequence is used to add a tab in the
output?
Activity:
- Ask students to write a program that:
- Displays the following formatted output:
o
Name Age
o
Alice 20
o
Bob 25
Homework:
- Write a program that uses at least five escape
sequences, such as new line, tab, double quote, and backslash.
- Research and explain the difference between \n and \r in
C.
Watch this video to learn how Escape Sequence works in C language
11.
Lesson Plan: Assignment Operators in C
Grade: 10th
Duration: 40 minutes
1.
Objectives
By the end of this lesson, students
will be able to:
- Define assignment operators and understand their role
in programming.
- Identify and use the basic assignment operator (=).
- Understand and apply compound assignment operators
(e.g., +=, -=, *=, /=, %=).
- Write C programs demonstrating the use of assignment
operators.
2.
Materials Required
- Computer or projector for presentation.
- C compiler (e.g., GCC, Turbo C, or Code::Blocks).
- Handouts/slides listing assignment operators and their
usage.
- Whiteboard and markers for explanation.
3.
Teaching Methodology
A. Introduction (5 minutes)
- Start with a simple analogy: "When we assign
homework, we expect students to complete it as instructed. Similarly, in
programming, the assignment operator is used to assign values to
variables."
- Define assignment operators: Operators that
store a value in a variable.
B. Theoretical Explanation (10
minutes)
- Basic Assignment Operator (=)
- Assigns the value on the right-hand side to the variable
on the left-hand side.
- Syntax:
o
variable
= value;
- Example:
o
int
x = 10;
- Compound Assignment Operators
- Combine arithmetic operations with assignment to
simplify coding.
- Common compound assignment operators:
|
Operator |
Example |
Equivalent
To |
Description |
|
+= |
x += 5; |
x = x + 5; |
Adds and assigns the result |
|
-= |
x -= 3; |
x = x - 3; |
Subtracts and assigns the result |
|
*= |
x *= 2; |
x = x * 2; |
Multiplies and assigns the result |
|
/= |
x /= 4; |
x = x / 4; |
Divides and assigns the result |
|
%= |
x %= 2; |
x = x % 2; |
Modulus and assigns the result |
- Key Points to Remember
- The variable must be declared before using assignment
operators.
- Compound assignment operators are shorthand for common
arithmetic operations.
- The value on the right-hand side can be a constant,
variable, or expression.
C. Practical Demonstration (20
minutes)
- Using the Basic Assignment Operator (=)
- Program to assign and display values:
o
#include
<stdio.h>
o
int
main() {
o
int a = 10; // Basic assignment
o
printf("The value of a is: %d",
a);
o
return 0;
o
}
- Using Compound Assignment Operators
- Demonstration program:
o
#include
<stdio.h>
o
int
main() {
o
int x = 10;
o
x += 5; // Add and assign
o
printf("After x += 5, x = %d\n",
x);
o
x -= 3; // Subtract and assign
o
printf("After x -= 3, x = %d\n",
x);
o
x *= 2; // Multiply and assign
o
printf("After x *= 2, x = %d\n",
x);
o
x /= 4; // Divide and assign
o
printf("After x /= 4, x = %d\n",
x);
o
x %= 3; // Modulus and assign
o
printf("After x %= 3, x = %d\n",
x);
o
return 0;
o
}
- Real-world Application of Assignment Operators
- Program to calculate and update values:
o
#include
<stdio.h>
o
int
main() {
o
int total = 100;
o
total += 20; // Adding bonus
o
total -= 10; // Deducting penalty
o
printf("The final total is: %d",
total);
o
return 0;
o
}
- Error Demonstration
- Show what happens when assignment operators are
misused:
o
#include
<stdio.h>
o
o
int
main() {
o
int x;
o
x += 5; // Using x without initialization
o
printf("x = %d", x);
o
return 0;
o
}
- Correct the error by initializing x
before using it:
o
int
x = 0;
o
x +=
5;
D. Conclusion (5 minutes)
- Recap the role of assignment operators in storing and
updating variable values.
- Highlight the efficiency of compound assignment
operators in simplifying code.
- Encourage students to practice using assignment
operators in different scenarios.
4.
Assessment
Oral Questions:
- What is the purpose of the assignment operator =?
- How does x +=
5 differ from x = x + 5?
- Name three compound assignment operators and explain
their usage.
Activity:
- Ask students to write a program that:
- Declares an integer variable y and
initializes it to 50.
- Updates the value of y using all compound assignment operators.
- Displays the updated values after each operation.
Homework:
- Write a program that calculates the total score of a
student:
- Initialize the score to 0.
- Add marks from five tests using the +=
operator.
- Deduct marks for a missed test using the -=
operator.
- Display the final score.
- Research and summarize the differences between compound
assignment operators and regular arithmetic operations.
Watch this video to learn how Assignment operators work in C language
This Video explains what is the difference between Assinment operator and Equal to Operator in C language
12. Lesson Plan: Arithmetic Operators in C
Grade: 10th
Duration: 40 minutes
1.
Objectives
By the end of this lesson, students
will be able to:
- Understand the purpose and types of arithmetic
operators in C.
- Use arithmetic operators to perform basic mathematical
calculations.
- Write programs demonstrating the usage of arithmetic
operators.
2.
Materials Required
- Computer or projector for presentation.
- C compiler (e.g., GCC, Turbo C, or Code::Blocks).
- Handouts/slides summarizing arithmetic operators with
examples.
- Whiteboard and markers for explanation.
3.
Teaching Methodology
A. Introduction (5 minutes)
- Begin by asking: "What are some basic
mathematical operations you use in daily life?"
- Relate to programming: Arithmetic operators in C allow
us to perform mathematical calculations like addition, subtraction,
multiplication, division, and modulus.
- Define arithmetic operators: Symbols used to
perform mathematical operations on variables and values.
B. Theoretical Explanation (10
minutes)
- List of Arithmetic Operators in C:
|
Operator |
Symbol |
Description |
Example |
|
Addition |
+ |
Adds two numbers |
x + y |
|
Subtraction |
- |
Subtracts the second number |
x - y |
|
Multiplication |
* |
Multiplies two numbers |
x * y |
|
Division |
/ |
Divides the first number by the
second |
x / y |
|
Modulus |
% |
Returns the remainder of division |
x % y |
- Key Points to Remember:
- Addition and Subtraction work as expected.
- Multiplication
uses the * symbol, not x.
- Division (/)
returns the quotient but ignores the remainder for integers.
- Modulus (%) returns
the remainder of division (works only with integers).
- Common Errors to Avoid:
- Dividing by zero (x /
0) causes a runtime error.
- Using % with non-integer data types like float.
C. Practical Demonstration (20
minutes)
- Basic Arithmetic Operations
- Program to perform addition, subtraction,
multiplication, and division:
o
#include
<stdio.h>
o
int
main() {
o
int a = 10, b = 5;
o
printf("Addition: %d\n", a + b);
o
printf("Subtraction: %d\n", a -
b);
o
printf("Multiplication: %d\n", a
* b);
o
printf("Division: %d\n", a / b);
o
return 0;
o
}
- Modulus Operator
- Program to demonstrate %:
o
#include
<stdio.h>
o
int
main() {
o
int x = 10, y = 3;
o
printf("Remainder when %d is divided
by %d: %d", x, y, x % y);
o
return 0;
o
}
- Combining Operators in Expressions
- Demonstrate operator precedence and associativity:
o
#include
<stdio.h>
o
int
main() {
o
int result = 10 + 5 * 2 - 6 / 2;
o
printf("Result of expression:
%d", result);
o
return 0;
o
}
- Explain the precedence: Multiplication (*),
division (/), and modulus (%) are evaluated before addition (+)
and subtraction (-).
- Using Arithmetic Operators with float
- Program to show operations with floating-point
numbers:
o
#include
<stdio.h>
o
o
int
main() {
o
float x = 5.5, y = 2.0;
o
printf("Addition: %.2f\n", x +
y);
o
printf("Division: %.2f\n", x /
y);
o
return 0;
o
}
- Error Demonstration
- Division by zero example:
o
#include
<stdio.h>
o
int
main() {
o
int x = 10, y = 0;
o
printf("Division: %d", x / y); //
Causes a runtime error
o
return 0;
o
}
- Correct the error: Ensure y is
not zero before division.
D. Conclusion (5 minutes)
- Recap the purpose of arithmetic operators and their
role in performing mathematical operations.
- Emphasize the importance of understanding operator
precedence and avoiding common errors like division by zero.
- Encourage students to experiment with different
expressions to strengthen their understanding.
4.
Assessment
Oral Questions:
- What is the purpose of the modulus operator %?
- What is the result of the expression 5 + 10 * 2?
- Can you use the % operator with floating-point numbers? Why or why not?
Activity:
- Write a program to calculate the area of a rectangle
and the perimeter of a square using the appropriate arithmetic operators.
Homework:
- Write a program that:
- Takes two integers as input.
- Performs all arithmetic operations (+, -, *, /, %)
and displays the results.
- Research and explain how operator precedence affects
the evaluation of the expression 10 +
20 * 3 / 2.
13. Lesson Plan: Relational Operators in C
Grade: 10th
Duration: 40 minutes
1.
Objectives
By the end of this lesson, students
will be able to:
- Understand the purpose of relational operators in C
programming.
- Use relational operators to compare values and
expressions.
- Write programs that demonstrate the use of relational
operators for decision-making.
2.
Materials Required
- Computer or projector for presentation.
- C compiler (e.g., GCC, Turbo C, or Code::Blocks).
- Handouts/slides summarizing relational operators and
their usage.
- Whiteboard and markers for explanation.
3.
Teaching Methodology
A. Introduction (5 minutes)
- Begin by asking: "Have you ever compared two
numbers or values to determine which one is greater, smaller, or
equal?"
- Relate this to programming: Relational operators
are used to compare values or variables and return a boolean value (true or false).
- Define relational operators: Operators used to
compare two values or expressions.
B. Theoretical Explanation (10
minutes)
- List of Relational Operators in C:
|
Operator |
Symbol |
Description |
Example |
|
Equal to |
== |
Checks if two values are equal |
x == y |
|
Not equal to |
!= |
Checks if two values are not equal |
x != y |
|
Greater than |
> |
Checks if the left value is
greater than the right |
x > y |
|
Less than |
< |
Checks if the left value is less
than the right |
x < y |
|
Greater than or equal to |
>= |
Checks if the left value is
greater than or equal to the right |
x >= y |
|
Less than or equal to |
<= |
Checks if the left value is less
than or equal to the right |
x <= y |
- Purpose of Relational Operators:
- Relational operators allow the programmer to make
comparisons between variables or values.
- They are essential in decision-making (e.g.,
if-else statements, loops).
- Results of Relational Operators:
- Relational operators return a boolean value:
- true
(1) if the condition is satisfied.
- false
(0) if the condition is not satisfied.
C. Practical Demonstration (20
minutes)
- Using the == Operator (Equality)
- Program to check if two numbers are equal:
o
#include
<stdio.h>
o
o
int
main() {
o
int a = 10, b = 10;
o
if (a == b) {
o
printf("a and b are
equal.\n");
o
} else {
o
printf("a and b are not
equal.\n");
o
}
o
return 0;
o
}
- Using the != Operator (Inequality)
- Program to check if two numbers are not equal:
o
#include
<stdio.h>
o
o
int
main() {
o
int a = 10, b = 5;
o
if (a != b) {
o
printf("a and b are not
equal.\n");
o
} else {
o
printf("a and b are
equal.\n");
o
}
o
return 0;
o
}
- Using the > and < Operators (Greater than and Less than)
- Program to check if one number is greater than or less
than another:
o
#include
<stdio.h>
o
o
int
main() {
o
int a = 10, b = 5;
o
if (a > b) {
o
printf("a is greater than
b.\n");
o
} else {
o
printf("a is not greater than
b.\n");
o
}
o
o
if (b < a) {
o
printf("b is less than
a.\n");
o
} else {
o
printf("b is not less than
a.\n");
o
}
o
o
return 0;
o
}
- Using the >= and <= Operators (Greater than or equal to, Less than or
equal to)
- Program to check if one number is greater than or
equal to or less than or equal to another:
o
#include
<stdio.h>
o
o
int
main() {
o
int a = 10, b = 10;
o
if (a >= b) {
o
printf("a is greater than or equal
to b.\n");
o
} else {
o
printf("a is not greater than or
equal to b.\n");
o
}
o
o
if (b <= a) {
o
printf("b is less than or equal to
a.\n");
o
} else {
o
printf("b is not less than or
equal to a.\n");
o
}
o
o
return 0;
o
}
- Using Relational Operators with if
Statements
- Show how relational operators are used in
decision-making with if-else statements:
o
#include
<stdio.h>
o
o
int
main() {
o
int x
= 10, y = 5;
o
o
if (x > y) {
o
printf("x is greater than
y\n");
o
} else {
o
printf("x is not greater than
y\n");
o
}
o
o
return 0;
o
}
D. Conclusion (5 minutes)
- Recap the role of relational operators in comparison
and decision-making.
- Emphasize their importance in controlling the flow of a
program through conditions (e.g., in if statements, loops).
- Encourage students to experiment with different
relational operators in their own programs.
4.
Assessment
Oral Questions:
- What is the difference between == and = in C?
- What value does the relational expression 10 > 5
return?
- Which relational operator would you use to check if two
values are equal?
Activity:
- Write a program that:
- Takes two integer inputs from the user.
- Compares the numbers using all relational operators
and prints the corresponding results (e.g., greater than, less than,
equal to).
Homework:
- Write a program that checks whether a number is between
10 and 20 using relational operators.
- Research and write about how relational operators are
used in while and for loops.
14. Lesson Plan: Logical Operators in C
Grade: 10th
Duration: 40 minutes
1.
Objectives
By the end of this lesson, students
will be able to:
- Understand the purpose and types of logical operators
in C.
- Use logical operators to combine multiple conditions in
expressions.
- Write programs that demonstrate the use of logical
operators in decision-making.
2.
Materials Required
- Computer or projector for presentation.
- C compiler (e.g., GCC, Turbo C, or Code::Blocks).
- Handouts/slides summarizing logical operators with
examples.
- Whiteboard and markers for explanation.
3.
Teaching Methodology
A. Introduction (5 minutes)
- Ask: "In programming, how do we make decisions
based on multiple conditions? For example, checking if a number is both
greater than 10 and less than 20?"
- Introduce logical operators: These operators
allow us to combine multiple conditions or expressions and return a
boolean result (true/false).
- Define logical operators: Operators used to
combine conditional statements.
B. Theoretical Explanation (10
minutes)
- List of Logical Operators in C:
|
Operator |
Symbol |
Description |
Example |
|
Logical AND |
&& |
Returns true if both conditions are true |
x > 5 && x < 10 |
|
Logical OR |
` |
` |
|
|
Logical NOT |
! |
Reverses the boolean value of the
condition |
!(x > 5) |
- How Logical Operators Work:
- Logical AND (&&):
Evaluates to true if both conditions are true. Otherwise, it
evaluates to false.
- Logical OR (||):
Evaluates to true if at least one of the conditions is true.
Otherwise, it evaluates to false.
- Logical NOT (!):
Reverses the boolean value. If the condition is true, it becomes false,
and if the condition is false, it becomes true.
- Truth Tables for Logical Operators:
- AND (&&)
|
Condition
1 |
Condition
2 |
Condition
1 && Condition 2 |
|
true |
true |
true |
|
true |
false |
false |
|
false |
true |
false |
|
false |
false |
false |
- OR (||)
| Condition 1 | Condition 2 | Condition 1 || Condition 2 |
|-------------|-------------|-----------------------------|
| true | true | true |
| true | false | true |
| false | true | true |
| false | false | false | - NOT (!)
|
Condition
1 |
!Condition
1 |
|
true |
false |
|
false |
true |
C. Practical Demonstration (20
minutes)
- Using Logical AND (&&)
- Program to check if a number is between two values:
o
#include
<stdio.h>
o
o
int
main() {
o
int num = 8;
o
if (num > 5 && num < 10) {
o
printf("The number is between 5
and 10.\n");
o
} else {
o
printf("The number is not between
5 and 10.\n");
o
}
o
return 0;
o
}
- Using Logical OR (||)
- Program to check if a number is either less than 5 or
greater than 20:
o
#include
<stdio.h>
o
o
int
main() {
o
int num = 3;
o
if (num < 5 || num > 20) {
o
printf("The number is less than 5
or greater than 20.\n");
o
} else {
o
printf("The number is between 5
and 20.\n");
o
}
o
return 0;
o
}
- Using Logical NOT (!)
- Program to reverse the boolean result:
o
#include
<stdio.h>
o
o
int
main() {
o
int x = 10;
o
if (!(x < 5)) {
o
printf("x is not less than
5.\n");
o
} else {
o
printf("x is less than
5.\n");
o
}
o
return 0;
o
}
- Combining Logical Operators in a Complex Expression
- Program to check multiple conditions using AND, OR,
and NOT:
o
#include
<stdio.h>
o
o
int
main() {
o
int age = 25;
o
char grade = 'A';
o
o
if (age >= 18 && grade == 'A') {
o
printf("You are eligible for the
prize.\n");
o
} else {
o
printf("You are not eligible for
the prize.\n");
o
}
o
return 0;
o
}
D. Conclusion (5 minutes)
- Recap the importance of logical operators in combining
conditions to control program flow.
- Highlight the use of &&, ||, and ! in decision-making and complex conditions.
- Encourage students to practice using logical operators
in real-world scenarios like validation or checking multiple conditions.
4.
Assessment
Oral Questions:
- What does the && operator do in C?
- How is the || operator different from &&?
- What will the following condition evaluate to: !(x > 10)
if x = 5?
Activity:
- Write a program that:
- Takes a user’s age and grade as input.
- Checks if the user is eligible for a scholarship: The
user must be at least 18 years old and have a grade of 'A' or 'B'.
- Prints "Eligible for scholarship" if the
condition is true, otherwise "Not eligible".
Homework:
- Write a program that checks if a number is positive,
even, and less than 100 using logical operators.
- Research and summarize how logical operators can be
used in loops (e.g., while, for).
15. Lesson Plan: Operator Precedence in C
Grade: 10th
Duration: 40 minutes
1.
Objectives
By the end of this lesson, students
will be able to:
- Understand the concept of operator precedence in C
programming.
- Learn how operators are evaluated in expressions based
on their precedence.
- Write programs that demonstrate operator precedence and
associativity.
- Use parentheses effectively to control the evaluation
order of expressions.
2.
Materials Required
- Computer or projector for presentation.
- C compiler (e.g., GCC, Turbo C, or Code::Blocks).
- Handouts/slides listing operator precedence and
associativity.
- Whiteboard and markers for explanation.
3.
Teaching Methodology
A. Introduction (5 minutes)
- Start with a question: "When you write an
expression with multiple operators, how does the computer know which
operator to evaluate first?"
- Explain that operator precedence dictates the
order in which operations are performed in an expression, ensuring the
program evaluates complex expressions correctly.
- Define operator precedence: The set of rules
that determine the order in which operators are applied in expressions.
B. Theoretical Explanation (10
minutes)
- What is Operator Precedence?
- Operators in C are given a priority based on their
type (arithmetic, relational, logical, etc.).
- Higher precedence operators are evaluated first, and
lower precedence operators are evaluated later.
- Operator Precedence Table:
- Display the precedence of common operators in C.
Higher precedence operators are listed first.
|
Operator |
Precedence |
Associativity |
|
Parentheses () |
Highest |
Left to right |
|
Unary operators ++, --, +,
-, !, ~ |
2nd |
Right to left |
|
Multiplication, Division, Modulus *, /, % |
3rd |
Left to right |
|
Addition, Subtraction +, - |
4th |
Left to right |
|
Relational operators >, <, >=, <=, ==,
!= |
5th |
Left to right |
|
Logical AND && |
6th |
Left to right |
|
Logical OR ` |
` |
|
|
Assignment =, +=, -=,
*=, /=, etc. |
Lowest |
Right to left |
- Associativity:
- The associativity of an operator determines the
direction in which operators with the same precedence are evaluated.
- Most operators in C have left-to-right
associativity, but some, like the assignment operators, have right-to-left
associativity.
- Examples of Operator Precedence in Action:
- Without parentheses, multiplication (*) is
performed before addition (+) because * has higher precedence than +.
- Example:
o
int
result = 5 + 3 * 2; // result = 11, not
16
C. Practical Demonstration (20
minutes)
- Simple Expressions Without Parentheses
- Program to demonstrate operator precedence:
o
#include
<stdio.h>
o
o
int
main() {
o
int result = 5 + 3 * 2;
o
printf("Result without parentheses:
%d\n", result); // Expected output:
11
o
return 0;
o
}
- Using Parentheses to Change Evaluation Order
- Program to modify the evaluation order with
parentheses:
o
#include
<stdio.h>
o
o
int
main() {
o
int result = (5 + 3) * 2;
o
printf("Result with parentheses:
%d\n", result); // Expected output:
16
o
return 0;
o
}
- Explain that the parentheses override the default
precedence, and the expression inside them is evaluated first.
- Complex Expression Example
- Program to evaluate a complex expression involving
multiple operators:
o
#include
<stdio.h>
o
o
int
main() {
o
int result = 10 + 20 / 5 * 2 - 3;
o
printf("Complex expression result:
%d\n", result); // Expected output:
15
o
return 0;
o
}
- Discuss the order of operations:
- First, division (/)
and multiplication (*) are evaluated, then
addition (+), and finally subtraction (-).
- Demonstrating Left-to-Right and Right-to-Left
Associativity
- Program to show left-to-right associativity with
addition and subtraction:
o
#include
<stdio.h>
o
o
int
main() {
o
int result = 5 - 3 + 2;
o
printf("Left-to-right associativity
result: %d\n", result); // Expected
output: 4
o
return 0;
o
}
- Program to show right-to-left associativity with
assignment:
o
#include
<stdio.h>
o
o
int
main() {
o
int x = 5, y = 3;
o
x += y -= 1; // y = 2, then x = 7
o
printf("Right-to-left associativity
result: x = %d, y = %d\n", x, y);
// Expected output: x = 7, y = 2
o
return 0;
o
}
D. Conclusion (5 minutes)
- Recap the importance of operator precedence and
associativity in controlling the order of evaluation in expressions.
- Emphasize how parentheses can be used to alter the
default precedence and ensure the desired order of operations.
- Encourage students to practice using operator
precedence in more complex expressions to reinforce their understanding.
4.
Assessment
Oral Questions:
- What is operator precedence, and why is it important in
C programming?
- Which operator has higher precedence: * or +?
- How do parentheses affect the evaluation of expressions
in C?
Activity:
- Write a program that evaluates the following expression
and displays the result:
·
result
= 4 + 5 * 2 / (3 - 1) + 8;
Homework:
- Write a program that checks if a given expression,
without parentheses, is evaluated the way you expect it (using precedence
rules).
- Research and summarize how operator precedence works in
other programming languages you are familiar with (e.g., Python, Java).
16. Lesson Plan: Difference Between if and if-else Statement in C
Grade: 10th
Duration: 40 minutes
1.
Objectives
By the end of this lesson, students
will be able to:
- Understand the purpose of the if and if-else
statements in C programming.
- Learn the syntax and behavior of both if and if-else
statements.
- Identify when to use if versus if-else based on conditions and requirements.
- Write programs demonstrating the use of if and if-else
statements.
2.
Materials Required
- Computer or projector for presentation.
- C compiler (e.g., GCC, Turbo C, or Code::Blocks).
- Handouts/slides summarizing the if and if-else
statements with examples.
- Whiteboard and markers for explanation.
3.
Teaching Methodology
A. Introduction (5 minutes)
- Begin with a real-life analogy: "Imagine you
have a condition, like whether it's raining. If it is, you take an
umbrella, and if it's not, you leave it at home. This is similar to
decision-making in a program using if and if-else statements."
- Introduce conditional statements: These are used
to make decisions based on certain conditions. In C, the if and if-else
statements are used for this purpose.
B. Theoretical Explanation (10
minutes)
- The if Statement:
- The if statement is used to execute a block of code only if
a specified condition is true. If the condition is false, the block is
skipped.
- Syntax:
o
if
(condition) {
o
// Code to be executed if condition is true
o
}
- Example:
o
int
x = 10;
o
if
(x > 5) {
o
printf("x is greater than 5");
o
}
- If the condition is true, the code inside the block runs. If the condition is false,
the block is ignored.
- The if-else Statement:
- The if-else statement allows you to specify a block of code to
execute if the condition is true and another block if the condition is
false.
- Syntax:
o
if
(condition) {
o
// Code to be executed if condition is true
o
}
else {
o
// Code to be executed if condition is
false
o
}
- Example:
o
int
x = 3;
o
if
(x > 5) {
o
printf("x is greater than 5");
o
}
else {
o
printf("x is less than or equal to
5");
o
}
- The code inside the else block runs if the condition is false.
- Key Differences Between if
and if-else:
- if:
Only checks one condition and executes the block if the condition is
true. If false, the block is skipped.
- if-else:
Checks one condition, and if true, executes the first block; otherwise,
it executes the second block.
C. Practical Demonstration (20
minutes)
- Example Using if Statement
- Program that checks if a number is positive using an if
statement:
o
#include
<stdio.h>
o
o
int
main() {
o
int number = 7;
o
if (number > 0) {
o
printf("The number is
positive.\n");
o
}
o
return 0;
o
}
- Explanation: The message is printed only if the
condition number > 0 is true. If the condition is false, nothing is
printed.
- Example Using if-else Statement
- Program to check if a number is positive or negative
using if-else:
o
#include
<stdio.h>
o
o
int
main() {
o
int number = -5;
o
if (number > 0) {
o
printf("The number is
positive.\n");
o
} else {
o
printf("The number is negative or
zero.\n");
o
}
o
return 0;
o
}
- Explanation: The message will be printed based on
whether the number is positive or not.
- Comparison of if and if-else Statements
- Using if statement alone:
- Only one block of code is
executed if the condition is true.
- No action is taken if the
condition is false.
- Using if-else statement:
- One block is executed if the
condition is true and the other if it is false.
D. Conclusion (5 minutes)
- Recap the difference between if and if-else:
- if is
used for checking conditions where we only need to execute code for true
conditions.
- if-else
is used when we need to handle both true and false conditions with two
distinct actions.
- Emphasize the importance of using the right conditional
statement based on the needs of the program.
- Encourage students to experiment with both statements
in their own programs.
4.
Assessment
Oral Questions:
- What is the difference between if and if-else?
- When would you use an if statement rather than an if-else
statement?
- What will happen if there is no else
block in an if-else statement?
Activity:
- Ask students to write a program that:
- Takes an integer input from the user.
- Checks if the number is even or odd using if and
if-else.
Example:
·
if
(num % 2 == 0) {
·
printf("Even");
·
}
else {
·
printf("Odd");
·
}
Homework:
- Write a program that checks whether a number is
positive, negative, or zero using if-else.
- Modify the program to print "Positive",
"Negative", or "Zero" based on the input.
17. Lesson Plan: Nested if-else Statements in C
Grade: 10th
Duration: 40 minutes
1.
Objectives
By the end of this lesson, students
will be able to:
- Understand the concept and syntax of nested if-else
statements in C.
- Use nested if-else statements to handle multiple conditions.
- Write programs that demonstrate the usage of nested if-else
statements for decision-making.
2.
Materials Required
- Computer or projector for presentation.
- C compiler (e.g., GCC, Turbo C, or Code::Blocks).
- Handouts/slides explaining the syntax and usage of
nested if-else statements.
- Whiteboard and markers for explanation.
3.
Teaching Methodology
A. Introduction (5 minutes)
- Ask: "In real-life decision-making, sometimes
we need to check multiple conditions. For example, when checking if a
number is positive, negative, or zero, we need more than one condition.
How do we do this in a program?"
- Introduce the concept of nested if-else
statements: These are if-else
statements inside other if or else blocks, used for checking multiple conditions in a
hierarchy.
B. Theoretical Explanation (10
minutes)
- What are Nested if-else Statements?
- Nested if-else: An if or else statement inside another if or else
statement.
- Used when you need to check multiple conditions or
make more complex decisions.
- Syntax of Nested if-else:
- Syntax:
o
if
(condition1) {
o
if (condition2) {
o
// Block of code if both condition1 and
condition2 are true
o
} else {
o
// Block of code if condition1 is true
and condition2 is false
o
}
o
}
else {
o
// Block of code if condition1 is false
o
}
- Example of a Simple Nested if-else
Statement:
- Checking if a number is positive, negative, or zero
using nested if-else:
o
if
(number > 0) {
o
printf("The number is
positive.\n");
o
}
else {
o
if (number < 0) {
o
printf("The number is
negative.\n");
o
} else {
o
printf("The number is
zero.\n");
o
}
o
}
- In this example, we first check if the number is
positive. If it is not, we then check if it is negative; if neither
condition is true, it must be zero.
- Key Points to Remember:
- Always ensure proper indentation to maintain
readability.
- Avoid deeply nested if-else statements for better program structure.
- Nested if-else statements can be used for handling more than two
choices or conditions.
C. Practical Demonstration (20
minutes)
- Example Program: Checking a Grade Using Nested if-else
- Program to check a student's grade based on a score:
o
#include
<stdio.h>
o
o
int
main() {
o
int score;
o
printf("Enter your score: ");
o
scanf("%d", &score);
o
o
if (score >= 90) {
o
printf("Grade A\n");
o
} else {
o
if (score >= 80) {
o
printf("Grade B\n");
o
} else {
o
if (score >= 70) {
o
printf("Grade C\n");
o
} else {
o
if (score >= 60) {
o
printf("Grade
D\n");
o
} else {
o
printf("Grade
F\n");
o
}
o
}
o
}
o
}
o
return 0;
o
}
- Explanation: The program checks the score and assigns
a grade based on ranges using nested if-else.
- Using Nested if-else for Multiple Choices
- Program to check the eligibility for a scholarship:
o
#include
<stdio.h>
o
o
int
main() {
o
int age;
o
char grade;
o
printf("Enter your age: ");
o
scanf("%d", &age);
o
printf("Enter your grade: ");
o
scanf(" %c", &grade); // Note the space before %c to consume
newline
o
o
if (age >= 18) {
o
if (grade == 'A' || grade == 'B') {
o
printf("You are eligible for
the scholarship.\n");
o
} else {
o
printf("You are not eligible
for the scholarship.\n");
o
}
o
} else {
o
printf("You are not eligible for
the scholarship.\n");
o
}
o
return 0;
o
}
- Explanation: This program checks if the person is old
enough and has a good enough grade to be eligible for a scholarship using
nested conditions.
- Error Demonstration with Nested if-else
- Program with an error in nested conditions:
o
#include
<stdio.h>
o
o
int
main() {
o
int number = -5;
o
o
if (number > 0) {
o
if (number > 10) {
o
printf("Greater than
10\n");
o
} else {
o
printf("Between 0 and
10\n");
o
}
o
} else {
o
printf("Negative number\n");
o
}
o
o
return 0;
o
}
- Explanation: This program works as expected, but deep
nesting can make the logic harder to follow. Always try to avoid
excessive nesting.
D. Conclusion (5 minutes)
- Recap the purpose and syntax of nested if-else
statements.
- Discuss when to use nested if-else
(e.g., when dealing with multiple conditions that need to be checked in
sequence).
- Emphasize the importance of code readability and
managing deep nesting effectively to avoid complex, hard-to-read programs.
4.
Assessment
Oral Questions:
- What is the difference between a simple if
statement and a nested if-else statement?
- What would happen if there were no else part
in a nested if-else statement?
- Why should we avoid excessive nesting of if-else
statements?
Activity:
- Write a program that:
- Takes a number from the user.
- Checks if it is positive, negative, or zero, and then
checks if it is even or odd using nested if-else statements.
Example output:
·
Enter
a number: 5
·
The
number is positive.
·
The
number is odd.
Homework:
- Write a program that takes the age of a person and
determines if they are eligible for a driving license. The conditions are:
- The person must be at least 18 years old.
- The person must pass a test (yes/no input).
- If they are under 18, the program should print a
message indicating they are too young to drive.
18. Lesson Plan: General Syntax of Loops in C
Grade: 10th
Duration: 40 minutes
1.
Objectives
By the end of this lesson, students
will be able to:
- Understand the general syntax of loops in C
programming.
- Learn the differences between the three main types of
loops in C: for, while, and do-while.
- Write programs that demonstrate the use of loops to
repeat tasks.
2.
Materials Required
- Computer or projector for presentation.
- C compiler (e.g., GCC, Turbo C, or Code::Blocks).
- Handouts/slides summarizing loop structures and their
syntax.
- Whiteboard and markers for explanation.
3.
Teaching Methodology
A. Introduction (5 minutes)
- Start with a real-life analogy: "Have you ever
had to repeat a task, like brushing your teeth every day or going through
a list of items? In programming, loops help us repeat actions or tasks
based on certain conditions."
- Introduce the concept of loops: A loop allows a
block of code to be executed repeatedly based on a condition.
B. Theoretical Explanation (10
minutes)
- What is a Loop?
- A loop is a programming construct that repeats a block
of code until a specified condition is met.
- Loops help avoid writing repetitive code and make it
easier to handle repetitive tasks efficiently.
- Types of Loops in C:
- for
loop: Best when the number of iterations
is known beforehand.
- while
loop: Best when the number of
iterations is not known, but the loop runs while a condition is true.
- do-while
loop: Similar to the while
loop, but the condition is checked after the block of code is executed,
ensuring that the block runs at least once.
- General Syntax of Each Loop:
- for
loop:
- Used when the number of
iterations is known in advance.
o
for
(initialization; condition; update) {
o
// Block of code to be executed
o
}
Example:
for
(int i = 0; i < 5; i++) {
printf("i = %d\n", i);
}
- while
loop:
- Used when the loop needs to
execute as long as a condition is true.
o
while
(condition) {
o
// Block of code to be executed
o
}
Example:
int
i = 0;
while
(i < 5) {
printf("i = %d\n", i);
i++;
}
- do-while
loop:
- Used when the loop must
execute at least once, regardless of the condition.
o
do
{
o
// Block of code to be executed
o
}
while (condition);
Example:
int
i = 0;
do
{
printf("i = %d\n", i);
i++;
}
while (i < 5);
- Key Differences Between the Loops:
- for
loop is used when you know how
many times you want to repeat the action.
- while
loop is used when you want the
loop to repeat as long as a condition is true.
- do-while
loop ensures that the block of
code is executed at least once, even if the condition is false initially.
C. Practical Demonstration (20
minutes)
- Using a for loop to Print Numbers:
- Program to print numbers from 1 to 5 using a for
loop:
o
#include
<stdio.h>
o
o
int
main() {
o
for (int i = 1; i <= 5; i++) {
o
printf("%d\n", i);
o
}
o
return 0;
o
}
- Explain how the initialization (i = 1),
condition (i <= 5), and update (i++) work together.
- Using a while loop to Print Numbers:
- Program to print numbers from 1 to 5 using a while
loop:
o
#include
<stdio.h>
o
o
int
main() {
o
int i = 1;
o
while (i <= 5) {
o
printf("%d\n", i);
o
i++;
o
}
o
return 0;
o
}
- Show how the loop runs as long as the condition i <= 5
is true.
- Using a do-while loop to Print Numbers:
- Program to print numbers from 1 to 5 using a do-while
loop:
o
#include
<stdio.h>
o
o
int
main() {
o
int i = 1;
o
do {
o
printf("%d\n", i);
o
i++;
o
} while (i <= 5);
o
return 0;
o
}
- Emphasize that the block is executed at least once,
even if the condition is initially false.
- Demonstrating an Infinite Loop:
- Program that uses a while loop that runs indefinitely:
o
#include
<stdio.h>
o
o
int
main() {
o
while (1) {
// Condition is always true
o
printf("This is an infinite
loop\n");
o
}
o
return 0;
o
}
- Discuss how an infinite loop works and the importance
of ensuring that conditions eventually become false to avoid infinite
loops.
D. Conclusion (5 minutes)
- Recap the syntax and use cases of each loop type (for, while, and
do-while).
- Emphasize that choosing the right type of loop depends
on the problem: use for for a known number of iterations, while for
an unknown number, and do-while when you want the loop to execute at least once.
- Encourage students to practice using different types of
loops in their programs to strengthen their understanding.
4.
Assessment
Oral Questions:
- What is the difference between a for loop
and a while loop?
- When would you use a do-while loop instead of a while loop?
- What will happen if you forget to update the loop
variable in a for loop?
Activity:
- Write a program that prints the first 10 even numbers
using a loop.
(You can use any loop type discussed: for, while, or do-while.)
Homework:
- Write a program to calculate the factorial of a number
using a for loop.
- Research how to exit a loop prematurely using the break
statement.
19. Lesson Plan: Nested Loops in C
Grade: 10th
Duration: 40 minutes
1.
Objectives
By the end of this lesson, students
will be able to:
- Understand the concept of nested loops in C
programming.
- Learn how to use one loop inside another to solve
complex problems.
- Write programs that demonstrate the use of nested loops
for tasks like printing patterns and handling multidimensional arrays.
2.
Materials Required
- Computer or projector for presentation.
- C compiler (e.g., GCC, Turbo C, or Code::Blocks).
- Handouts/slides explaining nested loops with examples.
- Whiteboard and markers for explanation.
3.
Teaching Methodology
A. Introduction (5 minutes)
- Start with a question: "Have you ever needed to
repeat a task multiple times within another repeated task? For example,
printing a pattern like a triangle or table?"
- Introduce nested loops: A nested loop is a loop
inside another loop. The outer loop controls how many times the inner loop
runs.
- Example: "In a multiplication table, the outer
loop may represent the rows, and the inner loop will represent the
columns."
B. Theoretical Explanation (10
minutes)
- What Are Nested Loops?
- A nested loop is a loop inside another loop.
The inner loop will execute its entire set of iterations for each
iteration of the outer loop.
- Commonly used for tasks like printing patterns,
working with 2D arrays, and performing operations over multiple sets of
data.
- Syntax of Nested Loops:
- General syntax:
o
for
(initialization1; condition1; update1) {
// Outer loop
o
for (initialization2; condition2; update2)
{ // Inner loop
o
// Code to be executed in inner loop
o
}
o
// Code to be executed in outer loop
o
}
- Example of a nested for
loop:
o
for
(int i = 0; i < 3; i++) { // Outer
loop
o
for (int j = 0; j < 3; j++) { // Inner loop
o
printf("%d %d\n", i, j); // Print row and column indices
o
}
o
}
- Key Points to Remember:
- The inner loop executes completely for each iteration
of the outer loop.
- Both loops can have different conditions and ranges.
The outer loop runs based on its own condition, and for each outer loop
iteration, the inner loop runs based on its condition.
- Nesting can go multiple levels deep, but it's often
best to avoid deep nesting to maintain readability.
C. Practical Demonstration (20
minutes)
- Example 1: Printing a Multiplication Table Using Nested
Loops
- Program to print a multiplication table from 1 to 5:
o
#include
<stdio.h>
o
o
int
main() {
o
int i, j;
o
for (i = 1; i <= 5; i++) { // Outer loop for rows
o
for (j = 1; j <= 5; j++) { // Inner loop for columns
o
printf("%d\t", i *
j); // Multiplication result
o
}
o
printf("\n");
o
}
o
return 0;
o
}
- Explanation:
The outer loop runs 5 times (for rows), and the inner loop also runs 5
times (for columns). The result is a 5x5 multiplication table.
- Example 2: Printing a Number Pattern (Triangle)
- Program to print a right-angled triangle pattern:
o
#include
<stdio.h>
o
o
int
main() {
o
int i, j;
o
for (i = 1; i <= 5; i++) { // Outer loop for rows
o
for (j = 1; j <= i; j++) { // Inner loop for columns
o
printf("* "); // Print a star
o
}
o
printf("\n"); // Move to the next line after each row
o
}
o
return 0;
o
}
- Explanation:
The outer loop determines the number of rows, and the inner loop prints
stars in each row. The number of stars increases with each row, forming a
right-angled triangle pattern.
- Example 3: Printing a Rectangle Pattern
- Program to print a rectangle of stars:
o
#include
<stdio.h>
o
o
int
main() {
o
int i, j;
o
for (i = 1; i <= 4; i++) { // Outer loop for rows
o
for (j = 1; j <= 5; j++) { // Inner loop for columns
o
printf("* ");
o
}
o
printf("\n");
o
}
o
return 0;
o
}
- Explanation:
The outer loop runs 4 times (for rows), and the inner loop runs 5 times
(for columns), creating a 4x5 rectangle pattern of stars.
- Example 4: Working with 2D Arrays Using Nested Loops
- Program to print a 2D array:
o
#include
<stdio.h>
o
o
int
main() {
o
int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
o
for (int i = 0; i < 2; i++) { // Outer loop for rows
o
for (int j = 0; j < 3; j++) { // Inner loop for columns
o
printf("%d ",
arr[i][j]); // Access array elements
o
}
o
printf("\n");
o
}
o
return 0;
o
}
- Explanation:
The outer loop runs through the rows, and the inner loop runs through the
columns, accessing each element of the 2D array.
D. Conclusion (5 minutes)
- Recap the importance of nested loops in solving
problems that require multiple iterations (e.g., working with 2D arrays,
printing patterns).
- Emphasize the flexibility of nested loops, allowing for
complex repetitive tasks.
- Discuss the importance of managing readability when
using multiple levels of nesting and avoid over-complicating code.
4.
Assessment
Oral Questions:
- What is a nested loop, and when would you use it?
- What is the difference between a single loop and a
nested loop?
- How do nested loops help in printing patterns like
triangles and rectangles?
Activity:
- Write a program to print an inverted triangle pattern
using stars. For example:
·
* *
* * *
·
* *
* *
·
* *
*
·
* *
·
*
Homework:
- Write a program to print a multiplication table from 1
to 10 using nested loops.
- Research how nested loops are used in image processing
or games (e.g., 2D grids) and provide an example.
20. Lesson Plan: Arrays in C
Grade: 10th
Duration: 40 minutes
1.
Objectives
By the end of this lesson, students
will be able to:
- Understand what arrays are and how they work in C.
- Declare, initialize, and access elements in an array.
- Perform operations like traversing and modifying array
elements.
- Write programs that demonstrate the use of arrays in C.
2.
Materials Required
- Computer or projector for presentation.
- C compiler (e.g., GCC, Turbo C, or Code::Blocks).
- Handouts/slides summarizing array concepts and syntax.
- Whiteboard and markers for explanation.
3.
Teaching Methodology
A. Introduction (5 minutes)
- Start with a question: "Have you ever had to
store a list of items, like a shopping list or the names of all your
friends? How did you organize them?"
- Introduce arrays: Arrays are data structures in
C that store a fixed-size sequence of elements of the same type.
- Explain that arrays help in storing multiple values
under one variable name, making it easier to manage related data.
B. Theoretical Explanation (10
minutes)
- What is an Array?
- An array is a collection of variables of the
same data type stored in contiguous memory locations.
- Arrays are useful when we need to store multiple
values of the same type, like a list of numbers, names, or scores.
- Syntax of Array Declaration:
- Declaration:
o
data_type
array_name[array_size];
Example:
int
arr[5]; // Array of 5 integers
- The array_size is the number of elements that the array can hold. In
the example above, arr can store 5 integers.
- Array Indexing:
- Array elements are accessed using indices
(starting from 0).
- To access the first element of the array, use arr[0];
for the second element, use arr[1], and so on.
- Initializing Arrays:
- At the time of declaration:
o
int
arr[5] = {1, 2, 3, 4, 5};
- Partially initialized arrays:
o
int
arr[5] = {1, 2}; // Remaining elements
will be initialized to 0
- Without specifying the size:
o
int
arr[] = {1, 2, 3, 4, 5}; // Compiler
will calculate the size
- Accessing Array Elements:
- Example of accessing and printing array elements:
o
printf("%d",
arr[0]); // Prints the first element of
the array
C. Practical Demonstration (20
minutes)
- Declaring and Initializing an Array:
- Program to declare and initialize an array of
integers:
o
#include
<stdio.h>
o
o
int
main() {
o
int arr[5] = {10, 20, 30, 40, 50};
o
printf("First element: %d\n",
arr[0]);
o
printf("Last element: %d\n",
arr[4]);
o
return 0;
o
}
- Explanation:
The array arr contains 5 integers, and we access the first and last
elements.
- Traversing an Array (Using a Loop):
- Program to print all elements of the array using a
loop:
o
#include
<stdio.h>
o
o
int
main() {
o
int arr[5] = {10, 20, 30, 40, 50};
o
for (int i = 0; i < 5; i++) {
o
printf("Element at index %d:
%d\n", i, arr[i]);
o
}
o
return 0;
o
}
- Explanation:
The for loop iterates through the array, accessing each
element using the index i.
- Modifying Array Elements:
- Program to change the value of an array element:
o
#include
<stdio.h>
o
o
int
main() {
o
int arr[5] = {10, 20, 30, 40, 50};
o
arr[2] = 100; // Modify the 3rd element
o
printf("Modified 3rd element:
%d\n", arr[2]);
o
return 0;
o
}
- Explanation:
The third element of the array (index 2) is updated to 100.
- Array of Characters (String Example):
- Program to declare and initialize a character array
(string):
o
#include
<stdio.h>
o
o
int
main() {
o
char name[] = "John";
o
printf("Name: %s\n", name); // Prints the string "John"
o
return 0;
o
}
- Explanation:
The array name stores the string "John", and it is
accessed using the %s format specifier in printf.
D. Conclusion (5 minutes)
- Recap the key points:
- Arrays are used to store multiple values of the same
type.
- Arrays are indexed, with the first element at index 0.
- You can initialize, access, modify, and traverse
arrays easily using loops.
- Emphasize the importance of using arrays for tasks like
storing lists, tables, and collections of related data.
4.
Assessment
Oral Questions:
- What is the difference between a single variable and an
array?
- How do you access the third element of an array?
- Can you modify array elements after initializing them?
How?
Activity:
- Write a program to declare an array of 5 integers,
assign values to them, and then print the sum of all the elements.
Homework:
- Write a program that:
- Takes 5 integers as input from the user and stores
them in an array.
- Prints the largest number in the array.
- Research and explain the differences between
one-dimensional and two-dimensional arrays in C.
21. Lesson Plan: Types of Functions in C
Grade: 10th
Duration: 40 minutes
1.
Objectives
By the end of this lesson, students
will be able to:
- Understand the concept of functions in C programming.
- Learn the different types of functions in C: library
functions and user-defined functions.
- Understand how to define, declare, and call functions.
- Write programs demonstrating the usage of both types of
functions.
2.
Materials Required
- Computer or projector for presentation.
- C compiler (e.g., GCC, Turbo C, or Code::Blocks).
- Handouts/slides explaining the types of functions in C
with examples.
- Whiteboard and markers for explanation.
3.
Teaching Methodology
A. Introduction (5 minutes)
- Start by asking: "Have you ever used a
calculator? Do you know how it performs calculations behind the scenes?
Similarly, in programming, we use functions to perform specific tasks or
calculations."
- Introduce the concept of functions: Functions
are blocks of code that perform a specific task, and they can be reused
throughout the program.
B. Theoretical Explanation (10
minutes)
- What Are Functions?
- A function is a self-contained block of code
designed to accomplish a specific task.
- Functions allow for code reusability, easier
debugging, and more organized code.
- Types of Functions in C:
In C, functions are classified into two main types: - Library Functions: Predefined functions provided by C libraries (e.g., printf(),
scanf(), sqrt(), strlen()).
- User-Defined Functions: Functions defined by the programmer to perform a
specific task.
- Basic Structure of a Function:
- Function Declaration (Prototype):
- The declaration of the
function tells the compiler the function's name, return type, and
parameters.
o
return_type
function_name(parameter_list);
- Function Definition:
- The function body contains
the code that is executed when the function is called.
o
return_type
function_name(parameter_list) {
o
// Function body
o
}
- Function Call:
- A function is called when we
need it to perform its task.
o
function_name(arguments);
C. Practical Demonstration (20
minutes)
- Library Functions:
- Example 1: Using printf() (library function) to print output:
o
#include
<stdio.h>
o
o
int
main() {
o
printf("Hello, World!\n");
o
return 0;
o
}
- Explanation: printf()
is a library function used to print output to the console. It is
predefined and ready to use in the C standard library.
- Example 2: Using scanf() (library function) to get input from the user:
o
#include
<stdio.h>
o
o
int
main() {
o
int number;
o
printf("Enter a number: ");
o
scanf("%d", &number); // Using scanf() to read an integer
o
printf("You entered: %d\n",
number);
o
return 0;
o
}
- Explanation:
scanf() is used to read input from the user and store it in a
variable. It is also a library function.
- User-Defined Functions:
- Example 1: Simple user-defined function that adds two
numbers:
o
#include
<stdio.h>
o
o
//
Function Declaration (Prototype)
o
int
add(int, int); // Function that takes
two integers and returns an integer
o
o
int
main() {
o
int result = add(5, 7); // Function call
o
printf("The sum is: %d\n",
result);
o
return 0;
o
}
o
o
//
Function Definition
o
int
add(int a, int b) {
o
return a + b; // Add two numbers and return the result
o
}
- Explanation:
- add is
a user-defined function that takes two integer arguments, adds them, and
returns the sum.
- The function is called in main(),
and the result is stored in the variable result.
- Example 2: Function with no return value (void
function):
o
#include
<stdio.h>
o
o
//
Function Declaration
o
void
greet(); // Function that does not
return anything
o
o
int
main() {
o
greet();
// Function call
o
return 0;
o
}
o
o
//
Function Definition
o
void
greet() {
o
printf("Hello, Welcome to the world of
C programming!\n");
o
}
- Explanation:
- greet()
is a function that doesn't return anything (i.e., its return type is void).
- The function simply prints a
greeting message to the console.
- Function with Multiple Arguments:
- Example 3: Function that calculates the average of
three numbers:
o
#include
<stdio.h>
o
o
//
Function Declaration
o
float
average(int, int, int); // Function that
calculates the average of three numbers
o
o
int
main() {
o
int a = 10, b = 20, c = 30;
o
float result = average(a, b, c); // Function call
o
printf("The average is: %.2f\n",
result);
o
return 0;
o
}
o
o
//
Function Definition
o
float
average(int x, int y, int z) {
o
return (x + y + z) / 3.0;
o
}
- Explanation:
- average()
is a function that accepts three integer arguments and returns their
average.
- It is called in main(),
and the result is printed with a precision of two decimal places.
D. Conclusion (5 minutes)
- Recap the two types of functions in C:
- Library functions are pre-written and provided by the C language (e.g.,
printf(), scanf()).
- User-defined functions are created by the programmer to perform specific
tasks.
- Emphasize that functions help in modularizing the code,
making it more readable and reusable.
- Encourage students to practice creating and using
functions in their own programs.
4.
Assessment
Oral Questions:
- What is the purpose of a function in C programming?
- How do you declare and define a function?
- What is the difference between a function that returns
a value and a function that does not return a value?
Activity:
- Write a program that:
- Takes two numbers as input from the user.
- Defines a function to multiply the two numbers and
return the result.
- Prints the result.
Homework:
- Write a program that defines a user-defined function to
find the factorial of a given number.
- Research about function overloading (in C++) and
explain why it's not supported in C.
22. Lesson Plan: Structure of a Function in C
Grade: 10th
Duration: 40 minutes
1.
Objectives
By the end of this lesson, students
will be able to:
- Understand the general structure of a function in C.
- Learn the key components of a function (declaration,
definition, and call).
- Write and call functions correctly in C.
2.
Materials Required
- Computer or projector for presentation.
- C compiler (e.g., GCC, Turbo C, or Code::Blocks).
- Handouts/slides explaining the structure of a function
in C.
- Whiteboard and markers for explanation.
3.
Teaching Methodology
A. Introduction (5 minutes)
- Start with a real-life analogy: "Think of a
function like a recipe. A recipe tells you what ingredients to use, how to
combine them, and what the result should be. Similarly, a function in
programming tells the computer what inputs to take, how to process them,
and what output to give."
- Introduce the concept of a function in C: A
function is a block of code that performs a specific task, which can be
reused in a program.
B. Theoretical Explanation (10
minutes)
- What is a Function in C?
- A function in C is a block of code that performs a
specific task and is executed when called from another part of the
program.
- Functions help organize code, making it modular and
easier to read, debug, and maintain.
- Structure of a Function in C: A function in C consists of the following parts:
- Function Declaration (Prototype):
The declaration tells the compiler about the function's name, return type, and parameters (if any). It is optional but good practice to declare functions before they are used.
o
return_type
function_name(parameter_list);
Example:
int
add(int, int); // Declaration of a
function that takes two integers and returns an integer
- Function Definition:
The definition provides the actual implementation of the function. It contains the code that defines what the function does when called.
o
return_type
function_name(parameter_list) {
o
// Body of the function
o
}
Example:
int
add(int a, int b) {
return a + b;
}
- Function Call:
A function is called to execute the code within it. The call is made by specifying the function's name and passing any required arguments.
o
function_name(arguments);
Example:
int
result = add(5, 10); // Calls the add
function with 5 and 10 as arguments
- Key Components of a Function:
- Return Type:
Specifies the type of value the function will return (e.g., int, float, void).
- Function Name:
The name used to identify the function. It should be descriptive.
- Parameters (Optional): Variables passed into the function, allowing it to
operate on values provided by the caller.
- Function Body:
Contains the statements or instructions that define the function's
behavior.
- Return Statement (Optional): The return statement is used to send a value back to the
function caller. If the return type is void, no return statement is needed.
C. Practical Demonstration (20
minutes)
- Basic Function Structure:
- Program to define and call a simple function that adds
two numbers:
o
#include
<stdio.h>
o
o
//
Function Declaration
o
int
add(int, int);
o
o
int
main() {
o
int sum = add(3, 4); // Function Call
o
printf("The sum is: %d\n", sum);
o
return 0;
o
}
o
o
//
Function Definition
o
int
add(int a, int b) {
o
return a + b; // Return sum of a and b
o
}
- Explanation:
- The function add is
declared at the top and defined later. The main function calls add
with 3 and 4 as
arguments, and the result is printed.
- Function with No Return Value (void):
- Program to define a function that prints a message:
o
#include
<stdio.h>
o
o
//
Function Declaration
o
void
greet(void);
o
o
int
main() {
o
greet();
// Function Call
o
return 0;
o
}
o
o
//
Function Definition
o
void
greet(void) {
o
printf("Hello, Welcome to C
programming!\n");
o
}
- Explanation:
- The greet
function does not return a value (void). It simply prints a message
when called.
- Function with Multiple Parameters:
- Program to define a function that calculates the
average of three numbers:
o
#include
<stdio.h>
o
o
//
Function Declaration
o
float
average(int, int, int);
o
o
int
main() {
o
float avg = average(10, 20, 30); // Function Call
o
printf("The average is: %.2f\n",
avg);
o
return 0;
o
}
o
o
//
Function Definition
o
float
average(int x, int y, int z) {
o
return (x + y + z) / 3.0; // Return the average
o
}
- Explanation:
- The average
function takes three integers as parameters, calculates their average,
and returns the result.
- Using Return Statement to Exit a Function:
- Program to demonstrate early exit using return:
o
#include
<stdio.h>
o
o
//
Function Declaration
o
void
checkNumber(int);
o
o
int
main() {
o
checkNumber(10); // Function Call
o
checkNumber(-5); // Function Call
o
return 0;
o
}
o
o
//
Function Definition
o
void
checkNumber(int num) {
o
if (num < 0) {
o
printf("Negative number\n");
o
return;
// Exit the function early
o
}
o
printf("Positive number\n");
o
}
- Explanation:
- The checkNumber
function checks if the number is negative or positive. If negative, it
prints the message and exits early using return.
D. Conclusion (5 minutes)
- Recap the structure of a function: declaration, definition,
and function call.
- Emphasize that functions are essential for making
programs more modular and reusable.
- Encourage students to practice writing simple functions
and calling them from main().
4.
Assessment
Oral Questions:
- What is the purpose of the return
keyword in a function?
- What is the difference between int and void
return types in C functions?
- How do you pass arguments to a function?
Activity:
- Write a program that:
- Defines a function to calculate the square of a
number.
- Calls the function with different numbers and prints
the result.
Homework:
- Write a program that defines a function to check if a
given number is prime or not and returns 1 if it is prime and 0 otherwise.
- Research and write a brief explanation of function
overloading and its limitations in C (as compared to C++).
23. Lesson Plan: Defining a Function in C
Grade: 10th
Duration: 40 minutes
1.
Objectives
By the end of this lesson, students
will be able to:
- Understand the concept and structure of defining a
function in C.
- Learn the syntax for defining a function with or
without parameters and return values.
- Write programs that define functions to perform
specific tasks.
2.
Materials Required
- Computer or projector for presentation.
- C compiler (e.g., GCC, Turbo C, or Code::Blocks).
- Handouts/slides summarizing the structure of defining
functions in C.
- Whiteboard and markers for explanation.
3.
Teaching Methodology
A. Introduction (5 minutes)
- Start with a question: "What do you think would
happen if every time we wanted to print something in our program, we had
to write the same code over and over again?"
- Introduce the concept of defining a function: A
function is a block of code that performs a specific task and can be used
multiple times in a program without repeating the code.
- Explain that defining a function helps organize and
simplify the code.
B. Theoretical Explanation (10
minutes)
- What is a Function in C?
- A function is a block of code that performs a
specific task and is executed when called.
- Functions allow code reuse, reduce redundancy, and
make code easier to maintain.
- Basic Structure of Defining a Function:
- A function consists of three main parts:
- Return Type: The type of value the
function will return (e.g., int, float, void).
- Function Name: A descriptive name that
identifies the function.
- Function Body: A block of code enclosed in {}
that defines what the function does.
Syntax
of Function Definition:
return_type
function_name(parameter_list) {
// Body of the function
}
- Types of Functions in C:
- Function with a Return Value: A function that performs a task and returns a value
to the caller.
- Function without a Return Value (void): A function that performs a task but does not return
any value to the caller.
- Example of a Function with a Return Value:
5. int add(int a, int b) {
6. return a + b; // Adds two numbers and returns the result
7. }
- Explanation:
- int is
the return type, indicating the function returns an integer.
- a
and b are parameters, which are
the inputs to the function.
- The function returns the sum
of a and b.
- Example of a Function with No Return Value (void):
9. void greet() {
10. printf("Hello, World!\n");
11.}
- Explanation:
- void
indicates that the function does not return any value.
- The function simply prints a
greeting message when called.
C. Practical Demonstration (20
minutes)
- Defining and Using a Simple Function:
- Program to define a function to add two numbers:
o
#include
<stdio.h>
o
o
//
Function Declaration
o
int
add(int, int);
o
o
int
main() {
o
int result = add(5, 7); // Function Call
o
printf("The sum is: %d\n",
result);
o
return 0;
o
}
o
o
//
Function Definition
o
int
add(int a, int b) {
o
return a + b; // Return the sum of a and b
o
}
- Explanation:
- The function add is
defined to take two integers as parameters and return their sum.
- It is called in the main
function with arguments 5 and 7,
and the result is printed.
- Defining a Function with void
(No Return Value):
- Program to define a function that prints a greeting:
o
#include
<stdio.h>
o
o
//
Function Declaration
o
void
greet();
o
o
int
main() {
o
greet();
// Function Call
o
return 0;
o
}
o
o
//
Function Definition
o
void
greet() {
o
printf("Hello, Welcome to C
programming!\n");
o
}
- Explanation:
- greet()
is a function that does not return anything (void).
- It prints a message to the
console when called.
- Function with Multiple Parameters:
- Program to define a function to calculate the average
of three numbers:
o
#include
<stdio.h>
o
o
//
Function Declaration
o
float
average(int, int, int);
o
o
int
main() {
o
float avg = average(10, 20, 30); // Function Call
o
printf("The average is: %.2f\n",
avg);
o
return 0;
o
}
o
o
//
Function Definition
o
float
average(int x, int y, int z) {
o
return (x + y + z) / 3.0; // Return the average
o
}
- Explanation:
- The average
function takes three integers as parameters and returns their average.
- The result is printed in main().
- Using Return Statement in a Function:
- Program to demonstrate the return statement in a
function:
o
#include
<stdio.h>
o
o
//
Function Declaration
o
int
square(int);
o
o
int
main() {
o
int result = square(4); // Function Call
o
printf("The square is: %d\n",
result);
o
return 0;
o
}
o
o
//
Function Definition
o
int
square(int num) {
o
return num * num; // Return the square of num
o
}
- Explanation:
- The square
function returns the square of a given number, which is then printed in main().
D. Conclusion (5 minutes)
- Recap the structure of a function: declaration, definition,
and function call.
- Emphasize that defining functions helps in organizing
code, making it more readable and modular.
- Encourage students to create and experiment with
different types of functions in their programs.
4.
Assessment
Oral Questions:
- What are the key components of a function in C?
- How does a function with a void
return type differ from a function with an int return type?
- How do you pass values to a function?
Activity:
- Write a program that:
- Defines a function to calculate the area of a
rectangle.
- Calls the function with length and width as parameters
and prints the result.
Homework:
- Write a program that defines a function to check if a
number is prime. The function should return 1 if the number is prime and 0 otherwise.
- Research how recursive functions work and
provide an example.
24. Lesson Plan: Calling a Function in C
Grade: 10th
Duration: 40 minutes
1.
Objectives
By the end of this lesson, students
will be able to:
- Understand the concept and syntax of calling a function
in C.
- Learn how to pass arguments to a function when calling
it.
- Call functions from different parts of a program and
understand how the flow of control works.
- Write programs that demonstrate how to call functions
with and without arguments.
2.
Materials Required
- Computer or projector for presentation.
- C compiler (e.g., GCC, Turbo C, or Code::Blocks).
- Handouts/slides summarizing the syntax of function
calls.
- Whiteboard and markers for explanation.
3.
Teaching Methodology
A. Introduction (5 minutes)
- Start with a real-life analogy: "Imagine you
have a function like a tool in a toolbox. When you need the tool, you call
it to do a specific task, such as fixing a leaky pipe. Similarly, when you
need a specific task performed in your program, you call a function."
- Introduce calling a function: Calling a function
refers to invoking it to perform a task. Functions are called from the main()
function or any other function in the program.
B. Theoretical Explanation (10 minutes)
- What Does It Mean to Call a Function?
- When you call a function, you are instructing the
program to jump to the function's body and execute the code inside it.
- Once the function finishes its task, the program
returns to the point where the function was called and continues
execution.
- Syntax of Calling a Function:
- The function call is done by writing the
function's name followed by parentheses containing any necessary
arguments.
3. function_name(arguments);
- Example:
4. int sum = add(5, 7); // Calls the 'add' function with 5 and 7 as
arguments
- When calling a function, the arguments are passed, and
the function performs its task, returning the result if applicable.
- Calling Functions with Arguments:
- Arguments (parameters) are values passed into a
function to give it data to work with.
- Example:
6. void greet(char name[]) {
7. printf("Hello, %s!\n",
name); // Greets the person with the
given name
8. }
9.
10.int
main() {
11. greet("John"); // Calls the greet function with
"John" as an argument
12. return 0;
13.}
- Calling Functions with Return Values:
- Functions can return a value, which is used in the
calling code.
- Example:
15.int
add(int a, int b) {
16. return a + b; // Adds a and b and returns the result
17.}
18.
19.int
main() {
20. int result = add(3, 4); // Calls the add function and stores the
result
21. printf("Sum: %d\n", result);
22. return 0;
23.}
C. Practical Demonstration (20
minutes)
- Calling a Function Without Arguments (No Return Value):
- Program to define and call a function that prints a
greeting:
o
#include
<stdio.h>
o
o
//
Function Declaration
o
void
greet();
o
o
int
main() {
o
greet();
// Function call
o
return 0;
o
}
o
o
//
Function Definition
o
void
greet() {
o
printf("Hello, Welcome to the C
Programming World!\n");
o
}
- Explanation:
- greet()
is a function that prints a greeting message. It doesn't take any
arguments and doesn't return any value.
- Calling a Function with Arguments:
- Program to call a function that adds two numbers:
o
#include
<stdio.h>
o
o
//
Function Declaration
o
int
add(int, int);
o
o
int
main() {
o
int sum = add(5, 7); // Function call with arguments 5 and 7
o
printf("The sum is: %d\n", sum);
o
return 0;
o
}
o
o
//
Function Definition
o
int
add(int a, int b) {
o
return a + b; // Returns the sum of a and b
o
}
- Explanation:
- add() is
called with arguments 5 and 7.
The function computes the sum and returns the result, which is then
printed in main().
- Calling a Function with Multiple Arguments:
- Program to call a function that calculates the average
of three numbers:
o
#include
<stdio.h>
o
o
//
Function Declaration
o
float
average(int, int, int);
o
o
int
main() {
o
float avg = average(10, 20, 30); // Function call with three arguments
o
printf("The average is: %.2f\n",
avg);
o
return 0;
o
}
o
o
//
Function Definition
o
float
average(int x, int y, int z) {
o
return (x + y + z) / 3.0; // Returns the average of x, y, and z
o
}
- Explanation:
- The average()
function is called with three arguments: 10, 20,
and 30. It calculates their average
and returns the result.
- Function Returning a Value:
- Program to call a function that returns a calculated
value:
o
#include
<stdio.h>
o
o
//
Function Declaration
o
int
square(int);
o
o
int
main() {
o
int result = square(4); // Function call with 4 as argument
o
printf("The square of 4 is:
%d\n", result);
o
return 0;
o
}
o
o
//
Function Definition
o
int
square(int num) {
o
return num * num; // Returns the square of num
o
}
- Explanation:
- square()
is called with the argument 4, and it returns the square
of the number. The result is stored in result and printed.
D. Conclusion (5 minutes)
- Recap the concept of calling a function and its
importance in making code reusable and modular.
- Emphasize the following:
- Functions can be called with or without arguments.
- Functions can return a value, which can be used
further in the program.
- Calling functions helps in breaking down tasks and
organizing code logically.
4.
Assessment
Oral Questions:
- What is the purpose of calling a function in C?
- How do you pass arguments to a function?
- What happens when you call a function that returns a
value?
Activity:
- Write a program that:
- Defines a function to calculate the area of a
rectangle (length × width).
- Calls the function with values for length and width,
and prints the area.
Homework:
- Write a program that defines a function to check if a
given number is prime or not. The function should return 1 if the number is prime and 0 otherwise.
- Research how functions are called in other programming
languages (like Python or Java) and compare them to how it is done in C.
Comments
Post a Comment