3. Algorithm
What is algorithm?
An algorithm is a procedure or step-by-step instruction for solving a
problem. They form the foundation of writing a program.
For writing any programs, the following has to be known:
Input
Tasks to be preformed
Output expected
For any task, the instructions given to a friend is different from the instructions
given to a computer. Let’s look at the difference between them and know how to
give instructions to a computer.
4. Qualities of Good Algorithms
Input and output should be defined precisely.
Each step in the algorithm should be clear and
unambiguous.
Algorithms should be most effective among many different
ways to solve a problem.
An algorithm shouldn't include computer code. Instead,
the algorithm should be written in such a way that it can
be used in different programming languages.
5. Algorithm Examples
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
6. Algorithm 2: Find the largest number
among three numbers
Step 1: Start
Step 2: Declare variables a,b and c.
Step 3: Read variables a,b and c.
Step 4: If a > b If a > c
Display a is the largest number.
Else
Display c is the largest number.
Else If b > c
Display b is the largest number.
Else
Display c is the greatest number.
Step 5: Stop
7. Flowchart
A flowchart is a type of diagram that represents
an algorithm, workflow or process.
The flowchart shows the steps as boxes of
various kinds, and their order by connecting the
boxes with arrows. … Flowcharts are used in
analyzing, designing, documenting or managing a
process or program in various fields.
8. Flowchart Symbols
Flowcharts use special shapes to represent
different types of actions or steps in a
process. Lines and arrows show the sequence
of the steps, and the relationships among
them. These are known as flowchart symbols.
9. Common Flowchart Symbols
Rectangle Shape – Represents a process
Oval or Pill Shape – Represents the start or end
Diamond Shape – Represents a decision
Parallelogram – Represents input/output
10.
11.
12. DATA TYPES IN C LANGUAGE
A data type specifies the type of data that a variable can store such as
integer, floating, character, etc.
13. There are the following data types in C
language.
Types Data Types
Basic Data Type int, char, float, double
Derived Data Type array, pointer, structure, union
Enumeration Data Type enum
Void Data Type void
14. Basic Data Types
The basic data types are integer-based and floating-point based. C language supports
both signed and unsigned literals.
The memory size of the basic data types may change according to 32 or 64-bit
operating system.
Data Types Memory Size Range
char 1 byte −128 to 127
signed char 1 byte −128 to 127
unsigned char 1 byte 0 to 255
short 2 byte −32,768 to 32,767
signed short 2 byte −32,768 to 32,767
unsigned short 2 byte 0 to 65,535
15. Data Types Memory Size Range
int 2 byte −32,768 to 32,767
signed int 2 byte −32,768 to 32,767
unsigned int 2 byte 0 to 65,535
short int 2 byte −32,768 to 32,767
signed short int 2 byte −32,768 to 32,767
unsigned short int 2 byte 0 to 65,535
long int 4 byte -2,147,483,648 to
2,147,483,647
signed long int 4 byte -2,147,483,648 to
2,147,483,647
unsigned long int 4 byte 0 to 4,294,967,295
float 4 byte
double 8 byte
long double 10 byte
16. Keywords in C
A keyword is a reserved word. You cannot use it as a variable name,
constant name, etc. There are only 32 reserved words (keywords) in the
C language.
A list of 32 keywords in the c language is given below:
auto break case char const continu
e
default do
double else enum extern float for goto if
int long register return short signed sizeof static
struct switch typedef union unsigne
d
void volatile while
17. Variables in C
A variable is a name of the memory location. It is used to store data. Its value
can be changed, and it can be reused many times.
It is a way to represent memory location through symbol so that it can be easily
identified.
Let's see the syntax to declare a variable:
Syntax:Datatype variable_list;
The example of declaring the variable is given below:
1. int a;
2. float b;
3. char c;
Here, a, b, c are variables. The int, float, char are the data types.
We can also provide values while declaring the variables as given below:
1. int a=10,b=20;//declaring 2 variable of integer type
2. float f=20.8;
3. char c='A';
18. Rules for defining variables
• A variable can have alphabets, digits, and underscore.
• A variable name can start with the alphabet, and underscore only. It can't
start with a digit.
• No whitespace is allowed within the variable name.
• A variable name must not be any reserved word or keyword, e.g. int, float,
etc.
Valid variable names:
1. int a;
2. int _ab;
3. int a30;
Invalid variable names:
1. int 2;
2. int a b;
3. int long;