1 Introduction To C 21dec2020
1 Introduction To C 21dec2020
1
Algorithm
An algorithm is a sequence of steps to
solve a particular problem.
or
algorithm is an ordered set of
unambiguous steps that produces a
result and terminates in a finite time.
2
3
HOW TO WRITE ALGORITHMS
Step 1 Define your algorithms input
4
FlowChart
• Flowchart uses different symbols to design a solution to
a problem.
5
• Flowchart is diagrammatic /Graphical representation of
sequence of steps to solve a problem. To draw a
flowchart following standard symbols are use:
6
7
8
9
10
11
12
13
14
15
16
17
18
No Ye
If Print A is
If A <B s
A==B smaller
Ye
s
No
Print both
number are Print B is
same smaller
19
20
21
22
Practice
• Write if conditions for these situations.
• Let’s say we are dealing with an integer called n.
– The number is between 5 and 25 inclusive.
– Less than 12 or equal to 95.
– Even and less than 100.
– Between 10 and 20, or between 80 and 90.
– Ends in 7.
– Is neither 13 nor 17.
• Compare two numbers x and y.
– y is positive, and x is strictly between 0 and y.
Practice
Q1. Write a C program to input basic salary of an employee and calculate its Gross salary
according to following:
Basic Salary <= 10000 : HRA = 20%, DA = 80%
Basic Salary <= 20000 : HRA = 25%, DA = 90%
Basic Salary > 20000 : HRA = 30%, DA = 95%
Q2.Write a C program to input electricity unit charges and calculate total electricity bill
according to the given condition:
For first 50 units Rs. 0.50/unit
For next 100 units Rs. 0.75/unit
For next 100 units Rs. 1.20/unit
For unit above 250 Rs. 1.50/unit
An additional surcharge of 20% is added to the bill
Q3. Write a C program to input month number and print number of days in that month.
24
von neumann architecture
25
26
27
Constants, Variables and Keywords
28
Constants
29
• Rules for Constructing Integer Constants
– An integer constant must have at least one digit.
– It must not have a decimal point.
– It can be either positive or negative.
– If no sign precedes an integer constant it is assumed to be
positive.
– No commas or blanks are allowed within an integer constant.
– The allowable range for integer constants is -32768 to 32767.
30
31
keywords
32
Basics
• Each instruction in a C program is written as a separate
statement. Therefore a complete C program would
comprise of a series of statements.
• The statements in a program must appear in the same
order in which we wish them to be executed; unless of
course the logic of the problem demands a deliberate
‘jump’ or transfer of control to a statement, which is out
of sequence.
• Blank spaces may be inserted between two words to
improve the readability of the statement. However, no
blank spaces are allowed within a variable, constant or
keyword.
33
• All statements are entered in small case letters.
34
Example:
35
Receive input
36
37
C Instructions
•Type Declaration
•Arithmetic instruction
•Control instruction
38
Type Declaration
int bas;
float rs, grosssal ;
char name, code ;
39
40
41
42
43