Session 5 - Flow of Program
Session 5 - Flow of Program
C FLOW OF PROGRAM
5.1 Introduction
5.1 Introduction
1
5.3 Flow of Program
Figure 1
2
Figure 2
Table 1
Example
int a = 20;
int b = 10;
int c = 15;
int d = 5;
int e;
e = (a + b) * c / d;
3
= (20+10) * 15/5
= 30 * 15/5
= 450 /5
= 90
e= a + (b * c) / d;
= 20+ (10 * 15)/5
= 20 + 150/5
= 20 + 30
e= 50
Write a program in C to display the area and perimeter of rectangle. Length and
Breadth of rectangle is input through the keyboard.
#include<conio.h>
void main ( )
{
// Variable Declaration
float len, wid;
float area, peri ;
clrscr ( );
// Input
printf ( “ \n Enter the Length \n “ );
scanf ( “ %f “, &len );
printf ( “ \n Enter the Breadth \n “ );
scanf ( “ %f “, &wid );
// Processing
area = len * wid ;
peri = 2 * ( len + wid );
// Output
printf ( “ \n Area = %f “, area );
printf ( “ \n Perimeter = %f “, peri );
4
getch ( );
}
Output
5
Figure 1: Source Code to Executable Code
6
5.7 I/O – Processing – I/O cycle
a. In any C program, any of these two things will happen- either I/O or CPU
processing.
b. I/O - > CPU processing - > I/O - CPU processing - > I/O -> CPU processing
and so on is called I/O- Processing – I/O cycle.
d. As we know that speed of computer is very fast, so, it performs Output and
CPU processing at very fast speed. (Although speed of Output depends on
the speed of output device and speed of CPU processing depends on speed
of processor used.)
There are three types of errors that may be found in any C program. They
include:
a. Syntax Errors
b. Logical Errors
c. Runtime Errors
7
Errors that occur when you violate the rules of writing C syntax are known as
syntax errors. These errors indicate something that must be fixed (corrected)
before the code can be compiled. All these errors are detected by compiler and
thus are also known as compile-time errors. Example include:
Missing parentheses ( } )
Mistake in formula
Mistake in logic
Divide by zero
a. Write algorithm, draw flowchart and write C program to swap both numbers
(Two numbers are input through the keyboard).
8
Output-
b = 10
9
b. A four digit number ‘n’ is input through the keyboard. Write algorithm, draw
flowchart and write C program to display the sum of its digits.
Output
10
Enter any four digit number (1000-9999) 9875
Sum of its digits = 29
5.10 Summary
In this session, we learned about the flow of program and different types of errors
that can be found in a program.
11