Anda di halaman 1dari 43

An

An Example
Example
 Compute Squares /*
/* Compute
Compute Squares
Squares */
*/
 Prints a table of squares as shown #include
#include <stdio.h>
<stdio.h>
below
main()
main() {{
1 1 int
int n,
n, n_square;
n_square;
int
int lower,
lower, upper,
upper, step;
step;
2 4
3 9
lower
lower == 1;
1; /*
/* Lower
Lower imit
imit */
*/
4 16 upper
upper == 10;
10; /*
/* Upper
Upper limit
limit */
*/
5 25 step
step == 1;
1;
6 36
7 49 nn == lower;
lower;
8 64 while
while (( nn <= <= upper
upper )) {{
9 81 n_square
n_square == nn ** n;n;
10 100 printf(“%3d\t%6d\n”,n,n_square);
printf(“%3d\t%6d\n”,n,n_square);
nn == nn ++ 1;
1;
}}
}}

Lectures on Numerical Methods 1


An
An Example
Example
 Functions /*
/* Compute
Compute Squares
Squares */
*/
 C programs contain functions #include
#include <stdio.h>
<stdio.h>
and variables
main()
main() {{
 Function names are followed by
int
int n,
n, n_square;
n_square;
a list of arguments enclosed in int lower,
int lower, upper,
upper, step;
step;
parenthesis.
 Function definition consists of a lower
lower == 1;
1; /*
/* Lower
Lower imit
imit */
*/
group of statements enclosed in upper
upper == 10;
10; /*
/* Upper
Upper limit
limit */
*/
step
step == 1;
curly brackets. 1;
 printf is a library function. nn == lower;
lower;
The information about this while
while (( nn <= <= upper
upper )) {{
function is contained in a file n_square
n_square == nn ** n;n;
named stdio.h. printf(“%3d\t%6d\n”,n,n_square);
printf(“%3d\t%6d\n”,n,n_square);
nn == nn ++ 1;
1;
}}
}}

Lectures on Numerical Methods 2


An
An Example
Example
 Variables /*
/* Compute
Compute Squares
Squares */
*/
 Variables refer the objects that can #include
#include <stdio.h>
<stdio.h>
be stored in computer’s memory
locations. Variables are named main()
main() {{
(identifiers) int
int n,
n, n_square;
n_square;
 Variables can store data of various int
int lower,
lower, upper,
upper, step;
step;
types. Some basic datatypes are
lower
lower == 1; /*
/* Lower
Lower imit
imit */
char characters (1/2 bytes) 1; */
upper
upper == 10;
10; /*
/* Upper
Upper limit
limit */
*/
int integers (2/4 bytes)
step
step == 1;
1;
float rationals (4 bytes)
double rationals(8 bytes) nn == lower;
lower;
 Variables must be declared and while
while (( nn <= <= upper
upper )) {{
initialized. n_square
n_square == nn ** n;n;
printf(“%3d\t%6d\n”,n,n_square);
printf(“%3d\t%6d\n”,n,n_square);
nn == nn ++ 1;
1;
}}
}}

Lectures on Numerical Methods 3


An
An Example
Example
 Declarations /*
/* Compute
Compute Squares
Squares */
*/
 Declarations have form #include
#include <stdio.h>
<stdio.h>
type var1, var2, var3;
 Merely announces the data type to main()
main() {{
be associated with a variable. int
int n,
n, n_square;
n_square;
int
int lower,
lower, upper,
upper, step;
step;
 Initial value of variable is not known.
 Examples lower
lower == 1;
1; /*
/* Lower
Lower imit
imit */
*/
char name; upper
upper == 10;
10; /*
/* Upper
Upper limit
limit */
*/
int numStudents; step
step == 1;
1;
float applePrice;
nn == lower;
lower;
double veryAccurateVar;
while
while (( nn <= <= upper
upper )) {{
n_square
n_square == nn ** n;n;
printf(“%3d\t%6d\n”,n,n_square);
printf(“%3d\t%6d\n”,n,n_square);
nn == nn ++ 1;
1;
}}
}}

Lectures on Numerical Methods 4


An
An Example
Example
 Assignments /*
/* Compute
Compute Squares
Squares */
*/
Statement #include
#include <stdio.h>
<stdio.h>
 The assignment statements have main()
main() {{
the following form int n,
int n, n_square;
n_square;
someVar = expression ; int
int lower,
lower, upper,
upper, step;
step;
 Expressions consist of variables,
functions and operators. lower
lower == 1;
1; /*
/* Lower
Lower imit
imit */
*/
 Examples upper
upper == 10;
10; /*
/* Upper
Upper limit
limit */
*/
step
step == 1;
1;
c = 5 * ( f – 32 ) / 9
s = sin(x) / x nn == lower;
lower;
y = log(x) + v0 while
while (( nn <= <= upper
upper )) {{
I = P * R * T / 100 n_square
n_square == nn ** n;n;
Tax = 0.3 * ( I – 60000 ) printf(“%3d\t%6d\n”,n,n_square);
printf(“%3d\t%6d\n”,n,n_square);
nn == nn ++ 1;
1;
}}
}}

Lectures on Numerical Methods 5


An
An Example
Example
 Loops /*
/* Compute
Compute Squares
Squares */
*/
 The form of the while statement is #include
#include <stdio.h>
<stdio.h>
While ( condition )
statement ; main()
main() {{
While ( condition ) { int
int n,
n, n_square;
n_square;
int
int lower,
lower, upper,
upper, step;
step;
statements }
 If the condition is true the
lower
lower == 1;
1; /*
/* Lower
Lower imit
imit */
*/
statements in the body of the while upper
loop are executed. At the end of the upper == 10;
10; /*
/* Upper
Upper limit
limit */
*/
step
step == 1;
1;
loop the the condition is tested
again and executes the loop if the
nn == lower;
lower;
condition is true. This procedure
while
while (( nn <= <= upper
upper )) {{
continues till the condition fails to be
n_square
n_square == nn ** n;
true. n;
printf(“%3d\t%6d\n”,n,n_square);
printf(“%3d\t%6d\n”,n,n_square);
 Conditions are usually logical
nn == nn ++ 1;
1;
expressions using operators like ==,
}}
<, <=, >, >= etc. These have
}}
boolean value

Lectures on Numerical Methods 6


An
An Example
Example
 Execution /*
/* Compute
Compute Squares
Squares */
*/
#include
#include <stdio.h>
<stdio.h>

Variable Before After main()


main() {{
exeution execution int
int n,
n, n_square;
n_square;
int lower,
lower, upper,
upper, step;
n ?? ?? int step;

n_square ?? ?? lower
lower == 1;
1; /*
/* Lower
Lower imit
imit */
*/
lower ?? ?? upper
upper == 10;
10; /*
/* Upper
Upper limit
limit */
*/
step
step == 1;
upper ?? ?? 1;

step ?? ?? nn == lower;
lower;
while
while (( nn <= <= upper
upper )) {{
n_square
n_square == nn ** n;n;
printf(“%3d\t%6d\n”,n,n_square);
printf(“%3d\t%6d\n”,n,n_square);
nn == nn ++ 1;
1;
}}
}}

Lectures on Numerical Methods 7


An
An Example
Example
 Execution /*
/* Compute
Compute Squares
Squares */
*/
#include
#include <stdio.h>
<stdio.h>

Variable Before After main()


main() {{
exeution execution int
int n,
n, n_square;
n_square;
int lower,
lower, upper,
upper, step;
n ?? ?? int step;

n_square ?? ?? lower
lower == 1;
1; /*
/* Lower
Lower imit
imit */
*/
lower ?? ?? upper
upper == 10;
10; /*
/* Upper
Upper limit
limit */
*/
step
step == 1;
upper ?? ?? 1;

step ?? ?? nn == lower;
lower;
while
while (( nn <= <= upper
upper )) {{
n_square
n_square == nn ** n;n;
printf(“%3d\t%6d\n”,n,n_square);
printf(“%3d\t%6d\n”,n,n_square);
nn == nn ++ 1;
1;
}}
}}

Lectures on Numerical Methods 8


An
An Example
Example
 Execution /*
/* Compute
Compute Squares
Squares */
*/
#include
#include <stdio.h>
<stdio.h>

Variable Before After main()


main() {{
exeution execution int
int n,
n, n_square;
n_square;
int lower,
lower, upper,
upper, step;
n ?? ?? int step;

n_square ?? ?? lower
lower == 1;
1; /*
/* Lower
Lower imit
imit */
*/
lower ?? 1 upper
upper == 10;
10; /*
/* Upper
Upper limit
limit */
*/
step
step == 1;
upper ?? ?? 1;

step ?? ?? nn == lower;
lower;
while
while (( nn <= <= upper
upper )) {{
n_square
n_square == nn ** n;n;
printf(“%3d\t%6d\n”,n,n_square);
printf(“%3d\t%6d\n”,n,n_square);
nn == nn ++ 1;
1;
}}
}}

Lectures on Numerical Methods 9


An
An Example
Example
 Execution /*
/* Compute
Compute Squares
Squares */
*/
#include
#include <stdio.h>
<stdio.h>

Variable Before After main()


main() {{
exeution execution int
int n,
n, n_square;
n_square;
int lower,
lower, upper,
upper, step;
n ?? ?? int step;

n_square ?? ?? lower
lower == 1;
1; /*
/* Lower
Lower imit
imit */
*/
lower 1 1 upper
upper == 10;
10; /*
/* Upper
Upper limit
limit */
*/
step
step == 1;
upper ?? 10 1;

step ?? ?? nn == lower;
lower;
while
while (( nn <= <= upper
upper )) {{
n_square
n_square == nn ** n;n;
printf(“%3d\t%6d\n”,n,n_square);
printf(“%3d\t%6d\n”,n,n_square);
nn == nn ++ 1;
1;
}}
}}

Lectures on Numerical Methods 10


An
An Example
Example
 Execution /*
/* Compute
Compute Squares
Squares */
*/
#include
#include <stdio.h>
<stdio.h>

Variable Before After main()


main() {{
exeution execution int
int n,
n, n_square;
n_square;
int lower,
lower, upper,
upper, step;
n ?? ?? int step;

n_square ?? ?? lower
lower == 1;
1; /*
/* Lower
Lower imit
imit */
*/
lower 1 1 upper
upper == 10;
10; /*
/* Upper
Upper limit
limit */
*/
step
step == 1;
upper 10 10 1;

step ?? 1 nn == lower;
lower;
while
while (( nn <= <= upper
upper )) {{
n_square
n_square == nn ** n;n;
printf(“%3d\t%6d\n”,n,n_square);
printf(“%3d\t%6d\n”,n,n_square);
nn == nn ++ 1;
1;
}}
}}

Lectures on Numerical Methods 11


An
An Example
Example
 Execution /*
/* Compute
Compute Squares
Squares */
*/
#include
#include <stdio.h>
<stdio.h>

Variable Before After main()


main() {{
exeution execution int
int n,
n, n_square;
n_square;
int lower,
lower, upper,
upper, step;
n ?? 1 int step;

n_square ?? ?? lower
lower == 1;
1; /*
/* Lower
Lower imit
imit */
*/
lower 1 1 upper
upper == 10;
10; /*
/* Upper
Upper limit
limit */
*/
step
step == 1;
upper 10 10 1;

step 1 1 nn == lower;
lower;
while
while (( nn <= <= upper
upper )) {{
n_square
n_square == nn ** n;n;
printf(“%3d\t%6d\n”,n,n_square);
printf(“%3d\t%6d\n”,n,n_square);
nn == nn ++ 1;
1;
}}
}}

Lectures on Numerical Methods 12


An
An Example
Example
 Execution /*
/* Compute
Compute Squares
Squares */
*/
 n <= upper is true. #include
#include <stdio.h>
<stdio.h>

Variable Before After main()


main() {{
exeution execution int
int n,
n, n_square;
n_square;
int lower,
lower, upper,
upper, step;
n 1 1 int step;

n_square ?? ?? lower
lower == 1;
1; /*
/* Lower
Lower imit
imit */
*/
lower 1 1 upper
upper == 10;
10; /*
/* Upper
Upper limit
limit */
*/
step
step == 1;
upper 10 10 1;

step 1 1 nn == lower;
lower;
while
while (( nn <= <= upper
upper )) {{
n_square
n_square == nn ** n;n;
printf(“%3d\t%6d\n”,n,n_square);
printf(“%3d\t%6d\n”,n,n_square);
nn == nn ++ 1;
1;
}}
}}

Lectures on Numerical Methods 13


An
An Example
Example
 Execution /*
/* Compute
Compute Squares
Squares */
*/
 n <= upper is true. #include
#include <stdio.h>
<stdio.h>

Variable Before After main()


main() {{
exeution execution int
int n,
n, n_square;
n_square;
int lower,
lower, upper,
upper, step;
n 1 1 int step;

n_square ?? 1 lower
lower == 1;
1; /*
/* Lower
Lower imit
imit */
*/
lower 1 1 upper
upper == 10;
10; /*
/* Upper
Upper limit
limit */
*/
step
step == 1;
upper 10 10 1;

step 1 1 nn == lower;
lower;
while
while (( nn <= <= upper
upper )) {{
n_square
n_square == nn ** n;n;
printf(“%3d\t%6d\n”,n,n_square);
printf(“%3d\t%6d\n”,n,n_square);
nn == nn ++ 1;
1;
}}
}}

Lectures on Numerical Methods 14


An
An Example
Example
 Execution /*
/* Compute
Compute Squares
Squares */
*/
 n <= upper is true. #include
#include <stdio.h>
<stdio.h>

Variable Before After main()


main() {{
exeution execution int
int n,
n, n_square;
n_square;
int lower,
lower, upper,
upper, step;
n 1 1 int step;

n_square 1 1 lower
lower == 1;
1; /*
/* Lower
Lower imit
imit */
*/
lower 1 1 upper
upper == 10;
10; /*
/* Upper
Upper limit
limit */
*/
step
step == 1;
upper 10 10 1;

step 1 1 nn == lower;
lower;
while
while (( nn <= <= upper
upper )) {{
n_square
n_square == nn ** n;n;
printf(“%3d\t%6d\n”,n,n_square);
printf(“%3d\t%6d\n”,n,n_square);
nn == nn ++ 1;
1;
}}
}}

Lectures on Numerical Methods 15


An
An Example
Example
 Execution /*
/* Compute
Compute Squares
Squares */
*/
 n <= upper is true. #include
#include <stdio.h>
<stdio.h>

Variable Before After main()


main() {{
exeution execution int
int n,
n, n_square;
n_square;
int lower,
lower, upper,
upper, step;
n 1 2 int step;

n_square 1 1 lower
lower == 1;
1; /*
/* Lower
Lower imit
imit */
*/
lower 1 1 upper
upper == 10;
10; /*
/* Upper
Upper limit
limit */
*/
step
step == 1;
upper 10 10 1;

step 1 1 nn == lower;
lower;
while
while (( nn <= <= upper
upper )) {{
n_square
n_square == nn ** n;n;
printf(“%3d\t%6d\n”,n,n_square);
printf(“%3d\t%6d\n”,n,n_square);
nn == nn ++ 1;
1;
}}
}}

Lectures on Numerical Methods 16


An
An Example
Example
 Execution /*
/* Compute
Compute Squares
Squares */
*/
 n <= upper is true. #include
#include <stdio.h>
<stdio.h>

Variable Before After main()


main() {{
exeution execution int
int n,
n, n_square;
n_square;
int lower,
lower, upper,
upper, step;
n 2 2 int step;

n_square 1 1 lower
lower == 1;
1; /*
/* Lower
Lower imit
imit */
*/
lower 1 1 upper
upper == 10;
10; /*
/* Upper
Upper limit
limit */
*/
step
step == 1;
upper 10 10 1;

step 1 1 nn == lower;
lower;
while
while (( nn <= <= upper
upper )) {{
n_square
n_square == nn ** n;n;
printf(“%3d\t%6d\n”,n,n_square);
printf(“%3d\t%6d\n”,n,n_square);
nn == nn ++ 1;
1;
}}
}}

Lectures on Numerical Methods 17


An
An Example
Example
 Execution /*
/* Compute
Compute Squares
Squares */
*/
 n <= upper is true. #include
#include <stdio.h>
<stdio.h>

Variable Before After main()


main() {{
exeution execution int
int n,
n, n_square;
n_square;
int lower,
lower, upper,
upper, step;
n 2 2 int step;

n_square 1 4 lower
lower == 1;
1; /*
/* Lower
Lower imit
imit */
*/
lower 1 1 upper
upper == 10;
10; /*
/* Upper
Upper limit
limit */
*/
step
step == 1;
upper 10 10 1;

step 1 1 nn == lower;
lower;
while
while (( nn <= <= upper
upper )) {{
n_square
n_square == nn ** n;n;
printf(“%3d\t%6d\n”,n,n_square);
printf(“%3d\t%6d\n”,n,n_square);
nn == nn ++ 1;
1;
}}
}}

Lectures on Numerical Methods 18


An
An Example
Example
 Execution /*
/* Compute
Compute Squares
Squares */
*/
 n <= upper is true. #include
#include <stdio.h>
<stdio.h>

Variable Before After main()


main() {{
exeution execution int
int n,
n, n_square;
n_square;
int lower,
lower, upper,
upper, step;
n 2 2 int step;

n_square 4 4 lower
lower == 1;
1; /*
/* Lower
Lower imit
imit */
*/
lower 1 1 upper
upper == 10;
10; /*
/* Upper
Upper limit
limit */
*/
step
step == 1;
upper 10 10 1;

step 1 1 nn == lower;
lower;
while
while (( nn <= <= upper
upper )) {{
n_square
n_square == nn ** n;n;
printf(“%3d\t%6d\n”,n,n_square);
printf(“%3d\t%6d\n”,n,n_square);
nn == nn ++ 1;
1;
}}
}}

Lectures on Numerical Methods 19


An
An Example
Example
 Execution /*
/* Compute
Compute Squares
Squares */
*/
 n <= upper is true. #include
#include <stdio.h>
<stdio.h>

Variable Before After main()


main() {{
exeution execution int
int n,
n, n_square;
n_square;
int lower,
lower, upper,
upper, step;
n 2 3 int step;

n_square 4 4 lower
lower == 1;
1; /*
/* Lower
Lower imit
imit */
*/
lower 1 1 upper
upper == 10;
10; /*
/* Upper
Upper limit
limit */
*/
step
step == 1;
upper 10 10 1;

step 1 1 nn == lower;
lower;
while
while (( nn <= <= upper
upper )) {{
n_square
n_square == nn ** n;n;
printf(“%3d\t%6d\n”,n,n_square);
printf(“%3d\t%6d\n”,n,n_square);
nn == nn ++ 1;
1;
}}
}}

Lectures on Numerical Methods 20


An
An Example
Example
 Execution /*
/* Compute
Compute Squares
Squares */
*/
 n <= upper is true. #include
#include <stdio.h>
<stdio.h>

Variable Before After main()


main() {{
exeution execution int
int n,
n, n_square;
n_square;
int lower,
lower, upper,
upper, step;
n 10 11 int step;

n_square 100 100 lower


lower == 1;
1; /*
/* Lower
Lower imit
imit */
*/
lower 1 1 upper
upper == 10;
10; /*
/* Upper
Upper limit
limit */
*/
step
step == 1;
upper 10 10 1;

step 1 1 nn == lower;
lower;
while
while (( nn <= <= upper
upper )) {{
n_square
n_square == nn ** n;n;
printf(“%3d\t%6d\n”,n,n_square);
printf(“%3d\t%6d\n”,n,n_square);
nn == nn ++ 1;
1;
}}
}}

Lectures on Numerical Methods 21


An
An Example
Example
 Execution /*
/* Compute
Compute Squares
Squares */
*/
 n <= upper is false. #include
#include <stdio.h>
<stdio.h>

Variable Before After main()


main() {{
exeution execution int
int n,
n, n_square;
n_square;
int lower,
lower, upper,
upper, step;
n 11 11 int step;

n_square 100 100 lower


lower == 1;
1; /*
/* Lower
Lower imit
imit */
*/
lower 1 1 upper
upper == 10;
10; /*
/* Upper
Upper limit
limit */
*/
step
step == 1;
upper 10 10 1;

step 1 1 nn == lower;
lower;
while
while (( nn <= <= upper
upper )) {{
n_square
n_square == nn ** n;n;
printf(“%3d\t%6d\n”,n,n_square);
printf(“%3d\t%6d\n”,n,n_square);
nn == nn ++ 1;
1;
}}
}}

Lectures on Numerical Methods 22


An
An Example
Example
 Execution /*
/* Compute
Compute Squares
Squares */
*/
 n <= upper is false. #include
#include <stdio.h>
<stdio.h>

Variable Before After main()


main() {{
exeution execution int
int n,
n, n_square;
n_square;
int lower,
lower, upper,
upper, step;
n 11 11 int step;

n_square 100 100 lower


lower == 1;
1; /*
/* Lower
Lower imit
imit */
*/
lower 1 1 upper
upper == 10;
10; /*
/* Upper
Upper limit
limit */
*/
step
step == 1;
upper 10 10 1;

step 1 1 nn == lower;
lower;
while
while (( nn <= <= upper
upper )) {{
 Program ends n_square
n_square == nn ** n;n;
printf(“%3d\t%6d\n”,n,n_square);
printf(“%3d\t%6d\n”,n,n_square);
nn == nn ++ 1;
1;
}}
}}

Lectures on Numerical Methods 23


Printf
Printf Function
Function
 The first argument of printf is a format specifier.
 Each % sign in the format specifier is a formatting instruction.
 %d print a decimal integer
 %f print a floating number
 %wd prints a decimal number with min width of w chars
 %w.pf prints a floating number with precision p and min width w
 Other characters in the format specifier are just printed.

Lectures on Numerical Methods 24


Printf
Printf Function
Function
 Examples
 Printf( “I am Charu” ); I am Charu
 Printf( “|%3d|” , 8 ); | 8|
 Printf( “|%03d|” , 8 ); |8 |
 Printf( “|%3d|” , 8000 ); |8000|
 Printf( “|%f|” , 123.456 ); |123.456000|
 Printf( “|%f|” , 123.4567890123 ); |123.456789|
 Printf( “|%8.3d|” , 123.4567890123 ); | 123.456|
 Printf( “ Age = %d years” , 22 ); Age = 22 years
 Printf( “%d and %d” , 1, 2 ); 1 and 2

Lectures on Numerical Methods 25


An
An Example
Example
 Area of a circle /*
/* Compute
Compute Area
circle
circle */
*/
Area and
and Perimeter
Perimeter of
of aa

 The program produces the following #include <stdio.h>


#include <stdio.h>
output when 5.0 is input
main()
main() {{
float
float rad;
rad;
Enter the radius 5.0 float
float area, peri;
area, peri;
Area = 78.539749 printf(
printf( “Enter
“Enter the
the radius
radius ““ );
);
Peri = 31.415899 scanf(“%f” , &rad);
scanf(“%f” , &rad);

if
if (( rad
rad >> 0.0
0.0 )) {{
 And produces the following when – area
area == 3.14159
3.14159 ** radrad ** rad;
rad;
3.0 is input. peri = 6.28318 * rad;
peri = 6.28318 * rad;

printf(
printf( “Area
“Area == %f\n”
%f\n” ,, area
area );
);
Enter the radius -3.0 printf( “Peri = %f\n” , peri );
printf( “Peri = %f\n” , peri );
Negative radius }}
else
else
printf(
printf( “Negative
“Negative radius\n”);
radius\n”);
}}

Lectures on Numerical Methods 26


An
An Example
Example
/*
/* Compute
Compute Area
Area and
and Perimeter
Perimeter of
of aa
 Decisions circle
circle */
*/
 The form of if-else statement is #include <stdio.h>
#include <stdio.h>
as follows:
main()
main() {{
if ( condition ) statements float
float rad;
rad;
float
float area, peri;
area, peri;
if ( condtion ) statements printf(
printf( “Enter
“Enter the
the radius
radius ““ );
);
else statements scanf(“%f” , &rad);
scanf(“%f” , &rad);

if
if (( rad
rad >> 0.0
0.0 )) {{
 If condition is true then if-block of area
area == 3.14159
3.14159 ** radrad ** rad;
rad;
statements is executed otherwise peri = 6.28318 * rad;
peri = 6.28318 * rad;
else-block of statements is
executed. printf(
printf( “Area
“Area == %f\n”
%f\n” ,, area
area );
);
printf( “Peri = %f\n” , peri
printf( “Peri = %f\n” , peri ); );
}}
else
else
printf(
printf( “Negative
“Negative radius\n”);
radius\n”);
}}

Lectures on Numerical Methods 27


An
An Example
Example
/*
/* Compute
Compute Area
Area and
and Perimeter
Perimeter of
of aa
 Execution circle
circle */
*/
#include <stdio.h>
#include <stdio.h>

main()
main() {{
Variable Before After float
float rad;
rad;
exeution execution float
float area, peri;
area, peri;
rad ?? ??
printf(
printf( “Enter
“Enter the
the radius
radius ““ );
);
area ?? ?? scanf(“%f” , &rad);
scanf(“%f” , &rad);
peri ?? ??
if
if (( rad
rad >> 0.0
0.0 )) {{
area
area == 3.14159
3.14159 ** radrad ** rad;
rad;
peri = 6.28318 *
peri = 6.28318 * rad; rad;

printf(
printf( “Area
“Area == %f\n”
%f\n” ,, area
area );
);
printf( “Peri = %f\n” , peri
printf( “Peri = %f\n” , peri ); );
}}
else
else
printf(
printf( “Negative
“Negative radius\n”);
radius\n”);
}}

Lectures on Numerical Methods 28


An
An Example
Example
/*
/* Compute
Compute Area
Area and
and Perimeter
Perimeter of
of aa
 Execution circle
circle */
*/
#include <stdio.h>
#include <stdio.h>

main()
main() {{
Variable Before After float
float rad;
rad;
exeution execution float
float area, peri;
area, peri;
rad ?? 5.000000
printf(
printf( “Enter
“Enter the
the radius
radius ““ );
);
area ?? ?? scanf(“%f” , &rad);
scanf(“%f” , &rad);
peri ?? ??
if
if (( rad
rad >> 0.0
0.0 )) {{
area
area == 3.14159
3.14159 ** radrad ** rad;
rad;
peri = 6.28318 *
peri = 6.28318 * rad; rad;

printf(
printf( “Area
“Area == %f\n”
%f\n” ,, area
area );
);
printf( “Peri = %f\n” , peri
printf( “Peri = %f\n” , peri ); );
}}
else
else
printf(
printf( “Negative
“Negative radius\n”);
radius\n”);
}}

Lectures on Numerical Methods 29


An
An Example
Example
/*
/* Compute
Compute Area
Area and
and Perimeter
Perimeter of
of aa
 Execution circle
circle */
*/
 Condition rad > 0.0 is true #include <stdio.h>
#include <stdio.h>

main()
main() {{
Variable Before After float
float rad;
rad;
exeution execution float
float area, peri;
area, peri;
rad 5.000000 5.000000
printf(
printf( “Enter
“Enter the
the radius
radius ““ );
);
area ?? ?? scanf(“%f” , &rad);
scanf(“%f” , &rad);
peri ?? ??
if
if (( rad
rad >> 0.0
0.0 )) {{
area
area == 3.14159
3.14159 ** radrad ** rad;
rad;
peri = 6.28318 *
peri = 6.28318 * rad; rad;

printf(
printf( “Area
“Area == %f\n”
%f\n” ,, area
area );
);
printf( “Peri = %f\n” , peri
printf( “Peri = %f\n” , peri ); );
}}
else
else
printf(
printf( “Negative
“Negative radius\n”);
radius\n”);
}}

Lectures on Numerical Methods 30


An
An Example
Example
/*
/* Compute
Compute Area
Area and
and Perimeter
Perimeter of
of aa
 Execution circle
circle */
*/
#include <stdio.h>
#include <stdio.h>

main()
main() {{
Variable Before After float
float rad;
rad;
exeution execution float
float area, peri;
area, peri;
rad 5.00000 5.00000
printf(
printf( “Enter
“Enter the
the radius
radius ““ );
);
area ?? 78.53975 scanf(“%f” , &rad);
scanf(“%f” , &rad);
peri ?? ??
if
if (( rad
rad >> 0.0
0.0 )) {{
area
area == 3.14159
3.14159 ** radrad ** rad;
rad;
peri = 6.28318 *
peri = 6.28318 * rad; rad;

printf(
printf( “Area
“Area == %f\n”
%f\n” ,, area
area );
);
printf( “Peri = %f\n” , peri
printf( “Peri = %f\n” , peri ); );
}}
else
else
printf(
printf( “Negative
“Negative radius\n”);
radius\n”);
}}

Lectures on Numerical Methods 31


An
An Example
Example
/*
/* Compute
Compute Area
Area and
and Perimeter
Perimeter of
of aa
 Execution circle
circle */
*/
#include <stdio.h>
#include <stdio.h>

main()
main() {{
Variable Before After float
float rad;
rad;
exeution execution float
float area, peri;
area, peri;
rad 5.00000 5.00000
printf(
printf( “Enter
“Enter the
the radius
radius ““ );
);
area 78.53975 78.53975 scanf(“%f” , &rad);
scanf(“%f” , &rad);
peri ?? 31.41590
if
if (( rad
rad >> 0.0
0.0 )) {{
area
area == 3.14159
3.14159 ** radrad ** rad;
rad;
peri = 6.28318 *
peri = 6.28318 * rad; rad;

printf(
printf( “Area
“Area == %f\n”
%f\n” ,, area
area );
);
printf( “Peri = %f\n” , peri
printf( “Peri = %f\n” , peri ); );
}}
else
else
printf(
printf( “Negative
“Negative radius\n”);
radius\n”);
}}

Lectures on Numerical Methods 32


An
An Example
Example
/*
/* Compute
Compute Area
Area and
and Perimeter
Perimeter of
of aa
 Execution circle
circle */
*/
#include <stdio.h>
#include <stdio.h>

main()
main() {{
Variable Before After float
float rad;
rad;
exeution execution float
float area, peri;
area, peri;
rad 5.00000 5.00000
printf(
printf( “Enter
“Enter the
the radius
radius ““ );
);
area 78.53975 78.53975 scanf(“%f” , &rad);
scanf(“%f” , &rad);
peri 31.41590 31.41590
if
if (( rad
rad >> 0.0
0.0 )) {{
area
area == 3.14159
3.14159 ** radrad ** rad;
rad;
peri = 6.28318 *
peri = 6.28318 * rad; rad;

printf(
printf( “Area
“Area == %f\n”
%f\n” ,, area
area );
);
printf( “Peri = %f\n” , peri
printf( “Peri = %f\n” , peri ); );
}}
else
else
printf(
printf( “Negative
“Negative radius\n”);
radius\n”);
}}

Lectures on Numerical Methods 33


An
An Example
Example
/*
/* Compute
Compute Area
Area and
and Perimeter
Perimeter of
of aa
 Execution circle
circle */
*/
#include <stdio.h>
#include <stdio.h>

main()
main() {{
Variable Before After float
float rad;
rad;
exeution execution float
float area, peri;
area, peri;
rad 5.00000 5.00000
printf(
printf( “Enter
“Enter the
the radius
radius ““ );
);
area 78.53975 78.53975 scanf(“%f” , &rad);
scanf(“%f” , &rad);
peri 31.41590 31.41590
if
if (( rad
rad >> 0.0
0.0 )) {{
area
area == 3.14159
3.14159 ** radrad ** rad;
rad;
peri = 6.28318 *
peri = 6.28318 * rad; rad;

printf(
printf( “Area
“Area == %f\n”
%f\n” ,, area
area );
);
printf( “Peri = %f\n” , peri
printf( “Peri = %f\n” , peri ); );
}}
else
else
printf(
printf( “Negative
“Negative radius\n”);
radius\n”);
}}

Lectures on Numerical Methods 34


An
An Example
Example
/*
/* Compute
Compute Area
Area and
and Perimeter
Perimeter of
of aa
 Execution circle
circle */
*/
#include <stdio.h>
#include <stdio.h>

main()
main() {{
Variable Before After float
float rad;
rad;
exeution execution float
float area, peri;
area, peri;
rad 5.00000 5.00000
printf(
printf( “Enter
“Enter the
the radius
radius ““ );
);
area 78.53975 78.53975 scanf(“%f” , &rad);
scanf(“%f” , &rad);
peri 31.41590 31.41590
if
if (( rad
rad >> 0.0
0.0 )) {{
area
area == 3.14159
3.14159 ** radrad ** rad;
rad;
 Program ends peri = 6.28318 *
peri = 6.28318 * rad; rad;

printf(
printf( “Area
“Area == %f\n”
%f\n” ,, area
area );
);
printf( “Peri = %f\n” , peri
printf( “Peri = %f\n” , peri ); );
}}
else
else
printf(
printf( “Negative
“Negative radius\n”);
radius\n”);
}}

Lectures on Numerical Methods 35


An
An Example
Example
/*
/* Compute
Compute Area
Area and
and Perimeter
Perimeter of
of aa
 Execution circle
circle */
*/
#include <stdio.h>
#include <stdio.h>

main()
main() {{
Variable Before After float
float rad;
rad;
exeution execution float
float area, peri;
area, peri;
rad ?? -3.00000
printf(
printf( “Enter
“Enter the
the radius
radius ““ );
);
area ?? ?? scanf(“%f” , &rad);
scanf(“%f” , &rad);
peri ?? ??
if
if (( rad
rad >> 0.0
0.0 )) {{
area
area == 3.14159
3.14159 ** radrad ** rad;
rad;
peri = 6.28318 *
peri = 6.28318 * rad; rad;

printf(
printf( “Area
“Area == %f\n”
%f\n” ,, area
area );
);
printf( “Peri = %f\n” , peri
printf( “Peri = %f\n” , peri ); );
}}
else
else
printf(
printf( “Negative
“Negative radius\n”);
radius\n”);
}}

Lectures on Numerical Methods 36


An
An Example
Example
/*
/* Compute
Compute Area
Area and
and Perimeter
Perimeter of
of aa
 Execution circle
circle */
*/
 The condition rad > 0.0 is false #include <stdio.h>
#include <stdio.h>

main()
main() {{
Variable Before After float
float rad;
rad;
exeution execution float
float area, peri;
area, peri;
rad -3.00000 -3.00000
printf(
printf( “Enter
“Enter the
the radius
radius ““ );
);
area ?? ?? scanf(“%f” , &rad);
scanf(“%f” , &rad);
peri ?? ??
if
if (( rad
rad >> 0.0
0.0 )) {{
area
area == 3.14159
3.14159 ** radrad ** rad;
rad;
peri = 6.28318 *
peri = 6.28318 * rad; rad;

printf(
printf( “Area
“Area == %f\n”
%f\n” ,, area
area );
);
printf( “Peri = %f\n” , peri
printf( “Peri = %f\n” , peri ); );
}}
else
else
printf(
printf( “Negative
“Negative radius\n”);
radius\n”);
}}

Lectures on Numerical Methods 37


An
An Example
Example
/*
/* Compute
Compute Area
Area and
and Perimeter
Perimeter of
of aa
 Execution circle
circle */
*/
 The condition rad > 0.0 is false #include <stdio.h>
#include <stdio.h>

main()
main() {{
Variable Before After float
float rad;
rad;
exeution execution float
float area, peri;
area, peri;
rad -3.00000 -3.00000
printf(
printf( “Enter
“Enter the
the radius
radius ““ );
);
area ?? ?? scanf(“%f” , &rad);
scanf(“%f” , &rad);
peri ?? ??
if
if (( rad
rad >> 0.0
0.0 )) {{
area
area == 3.14159
3.14159 ** radrad ** rad;
rad;
peri = 6.28318 *
peri = 6.28318 * rad; rad;

printf(
printf( “Area
“Area == %f\n”
%f\n” ,, area
area );
);
 Prints “Negative radius” and printf( “Peri = %f\n” , peri
printf( “Peri = %f\n” , peri ); );
 Program ends }}
else
else
printf(
printf( “Negative
“Negative radius\n”);
radius\n”);
}}

Lectures on Numerical Methods 38


Scanf
Scanf Function
Function
 The first argument of scanf is a format specifier.
 Each % sign in the format specifier is a formatting instruction.
 %d scans a decimal integer and stores it in int variable
 %f scans a floating number and stores it in float variable
 %wd maximum width of w chars are scanned
 %wf maximum width of w chars are scanned
 Other characters in the format specifier are expected to be input as is.
Best to avoid any other chars in the input.

Lectures on Numerical Methods 39


Scanf
Scanf Function
Function
 scanf ( format, &intvar, &fltvar );
Format “%d%f” “%3d%6f”
Input Intvar Fltvar Intvar Fltvar
3 4.5 3 4.5 3 4.5
3\t4.5 (tab) 3 4.5 3 4.5
3\n4.5(newline) 3 4.5 3 4.5
34.5 34 0.5 34 0.5
003 4.5 3 4.5 3 4.5
0034.5 34 0.5 3 4.5
0003 4.5 3 4.5 0 3.0
00034.5 34 0.5 0 34.5
3 000004.5 3 4.5 3 4.0
3 4.5678912 3 4.567891 3 4.567800
A3a4.5 ?? ?? ?? ??
3a4.5 3 ?? 3 ??

Lectures on Numerical Methods 40


Algorithms
Algorithms
 Problem Algorithm
Algorithm
 Let S be a finite sequence of 1.
1. The
The mm11 bebe the
the largest
largest in
in
positive nonzero integers ak, except SS1.. Clearly m = a
Clearly m11 = a1 1..
1
the last number which is always 0. 2.
2. Let
Let II == 1.1.
Find the largest number in the 3.
3. If a
If aI+1 is zero, go to step
I+1 is zero, go to step
sequence. 7.
7.
 Example 4.
4. mmI+1 == max
max {{ mmI, aI+1}.
I+1 I, aI+1}.
 7, 9, 4, 6, 2, 5, 8, 0 5.
5. Increment
Increment I. I.
 The largest is 9. 6.
6. Go
Go to to step
step 3.3.
7.
7. Print
Print mmI.
 If Sk is a subsequence of first k I.

numbers of S and mk is the largest


term of Sk then
 mk+1 = max { mk , ak+1 }

Lectures on Numerical Methods 41


An
An Example
Example
 Find Largest /*
/* Find
Find the
#include
the largest
largest number
<stdio.h>
#include <stdio.h>
number */
*/

 The program produces the following


main()
main() {{
output
int
int number,
number, largest;
largest;

Enter a number 7 printf(


printf( "Enter
"Enter aa number
number "" );
);
scanf( "%d" , &largest
scanf( "%d" , &largest ); );
Enter a number 9
Enter a number 4 printf(
printf( "Enter
"Enter aa number
number "" );
);
Enter a number 6 scanf( "%d" , &number
scanf( "%d" , &number ););

Enter a number 2 while


while (( number
number >> 00 )) {{
Enter a number 0 if
if (( number
number >> largest
largest ))
largest
largest == number;
Largest is 9 number;
printf(
printf( "Enter aa number
"Enter number "" );
);
scanf( "%d" , &number
scanf( "%d" , &number ); );
}}

printf(
printf( "Largest
"Largest is
is %d\n"
%d\n" ,,
largest
largest );
);
}}

Lectures on Numerical Methods 42


Lab
Lab Assignment
Assignment 11
 Print a table of sine values of angles starting from 0o upto 180o in steps of
10o. ( There is a standard library function sin(x) declared in math.h )
 Read a sequence of positive integers and count the number of input
integers and calculate their sum. Calculate the average of the integers.
 Let ax2+bx+c=0. Read a, b and c. Print the solutions to the quadratic
equation. (The solutions may be complex. )
 Print a sequence of Fibonacci numbers less than 100.
 Input a positive integer and determine if it is a prime.

Lectures on Numerical Methods 43

Anda mungkin juga menyukai