2. Algorithm:
In programming, algorithm is a set of well
defined instructions in sequence to solve the
problem
Key features of algorithms are:
Easy to grasp - Algorithms are there to help
humans to understand the solution.
Correctness - This is a must have feature for all
algorithms.
Precision - the steps are precisely stated(defined).
Finiteness - the algorithm stops after a finite number
of steps.
Generality - the algorithm applies to all possible
distribution of inputs as stated.
3. Example:
Write an algorithm to add two numbers entered by
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
4. Write an algorithm to find the largest among three
different numbers entered by user.
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
5. FlowChart:
Flowchart is a diagrammatic representation of an algorithm. Flowchart are
very helpful in writing program and explaining program to others.
Symbol Purpose Description
Flow line
Used to indicate the flow of logic by connecting
symbols.
Terminal(Stop/Start) Used to represent start and end of flowchart.
Input/Output Used for input and output operation.
Processing
Used for airthmetic operations and data-
manipulations.
Desicion
Used to represent the operation in which there
are two alternatives, true and false.
On-page Connector Used to join different flowline
Off-page Connector
Used to connect flowchart portion on different
page.
Predefined
Process/Function
Used to represent a group of statements
performing one processing task.
7. Pseudocode
Pseudocode is a detailed yet readable description of
what a computer program or algorithm must do,
expressed in a formally-styled natural language rather
than in a programming language. Pseudocode is
sometimes used as a detailed step in the process of
developing a program. It allows designers or lead
programmers to express the design in great detail and
provides programmers a detailed template for the next
step of writing code in a specific programming
language.
8. Generations of programming language
1. First generation languages (1GL)
2. Second generation languages (2GL)
3. Third generation languages (3GL)
4. Fourth generation languages (4GL)
5. Fifth generation languages (5GL)
9. Structured Programming Approach
Structured Programming Approach, as the word
suggests, can be defined as a programming approach
in which the program is made as a single structure. It
means that the code will execute the instruction by
instruction one after the other. It doesn’t support the
possibility of jumping from one instruction to some
other with help of any statement like GOTO, etc.
Therefore, the instructions in this approach will be
executed in a serial and structured manner. The
languages that support Structured programming
approach are:
C
C++
Java
11. Introduction to C:
'C' is a programming language that was developed in
the early 1970s by Dennis Ritchie at Bell
Laboratories.
It was designed for implementing system software
and used for developing the portable application
software.
It is one of the most popular languages.
C++ and Java are based on C programming which
means that if you are well versed with C, then the
above two programming languages can be learnt
very easily.
It is primarily used for the system programming. It
has the portability, efficiency and the ability to access
the specific hardware addresses and low runtime
demand on system resources which makes it a good
choice for implementation of operating systems and
13. Example: First C Program
Program:
#include <stdio.h>
void main()
{
printf(“n Welcome ”);
}
Output:
Welcome
14. Process of compilation and execution:
The process starts with the source file and ends with
the executable file.
A source file is created which consists of statements
written in C.
The source file is then processed by the compiler.
Compiler will translate the source code into object
code. Object code contains the machine instructions
for the CPU and calls the operating system API.
The object file is not an executable file.
The object file is then processed with a linker.
There can be different compilers for the program but
has a same linker for the object files.
The output of this linker will be an executable file.
16. Comments in C:
Comments in C language are used to provide information about lines of code. It is widely used for documenting code. There are 2 types
of comments in the C language.
1. Single Line Comments
2. Multi-Line Comments
Single Line Comments:
Single line comments are represented by double slash . Let's see an example of a single line comment in C.
#include<stdio.h>
int main(){
//printing information
printf("Hello C");
return 0;
}
Output:
Hello C
Even you can place the comment after the statement. For example:
printf("Hello C");//printing information
Mult Line Comments:
Multi-Line comments are represented by slash asterisk * ... *. It can occupy many lines of code, but it can't be nested. Syntax:
/*
code
to be commented
*/
Let's see an example of a multi-Line comment in C.
#include<stdio.h>
int main(){
/*printing information
Multi-Line Comment*/
17. Keywords in C:
auto break case char const continue default do
double else enum extern float for goto if
int long register return short signed sizeof static
struct switch typedef union unsigned void volatile while
18. Identifiers
Identifier refers to name given to entities such as
variables, functions, structures etc.
Identifier must be unique. They are created to give
unique name to a entity to identify it during the
execution of the program. For example:
int money; double accountBalance;
Here, money and accountBalance are identifiers.
19. Data Types in C:
A data type specifies the type of data that a variable
can store such as integer, floating, character, etc.
20. 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
21. 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.
23. Variable:
C variable is a named location in a memory where a
program can manipulate the data. This location is
used to hold the value of the variable.
The value of the C variable may get change in the
program.
C variable might be belonging to any of the data type
like int, float, char etc.
RULES FOR NAMING C VARIABLE:
Variable name must begin with letter or underscore.
Variables are case sensitive
They can be constructed with digits, letters.
No special symbols are allowed other than
underscore.
sum, height, _value are some examples for variable
24. Constants:
Constants refer to fixed values that the program may
not alter during its execution. These fixed values are
also called literals.
Constants can be of any of the basic data types
like an integer constant, a floating constant, a
character constant, or a string literal. There are
enumeration constants as well.
Constants are treated just like regular variables
except that their values cannot be modified after
their definition
25. Input : In any programming language input means to
feed some data into program. This can be given in the
form of file or from command line. C programming
language provides a set of built-in functions to read
given input and feed it to the program as per
requirement.
Output : In any programming language output means
to display some data on screen, printer or in any file. C
programming language provides a set of built-in
functions to output required data.
Here we will discuss only one input function and one
output function just to understand the meaning of
input and output. Rest of the functions are given
into C –Built-in Function
26. printf() function
This is one of the most frequently used functions in
C for output.
scanf() function
This is the function which can be used to to read an
input from the command line.
27. C Operators:
An operator is a symbol which operates on a value
or a variable. For example: + is an operator to
perform addition.
C has wide range of operators to perform various
operations.
28. Operator Meaning of Operator
+ addition or unary plus
- subtraction or unary minus
* Multiplication
/ Division
% remainder after division( modulo division)
29. // C Program to demonstrate the working of arithmetic operators
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d n",c);
c = a-b;
printf("a-b = %d n",c);
c = a*b;
printf("a*b = %d n",c);
c=a/b;
printf("a/b = %d n",c);
c=a%b;
printf("Remainder when a divided by b = %d n",c);
return 0;
}
Output
a+b = 13
a-b = 5
a*b = 36
30. C Assignment Operators
An assignment operator is used for assigning a
value to a variable. The most common assignment
operator is =
Operator Example Same as
= a = b a = b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
31. // C Program to demonstrate the working of assignment operators
#include <stdio.h>
int main()
{
int a = 5, c;
c = a;
printf("c = %d n", c);
c += a; // c = c+a
printf("c = %d n", c);
c -= a; // c = c-a
printf("c = %d n", c);
c *= a; // c = c*a
printf("c = %d n", c);
c /= a; // c = c/a
printf("c = %d n", c);
c %= a; // c = c%a
printf("c = %d n", c);
return 0;
}
c = 5
c = 10
c = 5
c = 25
c = 5
c = 0
OUTPUT
32. Relational operators are used in decision making and
loops:
Operator Meaning of Operator Example
== Equal to 5 == 3 returns 0
> Greater than 5 > 3 returns 1
< Less than 5 < 3 returns 0
!= Not equal to 5 != 3 returns 1
>= Greater than or equal to 5 >= 3 returns 1
<= Less than or equal to 5 <= 3 return 0
33. Logical operators :
Operator Meaning of Operator Example
&&
Logial AND. True only if all
operands are true
If c = 5 and d = 2 then, expression ((c == 5)
&& (d > 5)) equals to 0.
||
Logical OR. True only if either
one operand is true
If c = 5 and d = 2 then, expression ((c == 5)
|| (d > 5)) equals to 1.
!
Logical NOT. True only if the
operand is 0
If c = 5 then, expression ! (c == 5)equals to
0.
34. Bitwise Operators:
Operators Meaning of operators
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
<< Shift left
>> Shift right
35. C Ternary Operator (?:)
Ternary operator is a conditional operator that works
on 3 operands.
Conditional Operator Syntax:
conditionalExpression ? expression1 : expression2
The conditional operator works as follows:
The first expression conditionalExpression is
evaluated first. This expression evaluates to 1 if it's
true and evaluates to 0 if it's false.
If conditionalExpression is true, expression1 is
evaluated.
If conditionalExpression is false, expression2 is
evaluated.
36. Type Conversion in C
A type cast is basically a conversion from one type to
another. There are two types of type conversion:
1. Implicit Type Conversion
2. Explicit Type Conversion
Advantages of Type Conversion:
This is done to take advantage of certain features of
type hierarchies or type representations.
It helps us to compute expressions containing
variables of different data types.
38. Implicit Type Conversion:
Also known as ‘automatic type conversion’.
Done by the compiler on its own, without any external trigger
from the user.
Generally takes place when in an expression more than one
data type is present. In such condition type conversion (type
promotion) takes place to avoid lose of data.
All the data types of the variables are upgraded to the data
type of the variable with largest data type.
bool -> char -> short int -> int ->
unsigned int -> long -> unsigned ->
long long -> float -> double -> long
double
It is possible for implicit conversions to lose information,
signs can be lost (when signed is implicitly converted to
unsigned), and overflow can occur (when long long is
implicitly converted to float).
40. This process is also called type casting and it is user
defined.
Here the user can type cast the result to make it of a
particular data type.
The syntax in C:
(type) expression Type indicated the data type to
which the final result is converted.
41. Example:
// C program to demonstrate explicit type casting
#include<stdio.h>
int main()
{
double x = 1.2;
// Explicit conversion from double to int
int sum = (int)x + 1;
printf("sum = %d", sum);
return 0;
}
Output:
sum=2