0% found this document useful (0 votes)
2 views30 pages

C Programming Made Easy - Ferdh - 20250503 - 165710 - 0000

The document is a comprehensive guide to C programming, covering essential topics such as data types, variables, input/output, operators, control structures, loops, and practical exercises. It includes modules with detailed explanations, examples, and practice questions to reinforce learning. Additionally, it provides real-life applications and coding challenges to help students apply their knowledge effectively.

Uploaded by

GAMER72
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views30 pages

C Programming Made Easy - Ferdh - 20250503 - 165710 - 0000

The document is a comprehensive guide to C programming, covering essential topics such as data types, variables, input/output, operators, control structures, loops, and practical exercises. It includes modules with detailed explanations, examples, and practice questions to reinforce learning. Additionally, it provides real-life applications and coding challenges to help students apply their knowledge effectively.

Uploaded by

GAMER72
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

C PROGRAMMING

By FerdH

MADE EASY
LET'S EXPLORE TECHNOLOGY
TOGETHER TO LIVE IN THE FUTURE

1
Module 1: Data types & variables Page 5
Introduction to data types: int, float, double,
char, char[], bool
Declaring and initializing variables
Constants and identifiers
Real-life use of variables (e.g., budgeting,
grading systems)

Module 2: Input and Output Page 8


Using printf() and scanf() effectively
Format specifiers: %d, %f, %lf, %c, %s
Real-life input/output.

Module 3: Operators in C Page 11


Arithmetic operators: +, -, *, /, %
Comparison operators: ==, !=, >, <, >=, <=
Logical operators: &&, ||, !
Assignment and compound operators: =, +=, -=, etc.
Increment & Decrement: ++, -- (pre/post)
Exercises after each section

2
Module 4: Control structures Page 21
if, else, else if statements
Nested if statements
switch statements and break
Real-life projects: Grade evaluation,
login system

Module 5: Loops (for, while, do...while) Page 25


for loop: structure, flow, use cases
while loop: condition-first structure, examples
do...while: guaranteed one-time execution

Module 6: Real-Life Practice with Loops Page 28


Projects involving repetition:
ATM simulator (pin attempts)
Guessing game
Printing patterns with nested loops
Repeated menu interface
Breakdown with full code, line-by-line
explanation

3
Module 7: Exam Past Questions (Theory + Coding)
Easy: Page 31
Add two numbers
Even/odd checker
Use of if and scanf

Intermediate:
Largest of 3 numbers
for, while, and do...while applications
Factorials and summation

Advanced:
Login system
PIN retry system
Pattern printing

All questions include:


Code walkthrough
Explanation
Expected output

Module 8: Final Exam Practice


10–15 coding-only questions (no solutions
provided)
Students can contact to access answers or
join paid group
Builds independence and reinforces
concepts

4
What are data types?
Data types tell the computer what kind of
value a variable holds

They help manage memory and operations


on data

Examples: int, float, double, char, etc.

Common data types in C


Data Types Description Example

int teger number int age = 20;

float Decimal number float price = 3.99;

double Precise decimal double pi = 3.14159;

char Single character char grade = 'A';

char[] String of characters char name[] = "Sam";

5
Declaring variables
Syntax: data__type variable_name = value
Example:

int score = 100;


float weight = 72.5;
char grade = 'B';

Rules for naming variables


Must start with a letter or underscore
Cannot use C keywords like int, while,
etc.
Case-sensitive: Age and age are different
Use meaningful names: studentAge, not
just a

Constants in C
A constant value never changes
Declared using const keyword
const float PI = 3.14;

6
Real-life use of variables
Budget calculator: int total = income -
expenses;
Grading system: char grade = 'A';
Temperature sensor: float temp = 37.5;

Practice Questions
Declare an integer to store your age
Create a float variable to store product price
Use a char to represent your grade
Challenge: Store your name using char[]

7
Input and Output in C
Learn how to interact with users using
printf() and scanf()

Output Using printf()


Used to display messages or values

Example:

printf("Your age is %d", &age);

%d is a format specifier (for integers)

Input using scanf()


Used to take input from user
Example:

scanf("%d", &age);

& means "address of" — it stores input into a


variable

8
Format specifiers in C
Specifier Used For Example

%d Integers int age = 20;

%f Float float price = 3.98;

%lf Double double pi = 3.14159;

%c Character char grade = 'A';

%s String char name[] = "Sam";

Example: Basic I/O


#include <stdio.h>

int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
printf("You are %d years old",
age);
return 0;
}

9
Explanation
Asks user to input age
Stores it in variable
Prints

Additional Practice Questions


1. Take two integers as input and print their sum and
difference
Example:
Input: 5 and 3
Output: Sum = 8, Difference = 2

2. Input a user's name and age, then print a message like:


Hello Sarah, you are 19 years old.

3. Write a program to input marks for 3 subjects and print


the total and average.

4. Ask user to input temperature in Celsius, print the same


in Fahrenheit
Hint formula: F = C * 9/5 + 32

5. Write a program that takes an integer and a float as input


and displays both.

6. Create a program to display a shopping receipt:


Inputs: item name, quantity, price per item
Output: item name, quantity, total price

10
Operators in C
Learn how to perform calculations and
comparisons in your programs

What Are Operators?


Operators are symbols used to perform
operations on variables and values

Examples: +, -, *, /, ==, !=, etc.

Arithmetic Operators
Operator Meaning Example

+ Addition a+b

- Subtraction a-b

* Multiplication a*b

/ Division a/b

% Modulus (remainder) a%b

11
Logical Operators in C
Programming
Content:
Understand how to make decisions in a
code using &&, ||, and !
Logical operators are used to combine or
reverse conditions in decision-making (if,
while, e.t.c)

Types:
AND (&&)
OR (||)
NOT (!)
Logical AND (&&)
Syntax:
condition1 and condition2
Explanation:
Returns true only if both conditions are true

int age = 25;


if (age > 18 && age < 30) {
printf("You are a young adult.");
}
output: // You are a young adult.
12
Logical OR (||)
Syntax:
condition1 || condition2
Explanation:
Returns true if at least one condition is true

int temp = 40;


if (temp < 0 || temp > 35) {
printf("Extreme weather!");
}
output: Extreme weather!

Logical NOT (!)


Syntax:
!condition1
Explanation:
Reverses the result of the condition. If true,
false. If false, true

int loggedIn = 0;
if (!loggedIn) {
printf("Please log in.");
}
output: Please log in.
13
Truth Table

A B A && B A || B !A

0 0 0 0 1

0 1 0 1 1

1 0 0 1 0

1 1 1 1 0

Great question!

In C programming, 0 and 1 are used to


represent Boolean logic (true or false):

14
Equality operators in C
programming
Content:
Used to compare values and check if they
are the same or different
Equality operators are used to compare two
values or.expressions in the conditions (like if
statements)
Main operators:
== Equal to
!= Not Equal to

Equal to (==)
Syntax:
a == b
Explanation:
Checks if the value of a is equal to the value
of b
Returns 1 (true), if equal. 0 (false), if not
int x = 5;
if (x == 5) {
printf("x is equal to 5");
}
output: x is equal to 5 15
Not Equal to (!=)
Syntax:
a != b
Explanation:
checks if the value of b is not equal to
the value of a
Returns 1 (true) if the value is not equal
to. 0 (false) if equal

int y = 10;
if (y != 5) {
printf("y is not 5");
}
output: y is not 5

Common mistake warning


Don't confuse == with !=
= is for assignment (sets a value)
== Is for comparison
Wrong:

if (x = 5) // This sets x to 5!

Correct:

if (x == 5) // This compares x with 5

16
Relational (comparison) operators
Operator Meaning Example

== Equal to a == b;

!= Not Equal to a != b;

> Greater than a > b;

< Lesser than a < b;

>= Greater than or equal to a >= b;

<= Lesser than or Equal to a <= b;

Assignment Operators
Operator Meaning Example

= Assign value a = b;

+= Add and assign a += b;

-= Subtract and assign a -= b;

*= Multiply and assign a *= b;

17
Increment & Decrement
Operators in C

Content:
++ increases the value by 1
-- decreases the value by 1

int x = 5;
x++; // x is now 6
x--; // x goes back to 5

Pre-Increment VS Post-Increment
++x increasing before using
x++ increasing before using

int a = 5;
int b = ++a; // a = 6, b = 6
int c = a++; // c = 6, a = 7

18
Step by step explanation

int a = 5;
int b = ++a; // a = 6, b = 6
int c = a++; // c = 6, a = 7

1. int a = 5; 3. int c = a++;


This declares an integer variable a Here, a++ is a post-increment
and assigns it the value 5. operation.

2. int b = ++a; Post-increment means: first use


Here, ++a is a pre-increment the value of a, then increment it.
operation. So:

Pre-increment means: first c is assigned the current value of a,


increment a, then use it. which is 6.
So:
a becomes 6. Then, a is incremented to 7.

Then, b is assigned the value of a, Result: c = 6, a = 7.


which is now 6.
Final values:
Result: a = 6, b = 6. a=7
b=6
c=6

19
Real-Life Use Cases
Calculating grades, prices, tax
Comparing ages or scores
Checking if user input meets conditions
Adding items in shopping cart

Practice Questions
1. Write a program to calculate the sum,
difference, product and quotient of two
numbers.

2. Take two numbers and check which is


greater using relational operators.

3. Input a number and check if it's odd or


even using %.
4. Check if a student passed using logical
operator: marks > 40 and < 100.

5. Demonstrate post-increment and pre-


increment in a simple program.

20
Control statement in C
Making decisions in programs
What are control statements in C?
They allow your program to make decisions and
control the flow of execution based on
conditions.
Mainly used: if, else, else if

The if statement
if (condition) {
// code runs if condition is
true
}

Example:

if (age >= 18) {


printf("You can vote");
}

21
The if...else statements

if (condition) {
// runs if true
} else {
// runs if false
}

Example:

if (score >= 50) {


printf("Pass");
} else {
printf("Fail");
}

The else if Ladder

if (mark >= 70) {


printf("A");
} else if (mark >= 60) {
printf("B");
} else {
printf("Fail");
}
22
Nested if statements
You can write an if inside another if for
more complex checks.

if (a > 0) {
if (a % 2 == 0) {
printf("Positive
Even");
}
}

Real-life examples
Voting eligibility
Grading systems
ATM PIN validation
Login/password checks

Common Mistakes to Avoid


Missing { } for multiple lines
Using = instead of ==
Forgetting to end statements with ;

23
Practice Questions
1. Write a program that checks if a number
is positive or negative.

2. Ask for a user's age and tell if they are a


child, teenager, or adult.

3. Write a grading system using else if.

4. Input two numbers and print the larger


number.

5. Check if a number is even or odd using if-


else.

24
Loops in C
Repeating tasks efficiently

What are loops?


Loops allow a block of code to run multiple
times until a condition is false.

The while loop

while (condition) {
// code runs while condition
is true
}

Example:

int i = 1;
while (i <= 5) {
printf("%d\n", i);
i++;
}

25
The do-while loop

do {
//code runs at least once
}while (condition)

Example:

int I = 1;
do {
printf("%d\n", i);
. i++;
} while(i <= 5);

Real-Life Examples of Loops


Display menu until user chooses “Exit”
Keep asking for password until correct
Countdown timers
Printing repeated patterns

26
Common Mistakes to Avoid
Infinite loop: forgetting to update the variable
Wrong condition that never becomes false
Misplaced ; after loop declaration

Practice Questions
1. Print numbers from 1 to 10 using a while loop.
2. Use a do-while loop to ask user for a password
until it’s correct.
3. Write a program that prints the multiplication
table of a number.
4. Print even numbers from 1 to 20.
5. Sum the first 10 natural numbers using while
loop.

27
The for loop in C

A for loop is used when the number of iterations is known


in advance.
Syntax includes initialization, condition, and
increment/decrement in one line.

Syntax of for loop

for (initialization; condition;


update) {
// code runs in here
}

Example:

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


printf("%d/n", i)
}

How it Works (Step-by-Step)


1. Initialize i = 1
2. Check condition i <= 5
3. Execute code block
4. Update i++
5. Repeat until condition is false
28
Use cases of for loops
1. Printing number series
2. Multiplication tables
3. Pattern printing
4. Running fixed number of tests

Nested for Loops


One for loop inside another
Example:

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


for (int j = 1; j <= 2;
j++) {
printf("%d %d\n", i, j);
}
}

Common mistakes
1. Off-by-one errors (looping too much or
too little)
2. Using wrong condition
3. Infinite loop by forgetting update

29
Practice Questions

1. Print numbers from 10 to 1 using a


for loop.
2. Display the squares of numbers 1
to 5.
3. Print even numbers from 2 to 20.
4. Use nested loops to print a
rectangle of *.
5. Calculate the factorial of a
number using a for loop.

BUY ME

9038248481
moniepoint

30

You might also like