MA 511: Computer Programming
Lecture 2
http://www.iitg.ernet.in/psm/indexing_ma511/y20/index.html
Partha Sarathi Mandal
[email protected]
Dept. of Mathematics
IIT Guwahati
IS
NUMBER >
LARGEST
Largest of a set of
numbers
START
READ NUMBER
ANY MORE
NUMBERS
?
WRITE
LARGEST
STOP
Yes
Yes
No
No
LARGEST
NUMBER
LARGEST
NUMBER
READ NUMBER
Symbol for flow chart
Start/Stop
Read/ Print
Processing
statements
Conditional
check
Direction of
flow
Connectors
Example C Program
/* A program that prints on the screen the
phase “I love C programming”*/
#include<stdio.h>
int
main(
void
){
printf("I love C programming");
return 0;
}
Example C Program
/* A program that prints on the screen the
phase “I love C programming”*/
#include<stdio.h>
int
main(
void
){
printf("I love C programming
\n
");
return 0;
}
Example C Program
/* A program that prints on the screen the
phase “I love C programming”*/
#include<stdio.h>
int
main(
void
){
printf("I love ");
printf("C programming");
printf("
\n
");
return 0;
Example C Program
/* A program that prints on the screen the
phase “I love C programming”*/
#include<stdio.h>
int
main(
void
){
printf("I love
\n
");
printf("C
\n
");
printf("programming
\n
");
return 0;
Example C Program
/* A program that prints on the screen the
phase “I love C programming”*/
#include<stdio.h>
int
main(
void
){
printf("I love
\n
");
printf("C
\n
programming
\n
");
return 0;
}
Exercise
(Flow Charts & C Programming)
1. Solve: Ax
2
+ Bx + C = 0 for given (A,B,C)
2. Find area of a tringle for given side lengths
3. Celsius to Fahrenheit conversation
4. The largest of a given set of integers
/* Calculate the area of a circle */
#include<stdio.h>
int
main(
void
){
int
radius;
float
area;
printf("Radius = ? ");
scanf("%d",
&radius
);
area = 3.14159 * radius * radius;
printf("Area = %f",
area
);
return 0;
}
/* end of main */
Example of a C program
Preprocessor directive, stdio.h is included in the compiled machine
code at # . It contains the standard I/O routines. Must be in the first
column. Must not end with a semicolon.
main() is a function, is required in all C pgs, it
indicates start of a C pgs., main() it is not
followed by a comma or semicolon.
Braces { and } enclosed the
computations carried out by main()
Every statement is
terminated by a semicolon
MA511: Computer Programming Partha S Mandal, IITG
Declaration : it informs the compiler
that radius and area are variables
names and that individual boxes
must be reserved for them in the
memory of the computer.
Comments (remarks) are placed
anywhere within the program within
delimiters /* end of main */ or // end…
/* Calculate the area of a circle */
#include<stdio.h>
#define PI 3.14159
int
main(
void
){
float radius, area;
int i, n;
printf("n = ? ");
scanf("%d", &n);
for(i=1; i<= n; i=i+1){
printf("Radius = ? ");
scanf("%f", &radius);
area =
PI
* radius * radius;
printf("Area = %f\n", area);
} /* end of for */
return 0;
}
/* end of main */
Example C Program
Symbolic constant (PI) is a name that substitutes for a
sequence of characters; numeric, character or string
constant. It is replaced by its corresponding character
constant during compile
printf & scanf are not part of the C language;
there is no input or output defined in C itself.
Its just a useful function from the standard
library of functions that are normally
accessible to C pgs. The behavior of printf &
scanf are defined in the ANSI standard.
for is looping statement , the 1
stexpression specifies an initial value
for an index, 2
nddetermines whether or not the loop is continued,
Some standard keywords
Keywords in C Programming
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
MA511: Computer Programming