Problem solving through programming
Unit-1
Shalini Joseph ,ISE 1
Shalini Joseph ,ISE 2
Shalini Joseph ,ISE
3
Shalini Joseph ,ISE
4
Introduction
C was developed in the early 1970s by Dennis
Ritchie at Bell Laboratories.
A high-level programming language.
Small size. C has only 32 keywords. This makes it
relatively easy to learn.
C is portable: Software written for one computer
can run on another
C is well suited for structured programming.
Shalini Joseph ,ISE 5
Main Uses of C
1. System Programming
Writing operating systems (e.g., Unix, Linux kernel is written in C)
2. Embedded Systems
C is used to program microcontrollers and IoT devices
Common in automotive, consumer electronics, robotics
3. Developing Compilers and Interpreters
Many modern compilers (for C, C++, Python) are written in C
4. Operating Systems
Most OS kernels, including parts of Windows, Linux, and macOS,
are in C.
Shalini Joseph ,ISE 6
Computer
A computer is an
electronic device that
manipulates
information, or data.
It has the ability to
store, retrieve, and
process data.
Shalini Joseph ,ISE 7
You are explaining how data flows in a
computer system to a beginner. Using the
block diagram of a computer, explain how
input data is processed and displayed as
output.
Input Devices:
Central Processing Unit (CPU):
Central Processing Unit (CPU):
Memory/Storage:
Output Devices:
Shalini Joseph ,ISE 8
Introductory Concepts
INPUT UNIT
Major functions of the input unit:
Data is processed by the user.
Machine-readable form
Transmit converted data into main memory
Shalini Joseph ,ISE 9
Introductory Concepts
Brain of computer CPU
Controls operations
Core of any computer devices.
A program is a set of instructions.
Ex: Sending file to printer, opening browser,
playing music or video.
Shalini Joseph ,ISE 10
Introductory Concepts
Control Unit
Controller of all the tasks and operations.
Instructions are converted to control signals.
Prioritizing and scheduling activities.
Coordinates the tasks by sync with I/O
Shalini Joseph ,ISE 11
Introductory Concepts
Memory Unit
Processed data is stored in memory unit.
Acts as hub of all data.
Faster accessing and processing.
There are two types:
Primary memory
Secondary memory
Shalini Joseph ,ISE 12
Introductory Concepts
Memory Unit
Primary memory:
Cannot store vast amount of data.
Used to store recent data
Temporary Storage Device
Known as main memory / volatile memory
Ex: RAM, ROM, Cache..etc
Shalini Joseph ,ISE 13
Introductory Concepts
Memory Unit
Secondary memory:
Permanent / auxiliary memory.
Data does not get erased easily.
Ex: Hard disk , SSD, USB, Floppy Disk, CD, DVD..etc.
Shalini Joseph ,ISE 14
Introductory Concepts
ALU
Performs the computers data processing functions.
Mathematical calculations or arithmetic
operations are performed.
Comparison of data and decision-making actions.
Shalini Joseph ,ISE 15
Introductory Concepts
Output Unit
Processed data is received by the user
Form of soft copy or hard copy
Converts it into a readable form
Devices like printers, monitors,
projectors ..etc
Shalini Joseph ,ISE 16
How to develop a
program?
1. Understand the problem.
2. Plan the logic (write algorithms and draw flowcharts to understand the logic).
3. Code the program (Select a suitable language and platform).
4. Use software to translate the program into machine language.
5. Test the program.
6. Put the program into production.
7. Maintain the program.
Shalini Joseph ,ISE 17
Shalini Joseph ,ISE 18
Algorithm
Step by Step Description
Blueprint to write program
Solving complex problems efficiently and
effectively.
Shalini Joseph ,ISE 19
Algorithm 1: Add two numbers entered by
the user
Step 1: Start
Step 2: Declare variables num1, num2 and
sum.
Step 3: Read values num1 and num2.
Step 4: Add num1 and num2 and assign the
result to sum.
sum←num1+num2
Step 5: Display sum
Step 6: Stop
Shalini Joseph ,ISE 20
Algorithm to Find Average of Three
Integers
Step 1: Start
Step 2: Input three integers: A, B, C
Step 3: Compute sum = A + B + C
Step 4: Compute average = sum / 3
Step 5: Output the average
Step 6: End
Shalini Joseph ,ISE 21
Design an algorithm to find the
largest of three integers
Step 1: Start
Step 2: Input three integers: A, B, and C
Step 3:
If A > B and A > C, then
A is the largest
Else if B > C, then
B is the largest
Else
C is the largest
Step 4: Output the largest number
Step 5: End
Shalini Joseph ,ISE 22
Flowchart
A flowchart is a visual diagram that shows the
steps of a process or algorithm using symbols
and arrows.
Shalini Joseph ,ISE 23
Shalini Joseph ,ISE 24
Shalini Joseph ,ISE 25
Shalini Joseph ,ISE 26
Shalini Joseph ,ISE 27
Shalini Joseph ,ISE 28
A student is struggling to visualize how their code
will run. Draw a flowchart to find the largest of
three numbers provided by the user.
Shalini Joseph ,ISE 29
Flow chart to check if an integer
input is a positive or negative
integer
Shalini Joseph ,ISE 30
Design a flow chart to print the sum
of first n positive integers
Shalini Joseph ,ISE 31
Structure of C program
Shalini Joseph ,ISE 32
Your First C Program
// This is my first program in C
#include<stdio.h>
int main()
{
printf("\n Welcome to the world of C ");
return 0;
}
Shalini Joseph ,ISE 33
stdio.h stands for Standard Input Output Header.
It is a header file in C standard library that
provides functions for input and output (I/O).
main() is the entry point of every C program.
Execution of a C program always starts from
main().
It is a function that can return a value to the
operating system.
int means the function returns an integer value.
returning 0 means the program executed
successfully
Shalini Joseph ,ISE 34
Character Set(Alphabets of C)
A symbols that is used while writing a program is
called a character .
A character can be letter, digit or any special symbol.
Eg:
Letters: lowercase a to z ,upper case letters A to Z
Digits: 0 to 9
Symbols:#,(,),% etc
White space:\n(new line),\t(tab)
Shalini Joseph ,ISE 35
C Tokens
A token is a smallest or basic unit of a c program.
One or more characters are grouped in sequence to form
meaningful words.
These meaningful words are called tokens.
Shalini Joseph ,ISE 36
Keywords
Reserve words are called keyword.
They have standard predefined meanings in C
C has 32 keywords
Shalini Joseph ,ISE 37
Identifiers
Identifiers are names given to program elements such as
variables, arrays and functions.
Rules for forming identifier name
Can contain letters (A-Z, a-z), digits (0-9), and
underscores ( _ )
Must begin with a letter or underscore, NOT a digit
Can not be a keyword(int,for)
No special character like @,%
C is case sensitive(COUNT not equal to count)
Shalini Joseph ,ISE 38
Shalini Joseph ,ISE 39
Constants
Constant is a fixed value that can not be altered
during the execution of a program.
4 types of Constants
1. Integer
2. Floating-point
3. Character
4. String
Shalini Joseph ,ISE 40
Integer constant
Is an integer Valued Number.
It refers to a sequence of digits. Integers are of
three types viz:
Example: Hexadecimal, Decimal, Octal
15, -265, 0, 99818, +25, 045, 0X6
41
Shalini Joseph ,ISE
Floating-Point Constants in C
A floating-point constant is a numeric constant that
has either a fractional form or an exponent form.
A floating-point constant can be written in decimal
or exponential notation.
• Decimal floating-point .
Example: 3.14, 6.022, 6.0, etc.
• Exponential floating-point
• Example: 6.022e23, etc.
Shalini Joseph ,ISE 42
Character Constants & String Constants
in C
Character constants are used to represent individual
characters.
Example: ‘A’, ‘5’, etc.
String constants are used to represent a sequence of
characters.
Example: "Hello World", "123", etc.
Shalini Joseph ,ISE 43
How to declare Constants in C
Constants in C are declared in mainly two ways. The
two ways are:
Using const Keyword
Using #define Preprocessor Directive
Shalini Joseph ,ISE 44
Declaring Constants in C using const Keyword
#include <stdio.h>
int main() {
const int a=20;
printf("%d", a);
return 0;
}
Shalini Joseph ,ISE 45
Declaring Constants in C using #define
#include <stdio.h>
#define PI 3.14
int main() {
printf("%f", PI);
return 0;
}
Shalini Joseph ,ISE 46
Types of Constants in C
Type of Constants Data type Example of Data type
Integer constants
int
23, 738, -1278, etc. unsigned int 2000u, 5000U, etc.
long int, long long 325,647 1,245,473,940
int
Floating-point or Real
constants
doule float 20.987654
500.987654321
Octal constant int Example: 013 /*starts with 0
*/
Hexadecimal constant int Example: 0x90 /*starts with
0x*/
character constants char Example: ‘X’, ‘Y’, ‘Z’
string constants char Example: “PQRS”, “ABCD”
Shalini Joseph ,ISE 47
Variables
Variable is an identifier that is used to represent a single
data items
Variable is a name given to a memory location where the
data can be stored.
Declaration: A variable must be declared before
use.
data_type variable_name;
int age;
float salary;
char grade;
Shalini Joseph ,ISE 48
Variable Initialization
Initialization: Variables can be assigned a value during or
after declaration.
int age = 25;
Scope: Variables can have different scopes:
Local: Declared inside a function and accessible only
within it.
Global: Declared outside all functions and accessible
throughout the program.
Shalini Joseph ,ISE 49
Rules In Naming a Variable
It should use only alphabets, number and
underscore ( _ )
– It should not begin with a number and must
have at least one alphabet.
– It should not be a reserved word.
Shalini Joseph ,ISE 50
Data Type
Data types define the type and size of data a
variable can hold.
Shalini Joseph ,ISE 51
Size (Typical Format
Data Type Description Example
32-bit) Specifier
Integer
int int a = 10; 4 bytes %d
numbers
Single
float x =
float precision 4 bytes %f
3.14;
decimal
Double
double y =
double precision 8 bytes %lf
3.14159;
decimal
Single char ch =
char 1 byte %c
character 'A';
Shalini Joseph ,ISE 52
Formatted I/O Functions
Formatted I/O functions help to display the
output to the user in different formats using the
format specifiers. Support all datatypes.
printf()
printf() function is used in a C program to
display any value like float, integer, character,
string, etc on the console screen.
It is a pre-defined function that is already
declared in the stdio.h(header file).
Shalini Joseph ,ISE 53
Printf Syntax:
printf("format string", arg1, arg2, ...);
formatted_string: It is a string that specifies the
data to be printed. It may also contain a format
specifier as a placeholder to print the value of
any variable or value.
args...: These are the variable/values
corresponding to the format specifier.
Shalini Joseph ,ISE 54
#include <stdio.h>
int main() {
int a = 10;
float b = 3.1415;
char c = 'A';
char str[] = "Hello";
printf("Integer: %d\n", a);
printf("Float: %.2f\n", b);
printf("Character: %c\n", c);
printf("String: %s\n", str);
return 0;
}
Shalini Joseph ,ISE 55
Formatted I/O Functions
scanf()
scanf() function is used in the C program for reading
or taking any value from the keyboard by the user.
Is a pre-defined function declared in stdio.h(header
file).
In scanf() function we use &(address-of operator)
which is used to store the variable value on the
memory location of that variable.
scanf("Format Specifier", &var1, &var2, ...., &varn);
Shalini Joseph ,ISE 56
Difference Between printf and scanf
printf()
Feature scanf() (Input)
(Output)
Purpose Displays data Reads data
Uses Format specifiers Format specifiers
Variables Values Addresses (&)
Example printf("%d", x); scanf("%d", &x);
Shalini Joseph ,ISE 57
intage = 25;
printf("I am %d years old.\n", age);
intnum;
printf("Enter a number: ");
scanf("%d", &num);
Shalini Joseph ,ISE 58
Unformatted Input/Output
Functions
Unformatted I/O functions are used only for
character data type or character array/string and
cannot be used for any other datatype.
getchar()
The getchar() function reads one character at one
time until and unless the enter key is pressed.
Shalini Joseph ,ISE 59
Unformatted Input/Output Functions
putchar()
The putchar() function is used to display a single
character at a time by passing that character
directly to it.
Shalini Joseph ,ISE 60
#include <stdio.h>
int main()
{
char ch;
printf("Enter any character: ");
// Reads a character
ch = getchar();
// Displays that character
putchar(ch);
return 0;
}
Shalini Joseph ,ISE 61
Unformatted Input/Output Functions
gets()
gets() function reads a group of characters or
strings from the keyboard by the user.
puts()
In C programming puts() function is used to
display a group of characters.
Shalini Joseph ,ISE 62
Difference: Formatted vs
Unformatted I/O
Unformatted I/O
Formatted I/O (printf,
Feature (getchar, putchar,
scanf)
gets, puts)
Limited
High (uses format
Flexibility (characters/strings
specifiers)
only)
Ease of use More complex Simple and faster
Example Input scanf("%d", &x); x = getchar();
Example Output
Shalini Joseph ,ISE
printf("%d", x); putchar(x); 63
Operators and Expressions
Operators are symbols that represent some kind
of operation, such as mathematical, relational,
bitwise, conditional, or logical computations,
which are to be performed on values or
variables.
The values and variables used with operators are
called operands.
Shalini Joseph ,ISE 64
Operators
Arithmetic operators
Relational operators
Logical operators
Assignment operators
Increment and Decrement operators
Conditional operators
Bitwise operators
Special operators
Shalini Joseph ,ISE 65
Arithmetic Operators
Used for basic mathematical operations :
Shalini Joseph ,ISE 66
Addition (+): Adds two operands.
Subtraction (-): Subtracts second operand from
the first.
Multiplication (*): Multiplies two operands.
Division (/): Divides first operand by the second
(returns quotient).
Modulus (%): Returns the remainder of division
of the first operand by the second.
Shalini Joseph ,ISE 67
#include <stdio.h>
int main() {
int a = 25, b = 5;
// using operators and printing results
printf("a + b = %d\n", a + b);
printf("a - b = %d\n", a - b);
printf("a * b = %d\n", a * b);
printf("a / b = %d\n", a / b);
printf("a % b = %d\n", a % b);
return 0;
}
Shalini Joseph ,ISE 68
Relational Operators:
Used to compare two values: If the relation is true, it returns 1; if the relation is false, it returns value 0.
Shalini Joseph ,ISE 69
Relational Operators
#include <stdio.h>
int main()
{
int a = 25, b = 5;
// using operators and printing results
printf("a < b : %d\n", a < b);
printf("a > b : %d\n", a > b);
printf("a <= b: %d\n", a <= b);
printf("a >= b: %d\n", a >= b);
printf("a == b: %d\n", a == b);
printf("a != b : %d\n", a != b);
return 0;
}
Shalini Joseph ,ISE 70
Logical Operators
Used for logical operations:
OPERATOR MEANING EXAMPLE
If c = 5 and d = 2 then,
Logical AND. True only
&& expression ((c==5) && (d>5))
if all operands are true
equals to 0.
Logical OR. True only if If c = 5 and d = 2 then,
|| either one operand is expression ((c==5) || (d>5))
true equals to 1.
Logical NOT. True only If c = 5 then, expression !
!
if the operand is 0 (c==5) equals to 0.
Shalini Joseph ,ISE 71
Logical Operators
#include <stdio.h>
int main()
{
int a = 25, b = 5;
// using operators and printing results
printf("%d\n", a>4 && a<3);
printf("%d\n", a>8 || a<4);
printf("%d\n", !(a==5));
return 0;
}
Shalini Joseph ,ISE 72
Assignment Operators
An assignment operator is used for assigning a value to a
variable. The most common assignment operator is =
Operator Example
Shalini Joseph ,ISE 73
#include <stdio.h>
int main() {
int a = 5;
// Equivalent to a = a + 3
a += 3;
printf("%d", a);
return 0;
}
Shalini Joseph ,ISE 74
C Increment and Decrement Operators
Used to increase or decrease values:
++(increment):Increases the value of a variable by 1
and --(decrement):decreases the value of a variable by 1.
Pre Increment:
Increases the value of the variable by 1 before it is used in an expression.
int x = 5;
int y = ++x; // x becomes 6, y is assigned 6
Post Increment:
Increases the value of the variable by 1 after it is used in an expression.
int x = 5;
int y = x++; // y is assigned 5, then x becomes 6
Shalini Joseph ,ISE 75
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 5, d = 5;
a++; // postfix increment
++b; // prefix increment
c--; // postfix decrement
--d; // prefix decrement
printf("a = %d\n", a);
printf("b = %d\n", b);
printf("c = %d\n", c);
printf("d = %d\n", d);
return 0;
}
Shalini Joseph ,ISE 76
#include <stdio.h>
int main() {
int a = 5, b;
// Pre-increment
b = ++a;
printf("a = %d, b = %d\n", a, b); // a=6, b=6
a = 5; // reset
// Post-increment
b = a++;
printf("a = %d, b = %d\n", a, b); // a=6, b=5
return 0;
}
Shalini Joseph ,ISE 77
#include <stdio.h>
int main() {
int a = 5, b;
b = 2 * ++a; // a=6, then b=2*6=12
printf("a = %d, b = %d\n", a, b);
return 0;
}
Output
a = 6, b = 12
Shalini Joseph ,ISE 78
Applications of Pre-Increment (++x)
1.Used in Complex Expressions
When you want the variable incremented before use in an expression.
int x = 5, y;
y = 10 + ++x; // x becomes 6 first, then used
printf("%d %d", x, y);
// Output: 6 16
2.Efficient Loop Control (especially in while loops)
Updates the value before checking condition.
int i = 0;
while (++i <= 5) { // increment first, then check
printf("%d ", i);
}
// Output: 1 2 3 4 5
Shalini Joseph ,ISE 79
C Bitwise Operators
During computation, mathematical operations like: addition,
subtraction, multiplication, division, etc. are converted to bit-
level which makes processing faster and saves power.
Shalini Joseph ,ISE 80
Shalini Joseph ,ISE 81
Bitwise AND (&)
Result bit = 1 only if both bits are 1.
5 & 3 → 0101 & 0011 = 0001 → 1
Bitwise OR (|)
Result bit = 1 if at least one bit is 1.
5 | 3 → 0101 | 0011 = 0111 → 7
Bitwise XOR (^)
Result bit = 1 if bits are different.
5 ^ 3 → 0101 ^ 0011 = 0110 → 6
Shalini Joseph ,ISE 82
Bitwise NOT (~)
Inverts each bit (1 → 0, 0 → 1).
Left Shift (<<)
Shifts bits to the left, fills with 0s.
5 << 1 → 0101 → 1010 → 10
Right Shift (>>)
Shifts bits to the right.
For positive numbers: fills with 0s.
5 >> 1 → 0101 → 0010 → 2
Shalini Joseph ,ISE 83
#include <stdio.h>
int main() {
int a = 12, b = 25;
printf("Output = %d", a & b);
return 0;
}
Shalini Joseph ,ISE 84
#include <stdio.h>
int main() {
int a = 8; // 00001000 in binary
int b = 4; // 00000100 in binary
printf("a & b = %d\n", a & b); // Bitwise AND()
printf("a | b = %d\n", a | b); // Bitwise OR
printf("a ^ b = %d\n", a ^ b); // Bitwise XOR
printf("~a = %d\n", ~a); // Bitwise NOT
printf("a << 2 = %d\n", a << 2); // Left shift
printf("b >> 2 = %d\n", b >> 2); // Right shift
return 0;
}
Shalini Joseph ,ISE 85
Conditional or Ternary Operator (?:) in C
The conditional operator ?: is the only ternary
operator in C (takes 3 operands).
It is a shorthand for if-else.
Syntax of Conditional operators
condition ? expression1 : expression2;
If condition is true → expression1 is
executed/returned.
If condition is false → expression2 is
executed/returned.
Shalini Joseph ,ISE 86
Conditional or Ternary Operator (?:) in C
#include <stdio.h>
int main() {
int a = 10, b = 20;
int max = (a > b) ? a : b; // Assigns the larger
value to 'max'
printf("The maximum value is: %d\n", max);
return 0;
}
Shalini Joseph ,ISE 87
Special Operators in C
special operators that serve unique purposes.
1.Comma Operator(,)
Purpose: Used to separate multiple expressions,
evaluating them from left to right.
int a = (1, 2, 3); // a will be assigned the value 3
2.Sizeof Operator (sizeof):
Usage: Returns the size (in bytes) of a variable
or data type.
Shalini Joseph ,ISE 88
The sizeof operator
#include <stdio.h>
int main()
{
int a;
float b;
double c;
char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));
return 0;
}
Shalini Joseph ,ISE 89
Arithmetic Expressions
An arithmetic expression in C is a
combination of operands (constants,
variables) and arithmetic operators
that evaluates to a value.
a +b*c
Here a,b,c are operands
+,* are operators
Shalini Joseph ,ISE 90
Evaluation of Expression
Where a=5,b=10
c = a* 2 + b / 2 ;
c = 5 * 2 + 10 / 2 ;
c = 10 + 10 / 2 ;
c = 10 + 5;
c = 15;
Shalini Joseph ,ISE 91
Operator precedence and
associativity
Operator Precedence:
Precedence decides the order in which
operators are applied in an expression.
Example:
int x = 10 + 20 * 5; // Multiplication has higher
precedence than addition
// => 10 + (20 * 5) = 110
Shalini Joseph ,ISE 92
Operator precedence and
associativity
Associativity:
Associativity decides the direction of evaluation
when operators have the same precedence.
int x = 5, y = 10, z = 15;
int result = x < y < z; // '<' is left to right
// (x < y) < z → (1) < 15 → 1
Shalini Joseph ,ISE 93
Operator Precedence
Shalini Joseph ,ISE 94
A calculator program needs to compute
the result of (5 + 3) * 2 - 8 / 4. Evaluate
the expression and explain the operator
precedence used in evaluation.
Expression:
(5+3)∗2−8/4
Step 1: Parentheses first
(5+3)=8
So expression becomes:
8∗2−8/4
Shalini Joseph ,ISE 95
Step 2: Multiplication and Division (from left to right)
First multiplication:
8∗2=16
16−8/4
Then division:
8/4=2
16−2
Step 3: Subtraction
16−2=14
Final Answer
Shalini Joseph ,ISE 96
Evaluation of Expression
(10 - 4) + (20 / (2 * 5)) * 3;
1.Parentheses:
(10−4)=6 and (2×5)=10
2. Division and multiplication (left → right):
20/10=2
2×3=6
3.Addition:
6+6=12
Shalini Joseph ,ISE 97
#include <stdio.h>
int main()
{
printf("%d",7 + 2 * 4 - 3);
return 0;
}
Shalini Joseph ,ISE 98
#include <stdio.h>
int main()
{
printf("%d",1 == 2 != 3);
return 0;
}
Output
1
Shalini Joseph ,ISE 99
(20 / 3) * 3 + (20 % 3)
Steps:
20 / 3 = 6 (integer division) → 6 * 3 = 18
20 % 3 = 2 → 18 + 2 = 20
Result: 20
Shalini Joseph ,ISE 100
Type Conversion
In C, when different data types are used together in an
expression, the compiler automatically converts them to
a common type to avoid data loss or mismatch. This
process is called type conversion.
Two Type:
Implicit Type Conversion
Explicit Type Conversion
Shalini Joseph ,ISE 101
Implicit Type Conversion
Happens automatically by the compiler.
Example
int a = 5;
double b = 2.5;
double c = a + b; // 'a' is converted to double
before addition
Shalini Joseph ,ISE 102
Explicit Type Conversion
The programmer forces a conversion using cast
operator.
Example
int a = 5, b = 2;
double c;
// With casting → floating-point division
c = (double)a / b; // c = 2.5
Shalini Joseph ,ISE 103