Algorithms and Flowcharts
Algorithms and Flowcharts
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
Flowchart Symbols
Terminal symbol - indicates the beginning and
end points of an algorithm.
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
Lcm Lft x 30
Print
Lcm
STOP
Example 3
Algorithm
START
• Step 1: Input W,L
• Step 2: A L 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
given number.
4. Create an algorithm and a flowchart that will output all the prime
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.
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 (A && B) is false.
are 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
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.
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 Flowchart of if-else construct
as follows:
if(TestExpr)
TestEx
stmtT; yes pr No
else
stmtF;
stmtT stmtF
Example
C Program to Check Number is Multiple of 7 or not
#include<stdio.h>
int main()
{
int number;
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");
}
FLOWCHART OF AN IF-ELSE-IF
CONSTRUCT
TestExp
r
TestExpr
2
TestExpr
stmtT 3
1 TestExpr
stmtT
2 N
stmtT
3 stmtT
stmtT F
N
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 Construct 1 Construct 2
written under another if
statement, this cluster is called if(TestExprA) if(TestExprA)
a nested if.
if(TestExprB) if(TestExprB)
• The syntax for the nested is stmtBT; stmtBT;
given here: 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);
It executes by first evaluating c=a>b? a : b>a ? b :-1;
if(c==-1)
expr1, which is normally a printf(“\n BOTH NUMBERS ARE
relational expression, and then EQUAL”);
evaluates either expr2, if the else
first result was true, or expr3, if printf(“\n LARGER NUMBER IS %d”,c);
the 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 find the factorial of a number.
unconditionally transferred #include <stdio.h>
to the statement associated int main()
with the label specified in the {
int n, c;
goto statement. The form of long int f=1;
a goto statement is printf(“\n Enter the number:”);
scanf(“%d”,&n);
if(n<0)
goto label_name; 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 int main()
another 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 printf(“\n”);
program will be }
return 0;
}