Professional Documents
Culture Documents
Algorithms PPT 19 4 22
Algorithms PPT 19 4 22
FLOWCHARTS
ALGORITHMS AND FLOWCHARTS
Pseudocode:
1.Input a set of 4 marks
2.Calculate their average by summing and dividing by
4
3.if average is below 50
Print “FAIL”
else
Print “PASS”
Pseudocode & Algorithm
•Detailed Algorithm
• Step 1: Input M1,M2,M3,M4
Step 2: GRADE (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif
The Flowchart
A Flowchart
9
Flowchart Symbols
Basic
Name Symbol Use in Flowchart
11
Flowchart Symbols cont…
Selection symbol - shows a selection process
for two-way selection.
12
Flowchart – sequence control structure
Statement 1
Statement 2
Statement 3
13
Flowchart – selection control structure
No Yes
Condition
else- then-
statement(s) statement(s)
14
Flowchart – repetition control structure
yes Loop
Condition
Statement(s)
no
15
Example
START
Step 1: Input M1,M2,M3,M4
Step 2: GRADE (M1+M2+M3+M4)/4
Input
M1,M2,M3,M4
Step 3: if (GRADE <50) then
Print “FAIL”
else
GRADE(M1+M2+M3+M4)/4 Print “PASS”
endif
N IS Y
GRADE<50
PRINT PRINT
“PASS” “FAIL”
STOP
Example 2
Algorithm
Flowchart
• Step 1: Input Lft
START
• Step 2: Lcm Lft x 30
• Step 3: Print Lcm Input
Lft
Lcm Lft x 30
Print
Lcm
STOP
Example 3
Algorithm
START
• Step 1: Input W,L
• Step 2: AL x W Input
• Step 3: Print A W, L
ALxW
Print
A
STOP
Flowchart – example 1
Begin
Calculate
Age = current year – birth date
Display
age
End
21
Flowchart – example 2
Begin
Read age
End
22
Flowchart – example 5
Begin
sum = 0
current_number = 1
NO
current_number <= 10? print sum
YES
End
sum = sum + current_number
current_number = current_number + 1
23
Exercises: Algorithm & Flowchart
2.) Create an algorithm and a flowchart that will compute the area of a
circle.
Exercises: Algorithm & Flowchart
3.) Create an algorithm and a flowchart that will compute the sum of two
entered again. If the sum is above 20, it will display the sum.
Lab Activity: Algorithm & Flowchart
4) Create an algorithm and a flowchart that will output the largest number
2. Create an algorithm and a flowchart that will output the factorial of a given
number.
3. Create an algorithm and a flowchart that will output the Fibonacci series up
to a given number.
4. Create an algorithm and a flowchart that will output all the prime numbers
between 2 numbers.
The Character set of ‘C’
C language consist of some characters set, numbers and
some special symbols. The character set of C consist of all
the alphabets of English language. C consist of
Alphabets a to z, A to Z
Numeric 0,1 to 9
Special Symbols {,},[,],?,+,-,*,/,%,!,;,and more
The words formed from the character set are building
blocks of C and are sometimes known as tokens. These
tokens represent the individual entity of language. The
following different types of token are used in C
1) Identifiers 2)Keywords 3)Constants
4) Operators 5)Punctuation Symbols
Identifiers
A 'C' program consist of two types of elements , user defined and system
defined. Identifiers is nothing but a name given to these elements.
An identifier is a word used by a programmer to name a variable ,
function, or label.
identifiers consist of letters and digits, in any order, except that the first
character or label.
Identifiers consist of letters and digits if any order, except that the first
character must be letter.
Both Upper and lowercase letters can be used
Keywords
• Keywords are nothing but auto double int struct
system defined identifiers.
• Keywords are reserved words break else long switch
of the language.
• They have specific meaning in case enum register typedef
the language and cannot be
used by the programmer as char extern return union
variable or constant names
• C is case senitive, it means const float short unsigned
these must be used as it is
continue for signed void
• 32 Keywords in C
Programming default goto sizeof volatile
do if static while
Variables
• A variable is nothing but a name given to a storage area that our programs can manipulate.
Each variable in C has a specific type, which determines the size and layout of the
variable's memory; the range of values that can be stored within that memory; and the set
of operations that can be applied to the variable.
• The name of a variable can be composed of letters, digits, and the underscore character. It
must begin with either a letter or an underscore. Upper and lowercase letters are distinct
because C is case-sensitive. There are following basic variable types −
Type Description
• char Typically a single octet(one byte). This is an integer type.
• int The most natural size of integer for the machine.
• float A single-precision floating point value.
• double A double-precision floating point value.
• void Represents the absence of type.
Constants
• A constant is a value or an identifier whose value cannot be altered in
a program. For example: 1, 2.5,
• As mentioned, an identifier also can be defined as a constant. eg.
const double PI = 3.14
• Here, PI is a constant. Basically what it means is that, PI and 3.14 is
same for this program.
Integer constants
• A integer constant is a numeric constant (associated with number)
without any fractional or exponential part. There are three types of
integer constants in C programming:
• decimal constant(base 10)
• octal constant(base 8)
• hexadecimal constant(base 16)
Constants
Floating-point constants
• A floating point constant is a numeric constant that has either a
fractional form or an exponent form. For example:
2.0,0.0000234,-0.22E-5
Character constants
• A character constant is a constant which uses single quotation
around characters. For example: 'a', 'l', 'm', 'F'
String constants
• String constants are the constants which are enclosed in a pair
of double-quote marks. For example: "good" ,"x","Earth is
round\n"
• Convert 108 to a binary number.
• Solution: Given number is 108
• 108 = (1 * 81) + (0 * 80)
•=1*8+0*1
•=8+0
• = 8 (Decimal number)
• Now convert this decimal number into its equivalent binary number.
• Ans: (1000)2
• hex number (4FA)16
• Step 1: 4 F A
• A 4 15 10
• =1024+240+10
• Ans: 1247
Escape Sequences
Basic C Program
#include <stdio.h>
int main()
printf("Hello World!\n\n");
return 0;
}
Steps to RUN
• Step 1: Open turbo C IDE(Integrated Development Environment), click
on File and then click on New
•
• Step 2: Write a Hello World program that we created in the previous
article - C Hello World program.
•
• Step 3: Click on Compile menu and then on Compile option, or press the
keys press Alt + F9 to compile the code.
•
• Step 4: Click on Run or press Ctrl + F9 to run the code. Yes, C programs
are first compiled to generate the object code and then that object code is
Run.
• Step 5: Output is here.
Operators in C
&& Called Logical AND operator. If both the operands are (A && B) is false.
non-zero, then the condition becomes true.
! Called Logical NOT Operator. It is used to reverse the !(A && B) is true.
logical state of its operand. If a condition is true, then
Logical NOT operator will make it false.
Example
#include <stdio.h>
main() {
int a = 5;
int b = 20;
int c ;
if ( a && b ) {
printf("Line 1 - Condition is true\n" );
}
if ( a || b ) {
printf("Line 2 - Condition is true\n" );
}
/* lets change the value of a and b */
a = 0;
b = 10;
if ( a && b ) {
printf("Line 3 - Condition is true\n" );
} else {
printf("Line 3 - Condition is not true\n" );
}
if ( !(a && b) ) {
printf("Line 4 - Condition is true\n" );
}
}
Conditional or Ternary Operator (?:) in C
58
Data Types
C has a concept of 'data types' which are used to define a variable before its
use. The definition of a variable will assign storage for the variable and
define the type of data that will be held in the location.
The value of a variable can be changed any time.
C has the following basic built-in datatypes.
int
float
double
char
1. int - data type Data Types
int is used to define integer numbers.
{
int Count;
Count = 5;
}
2. float - data type
float is used to define floating point numbers.
{
float Miles;
Miles = 5.6;
}
3. double - data type
double is used to define BIG floating point numbers. It reserves twice the
storage for the number
{
double Atoms;
Atoms = 2500000;
}
UNIT-II
DESIGNING STRUCTURED
PROGRAMS IN C
CONTROL STATEMENTS
1. LEARNING OBJECTIVES
Understanding meaning of a statement and statement block
Negation !
POINTS TO NOTE
Space can be given between operand and operator (relational or logical) but space
is not allowed between any compound operator like <=, >=, ==, !=. It is also
compiler error to reverse them.
The relational operators have lower precedence than all arithmetic operators.
A FEW EXAMPLES
The following declarations and initializations
are given:
int x=1, y=2, z=3;
Then,
The expression x>=y evaluates to 0 (false).
The expression x+y evaluates to 3 (true).
The expression x=y evaluates to 2 (true).
Multi-way decisions
stmtT
Example
#include <stdio.h>
int main()
{
int x = 20;
int y = 22;
if (x<y)
{
printf("Variable x is less than y");
}
return 0;
}
TWO-WAY DECISIONS USING IF-ELSE
STATEMENT
The form of a two-way decision is as Flowchart of if-else construct
follows:
if(TestExpr)
stmtT; yes TestExpr
No
else
stmtF;
stmtT stmtF
Example
if(number%7==0)
{
printf("%d is multiple of 7.",number);
}
else
{
printf("%d is not multiple of 7.",number);
}
return 0;
}
MULTI-WAY DECISIONS
if(TestExpr1)
switch(expr)
stmtT1;
{
else if(TestExpr2) case constant1: stmtList1;
stmtT2; break;
else if(TestExpr3) case constant2: stmtList2;
stmtT3; break;
.. . case constant3: stmtList3;
else if(TestExprN) break;
………………………….
stmtTN;
………………………….
else
default: stmtListn;
stmtF; }
if-else-if ladder General format of switch
statements
Example
#include<stdio.h>
int main()
{
float number;
if(number>0)
{
printf("%.2f is a positive number",number);
}
else if(number<0)
{
printf("%.2f is a negative number",number);
}
else
{
printf("Number is zero");
}
return 0;
}
FLOWCHART OF AN IF-ELSE-IF
CONSTRUCT
TestExpr
TestExpr
2
TestExpr3
stmtT1
TestExpr
stmtT2 N
stmtT3
stmtTF
stmtTN
THE FOLLOWING PROGRAM CHECKS WHETHER A
NUMBER GIVEN BY THE USER IS ZERO, POSITIVE,
OR NEGATIVE
#include <stdio.h>
int main()
{
int x;
printf(“\n ENTER THE NUMBER:”);
scanf(“%d”, &x);
if(x > 0)
printf(“x is positive \n”);
else if(x == 0)
printf(“x is zero \n”);
else
printf(“x is negative \n”);
return 0;
}
NESTED IF
• When any if statement is written Construct 1 Construct 2
under another if statement, this
cluster is called a nested if. if(TestExprA) if(TestExprA)
if(TestExprB)
• The syntax for the nested is given if(TestExprB) stmtBT;
here: stmtBT; else
else stmtBF;
stmtBF; else
else
stmtAF; if(TestExprC)
stmtCT;
else
stmtCF;
A PROGRAM TO FIND THE LARGEST
AMONG THREE NUMBERS USING THE
NESTED LOOP
#include <stdio.h>
int main()
{
int a, b, c;
printf(“\nEnter the three numbers”);
scanf(“%d %d %d”, &a, &b, &c);
if(a > b)
if(a > c)
printf(“%d”, a);
else
printf(“%d”, c);
else
if(b > c)
printf(“%d”, b);
else
printf(“%d”, c);
return 0;
}
6. THE CONDITIONAL OPERATOR
• It has the following simple #include <stdio.h>
format: int main()
{
int a,b,c;
expr1 ? expr2 : expr3 printf(“\n ENTER THE TWO
NUMBERS:”);
scanf(“%d %d”, &a, &b);
c=a>b? a : b>a ? b :-1;
It executes by first evaluating if(c==-1)
expr1, which is normally a printf(“\n BOTH NUMBERS ARE
relational expression, and then EQUAL”);
evaluates either expr2, if the first else
result was true, or expr3, if the printf(“\n LARGER NUMBER IS %d”,c);
first result was false. return 0;
}
An Example
THE SWITCH STATEMENT
The general format of a switch
statement is
switch(expr)
{
case constant1: stmtList1;
break;
case constant2: stmtList2;
break;
case constant3: stmtList3;
break;
………………………….
………………………….
default: stmtListn;
}
Using Counter
GOTO STATEMENT
The following program is used to
The control is unconditionally find the factorial of a number.
transferred to the statement #include <stdio.h>
associated with the label int main()
{
specified in the goto statement. int n, c;
The form of a goto statement is long int f=1;
printf(“\n Enter the number:”);
scanf(“%d”,&n);
goto label_name; if(n<0)
goto end;
for(c=1; c<=n; c++)
f*=c;
printf(“\n FACTORIAL IS %ld”, f);
end:
return 0;
}
10. SPECIAL CONTROL STATEMENTS
“return” statements
“break” statements
“continue” statements
“BREAK” AND “CONTINUE” STATEMENTS
NESTED LOOPS
A nested loop refers to a loop #include <stdio.h>
that is contained within another int main()
loop.
{
If the following output has to
be obtained on the screen int row, col;
1 for(row=1;row<=4;++row)
22 {
333 for(col=1;col<=row;++col)
4444 printf(“%d \t”, row);
then the corresponding program printf(“\n”);
will be }
return 0;
}