SlideShare a Scribd company logo
Lectures on Numerical Methods 1
An Example
 Compute Squares
 Prints a table of squares as shown
below
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100
/* Compute Squares */
#include <stdio.h>
main() {
int n, n_square;
int lower, upper, step;
lower = 1; /* Lower imit */
upper = 10; /* Upper limit */
step = 1;
n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3dt%6dn”,n,n_square);
n = n + 1;
}
}
Lectures on Numerical Methods 2
An Example
 Functions
 C programs contain functions
and variables
 Function names are followed by
a list of arguments enclosed in
parenthesis.
 Function definition consists of a
group of statements enclosed in
curly brackets.
 printf is a library function.
The information about this
function is contained in a file
named stdio.h.
/* Compute Squares */
#include <stdio.h>
main() {
int n, n_square;
int lower, upper, step;
lower = 1; /* Lower imit */
upper = 10; /* Upper limit */
step = 1;
n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3dt%6dn”,n,n_square);
n = n + 1;
}
}
Lectures on Numerical Methods 3
An Example
 Variables
 Variables refer the objects that can
be stored in computer’s memory
locations. Variables are named
(identifiers)
 Variables can store data of various
types. Some basic datatypes are
char characters (1/2 bytes)
int integers (2/4 bytes)
float rationals (4 bytes)
double rationals(8 bytes)
 Variables must be declared and
initialized.
/* Compute Squares */
#include <stdio.h>
main() {
int n, n_square;
int lower, upper, step;
lower = 1; /* Lower imit */
upper = 10; /* Upper limit */
step = 1;
n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3dt%6dn”,n,n_square);
n = n + 1;
}
}
Lectures on Numerical Methods 4
An Example
 Declarations
 Declarations have form
type var1, var2, var3;
 Merely announces the data type to
be associated with a variable.
 Initial value of variable is not known.
 Examples
char name;
int numStudents;
float applePrice;
double veryAccurateVar;
/* Compute Squares */
#include <stdio.h>
main() {
int n, n_square;
int lower, upper, step;
lower = 1; /* Lower imit */
upper = 10; /* Upper limit */
step = 1;
n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3dt%6dn”,n,n_square);
n = n + 1;
}
}
Lectures on Numerical Methods 5
An Example
 Assignments
Statement
 The assignment statements have
the following form
someVar = expression ;
 Expressions consist of variables,
functions and operators.
 Examples
c = 5 * ( f – 32 ) / 9
s = sin(x) / x
y = log(x) + v0
I = P * R * T / 100
Tax = 0.3 * ( I – 60000 )
/* Compute Squares */
#include <stdio.h>
main() {
int n, n_square;
int lower, upper, step;
lower = 1; /* Lower imit */
upper = 10; /* Upper limit */
step = 1;
n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3dt%6dn”,n,n_square);
n = n + 1;
}
}
Lectures on Numerical Methods 6
An Example
 Loops
 The form of the while statement is
While ( condition )
statement ;
While ( condition ) {
statements }
 If the condition is true the
statements in the body of the while
loop are executed. At the end of the
loop the the condition is tested
again and executes the loop if the
condition is true. This procedure
continues till the condition fails to be
true.
 Conditions are usually logical
expressions using operators like ==,
<, <=, >, >= etc. These have
boolean value
/* Compute Squares */
#include <stdio.h>
main() {
int n, n_square;
int lower, upper, step;
lower = 1; /* Lower imit */
upper = 10; /* Upper limit */
step = 1;
n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3dt%6dn”,n,n_square);
n = n + 1;
}
}
Lectures on Numerical Methods 7
An Example
 Execution /* Compute Squares */
#include <stdio.h>
main() {
int n, n_square;
int lower, upper, step;
lower = 1; /* Lower imit */
upper = 10; /* Upper limit */
step = 1;
n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3dt%6dn”,n,n_square);
n = n + 1;
}
}
Variable Before
exeution
After
executio
n
n ?? ??
n_square ?? ??
lower ?? ??
upper ?? ??
step ?? ??
Lectures on Numerical Methods 8
An Example
 Execution /* Compute Squares */
#include <stdio.h>
main() {
int n, n_square;
int lower, upper, step;
lower = 1; /* Lower imit */
upper = 10; /* Upper limit */
step = 1;
n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3dt%6dn”,n,n_square);
n = n + 1;
}
}
Variable Before
exeution
After
executio
n
n ?? ??
n_square ?? ??
lower ?? ??
upper ?? ??
step ?? ??
Lectures on Numerical Methods 9
An Example
 Execution /* Compute Squares */
#include <stdio.h>
main() {
int n, n_square;
int lower, upper, step;
lower = 1; /* Lower imit */
upper = 10; /* Upper limit */
step = 1;
n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3dt%6dn”,n,n_square);
n = n + 1;
}
}
Variable Before
exeution
After
executio
n
n ?? ??
n_square ?? ??
lower ?? 1
upper ?? ??
step ?? ??
Lectures on Numerical Methods 10
An Example
 Execution /* Compute Squares */
#include <stdio.h>
main() {
int n, n_square;
int lower, upper, step;
lower = 1; /* Lower imit */
upper = 10; /* Upper limit */
step = 1;
n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3dt%6dn”,n,n_square);
n = n + 1;
}
}
Variable Before
exeution
After
executio
n
n ?? ??
n_square ?? ??
lower 1 1
upper ?? 10
step ?? ??
Lectures on Numerical Methods 11
An Example
 Execution /* Compute Squares */
#include <stdio.h>
main() {
int n, n_square;
int lower, upper, step;
lower = 1; /* Lower imit */
upper = 10; /* Upper limit */
step = 1;
n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3dt%6dn”,n,n_square);
n = n + 1;
}
}
Variable Before
exeution
After
executio
n
n ?? ??
n_square ?? ??
lower 1 1
upper 10 10
step ?? 1
Lectures on Numerical Methods 12
An Example
 Execution /* Compute Squares */
#include <stdio.h>
main() {
int n, n_square;
int lower, upper, step;
lower = 1; /* Lower imit */
upper = 10; /* Upper limit */
step = 1;
n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3dt%6dn”,n,n_square);
n = n + 1;
}
}
Variable Before
exeution
After
executio
n
n ?? 1
n_square ?? ??
lower 1 1
upper 10 10
step 1 1
Lectures on Numerical Methods 13
An Example
 Execution
 n <= upper is true.
/* Compute Squares */
#include <stdio.h>
main() {
int n, n_square;
int lower, upper, step;
lower = 1; /* Lower imit */
upper = 10; /* Upper limit */
step = 1;
n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3dt%6dn”,n,n_square);
n = n + 1;
}
}
Variable Before
exeution
After
executio
n
n 1 1
n_square ?? ??
lower 1 1
upper 10 10
step 1 1
Lectures on Numerical Methods 14
An Example
 Execution
 n <= upper is true.
/* Compute Squares */
#include <stdio.h>
main() {
int n, n_square;
int lower, upper, step;
lower = 1; /* Lower imit */
upper = 10; /* Upper limit */
step = 1;
n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3dt%6dn”,n,n_square);
n = n + 1;
}
}
Variable Before
exeution
After
executio
n
n 1 1
n_square ?? 1
lower 1 1
upper 10 10
step 1 1
Lectures on Numerical Methods 15
An Example
 Execution
 n <= upper is true.
/* Compute Squares */
#include <stdio.h>
main() {
int n, n_square;
int lower, upper, step;
lower = 1; /* Lower imit */
upper = 10; /* Upper limit */
step = 1;
n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3dt%6dn”,n,n_square);
n = n + 1;
}
}
Variable Before
exeution
After
executio
n
n 1 1
n_square 1 1
lower 1 1
upper 10 10
step 1 1
Lectures on Numerical Methods 16
An Example
 Execution
 n <= upper is true.
/* Compute Squares */
#include <stdio.h>
main() {
int n, n_square;
int lower, upper, step;
lower = 1; /* Lower imit */
upper = 10; /* Upper limit */
step = 1;
n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3dt%6dn”,n,n_square);
n = n + 1;
}
}
Variable Before
exeution
After
executio
n
n 1 2
n_square 1 1
lower 1 1
upper 10 10
step 1 1
Lectures on Numerical Methods 17
An Example
 Execution
 n <= upper is true.
/* Compute Squares */
#include <stdio.h>
main() {
int n, n_square;
int lower, upper, step;
lower = 1; /* Lower imit */
upper = 10; /* Upper limit */
step = 1;
n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3dt%6dn”,n,n_square);
n = n + 1;
}
}
Variable Before
exeution
After
executio
n
n 2 2
n_square 1 1
lower 1 1
upper 10 10
step 1 1
Lectures on Numerical Methods 18
An Example
 Execution
 n <= upper is true.
/* Compute Squares */
#include <stdio.h>
main() {
int n, n_square;
int lower, upper, step;
lower = 1; /* Lower imit */
upper = 10; /* Upper limit */
step = 1;
n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3dt%6dn”,n,n_square);
n = n + 1;
}
}
Variable Before
exeution
After
executio
n
n 2 2
n_square 1 4
lower 1 1
upper 10 10
step 1 1
Lectures on Numerical Methods 19
An Example
 Execution
 n <= upper is true.
/* Compute Squares */
#include <stdio.h>
main() {
int n, n_square;
int lower, upper, step;
lower = 1; /* Lower imit */
upper = 10; /* Upper limit */
step = 1;
n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3dt%6dn”,n,n_square);
n = n + 1;
}
}
Variable Before
exeution
After
executio
n
n 2 2
n_square 4 4
lower 1 1
upper 10 10
step 1 1
Lectures on Numerical Methods 20
An Example
 Execution
 n <= upper is true.
/* Compute Squares */
#include <stdio.h>
main() {
int n, n_square;
int lower, upper, step;
lower = 1; /* Lower imit */
upper = 10; /* Upper limit */
step = 1;
n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3dt%6dn”,n,n_square);
n = n + 1;
}
}
Variable Before
exeution
After
executio
n
n 2 3
n_square 4 4
lower 1 1
upper 10 10
step 1 1
Lectures on Numerical Methods 21
An Example
 Execution
 n <= upper is true.
/* Compute Squares */
#include <stdio.h>
main() {
int n, n_square;
int lower, upper, step;
lower = 1; /* Lower imit */
upper = 10; /* Upper limit */
step = 1;
n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3dt%6dn”,n,n_square);
n = n + 1;
}
}
Variable Before
exeution
After
executio
n
n 10 11
n_square 100 100
lower 1 1
upper 10 10
step 1 1
Lectures on Numerical Methods 22
An Example
 Execution
 n <= upper is false.
/* Compute Squares */
#include <stdio.h>
main() {
int n, n_square;
int lower, upper, step;
lower = 1; /* Lower imit */
upper = 10; /* Upper limit */
step = 1;
n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3dt%6dn”,n,n_square);
n = n + 1;
}
}
Variable Before
exeution
After
executio
n
n 11 11
n_square 100 100
lower 1 1
upper 10 10
step 1 1
Lectures on Numerical Methods 23
An Example
 Execution
 n <= upper is false.
 Program ends
/* Compute Squares */
#include <stdio.h>
main() {
int n, n_square;
int lower, upper, step;
lower = 1; /* Lower imit */
upper = 10; /* Upper limit */
step = 1;
n = lower;
while ( n <= upper ) {
n_square = n * n;
printf(“%3dt%6dn”,n,n_square);
n = n + 1;
}
}
Variable Before
exeution
After
executio
n
n 11 11
n_square 100 100
lower 1 1
upper 10 10
step 1 1
Lectures on Numerical Methods 24
Printf 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 25
Printf 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 26
An Example
 Area of a circle
 The program produces the following
output when 5.0 is input
Enter the radius 5.0
Area = 78.539749
Peri = 31.415899
 And produces the following when –
3.0 is input.
Enter the radius -3.0
Negative radius
/* Compute Area and Perimeter of a
circle */
#include <stdio.h>
main() {
float rad;
float area, peri;
printf( “Enter the radius “ );
scanf(“%f” , &rad);
if ( rad > 0.0 ) {
area = 3.14159 * rad * rad;
peri = 6.28318 * rad;
printf( “Area = %fn” , area );
printf( “Peri = %fn” , peri );
}
else
printf( “Negative radiusn”);
}
Lectures on Numerical Methods 27
An Example
 Decisions
 The form of if-else statement is
as follows:
if ( condition ) statements
if ( condtion ) statements
else statements
 If condition is true then if-block of
statements is executed otherwise
else-block of statements is
executed.
/* Compute Area and Perimeter of a
circle */
#include <stdio.h>
main() {
float rad;
float area, peri;
printf( “Enter the radius “ );
scanf(“%f” , &rad);
if ( rad > 0.0 ) {
area = 3.14159 * rad * rad;
peri = 6.28318 * rad;
printf( “Area = %fn” , area );
printf( “Peri = %fn” , peri );
}
else
printf( “Negative radiusn”);
}
Lectures on Numerical Methods 28
An Example
 Execution /* Compute Area and Perimeter of a
circle */
#include <stdio.h>
main() {
float rad;
float area, peri;
printf( “Enter the radius “ );
scanf(“%f” , &rad);
if ( rad > 0.0 ) {
area = 3.14159 * rad * rad;
peri = 6.28318 * rad;
printf( “Area = %fn” , area );
printf( “Peri = %fn” , peri );
}
else
printf( “Negative radiusn”);
}
Variable Before
exeution
After
executio
n
rad ?? ??
area ?? ??
peri ?? ??
Lectures on Numerical Methods 29
An Example
 Execution /* Compute Area and Perimeter of a
circle */
#include <stdio.h>
main() {
float rad;
float area, peri;
printf( “Enter the radius “ );
scanf(“%f” , &rad);
if ( rad > 0.0 ) {
area = 3.14159 * rad * rad;
peri = 6.28318 * rad;
printf( “Area = %fn” , area );
printf( “Peri = %fn” , peri );
}
else
printf( “Negative radiusn”);
}
Variable Before
exeution
After
executio
n
rad ?? 5.000000
area ?? ??
peri ?? ??
Lectures on Numerical Methods 30
An Example
 Execution
 Condition rad > 0.0 is true
/* Compute Area and Perimeter of a
circle */
#include <stdio.h>
main() {
float rad;
float area, peri;
printf( “Enter the radius “ );
scanf(“%f” , &rad);
if ( rad > 0.0 ) {
area = 3.14159 * rad * rad;
peri = 6.28318 * rad;
printf( “Area = %fn” , area );
printf( “Peri = %fn” , peri );
}
else
printf( “Negative radiusn”);
}
Variable Before
exeution
After
executio
n
rad 5.000000 5.000000
area ?? ??
peri ?? ??
Lectures on Numerical Methods 31
An Example
 Execution /* Compute Area and Perimeter of a
circle */
#include <stdio.h>
main() {
float rad;
float area, peri;
printf( “Enter the radius “ );
scanf(“%f” , &rad);
if ( rad > 0.0 ) {
area = 3.14159 * rad * rad;
peri = 6.28318 * rad;
printf( “Area = %fn” , area );
printf( “Peri = %fn” , peri );
}
else
printf( “Negative radiusn”);
}
Variable Before
exeution
After
executio
n
rad 5.00000 5.00000
area ?? 78.53975
peri ?? ??
Lectures on Numerical Methods 32
An Example
 Execution /* Compute Area and Perimeter of a
circle */
#include <stdio.h>
main() {
float rad;
float area, peri;
printf( “Enter the radius “ );
scanf(“%f” , &rad);
if ( rad > 0.0 ) {
area = 3.14159 * rad * rad;
peri = 6.28318 * rad;
printf( “Area = %fn” , area );
printf( “Peri = %fn” , peri );
}
else
printf( “Negative radiusn”);
}
Variable Before
exeution
After
executio
n
rad 5.00000 5.00000
area 78.53975 78.53975
peri ?? 31.41590
Lectures on Numerical Methods 33
An Example
 Execution /* Compute Area and Perimeter of a
circle */
#include <stdio.h>
main() {
float rad;
float area, peri;
printf( “Enter the radius “ );
scanf(“%f” , &rad);
if ( rad > 0.0 ) {
area = 3.14159 * rad * rad;
peri = 6.28318 * rad;
printf( “Area = %fn” , area );
printf( “Peri = %fn” , peri );
}
else
printf( “Negative radiusn”);
}
Variable Before
exeution
After
executio
n
rad 5.00000 5.00000
area 78.53975 78.53975
peri 31.41590 31.41590
Lectures on Numerical Methods 34
An Example
 Execution /* Compute Area and Perimeter of a
circle */
#include <stdio.h>
main() {
float rad;
float area, peri;
printf( “Enter the radius “ );
scanf(“%f” , &rad);
if ( rad > 0.0 ) {
area = 3.14159 * rad * rad;
peri = 6.28318 * rad;
printf( “Area = %fn” , area );
printf( “Peri = %fn” , peri );
}
else
printf( “Negative radiusn”);
}
Variable Before
exeution
After
executio
n
rad 5.00000 5.00000
area 78.53975 78.53975
peri 31.41590 31.41590
Lectures on Numerical Methods 35
An Example
 Execution
 Program ends
/* Compute Area and Perimeter of a
circle */
#include <stdio.h>
main() {
float rad;
float area, peri;
printf( “Enter the radius “ );
scanf(“%f” , &rad);
if ( rad > 0.0 ) {
area = 3.14159 * rad * rad;
peri = 6.28318 * rad;
printf( “Area = %fn” , area );
printf( “Peri = %fn” , peri );
}
else
printf( “Negative radiusn”);
}
Variable Before
exeution
After
executio
n
rad 5.00000 5.00000
area 78.53975 78.53975
peri 31.41590 31.41590
Lectures on Numerical Methods 36
An Example
 Execution /* Compute Area and Perimeter of a
circle */
#include <stdio.h>
main() {
float rad;
float area, peri;
printf( “Enter the radius “ );
scanf(“%f” , &rad);
if ( rad > 0.0 ) {
area = 3.14159 * rad * rad;
peri = 6.28318 * rad;
printf( “Area = %fn” , area );
printf( “Peri = %fn” , peri );
}
else
printf( “Negative radiusn”);
}
Variable Before
exeution
After
executio
n
rad ?? -3.00000
area ?? ??
peri ?? ??
Lectures on Numerical Methods 37
An Example
 Execution
 The condition rad > 0.0 is false
/* Compute Area and Perimeter of a
circle */
#include <stdio.h>
main() {
float rad;
float area, peri;
printf( “Enter the radius “ );
scanf(“%f” , &rad);
if ( rad > 0.0 ) {
area = 3.14159 * rad * rad;
peri = 6.28318 * rad;
printf( “Area = %fn” , area );
printf( “Peri = %fn” , peri );
}
else
printf( “Negative radiusn”);
}
Variable Before
exeution
After
executio
n
rad -3.00000 -3.00000
area ?? ??
peri ?? ??
Lectures on Numerical Methods 38
An Example
 Execution
 The condition rad > 0.0 is false
 Prints “Negative radius” and
 Program ends
/* Compute Area and Perimeter of a
circle */
#include <stdio.h>
main() {
float rad;
float area, peri;
printf( “Enter the radius “ );
scanf(“%f” , &rad);
if ( rad > 0.0 ) {
area = 3.14159 * rad * rad;
peri = 6.28318 * rad;
printf( “Area = %fn” , area );
printf( “Peri = %fn” , peri );
}
else
printf( “Negative radiusn”);
}
Variable Before
exeution
After
executio
n
rad -3.00000 -3.00000
area ?? ??
peri ?? ??
Lectures on Numerical Methods 39
Scanf 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 40
Scanf Function
Format “%d%f” “%3d%6f”
Input Intvar Fltvar Intvar Fltvar
3 4.5 3 4.5 3 4.5
3t4.5 (tab) 3 4.5 3 4.5
3n4.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 ??
 scanf ( format, &intvar, &fltvar );
Lectures on Numerical Methods 41
Algorithms
 Problem
 Let S be a finite sequence of
positive nonzero integers ak, except
the last number which is always 0.
Find the largest number in the
sequence.
 Example
 7, 9, 4, 6, 2, 5, 8, 0
 The largest is 9.
 If Sk is a subsequence of first k
numbers of S and mk is the largest
term of Sk then
 mk+1 = max { mk , ak+1 }
Algorithm
1. The m1 be the largest in
S1. Clearly m1 = a1.
2. Let I = 1.
3. If aI+1 is zero, go to step
7.
4. mI+1 = max { mI, aI+1}.
5. Increment I.
6. Go to step 3.
7. Print mI.
Lectures on Numerical Methods 42
An Example
 Find Largest
 The program produces the following
output
Enter a number 7
Enter a number 9
Enter a number 4
Enter a number 6
Enter a number 2
Enter a number 0
Largest is 9
/* Find the largest number */
#include <stdio.h>
main() {
int number, largest;
printf( "Enter a number " );
scanf( "%d" , &largest );
printf( "Enter a number " );
scanf( "%d" , &number );
while ( number > 0 ) {
if ( number > largest )
largest = number;
printf( "Enter a number " );
scanf( "%d" , &number );
}
printf( "Largest is %dn" ,
largest );
}
Lectures on Numerical Methods 43
Lab Assignment 1
 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.

More Related Content

PPTX
Operators1.pptx
PDF
Slides13.pdf
PPT
FDS- Basic Concepts.ppt
PPT
chapter1.ppt
PDF
7 functions
PPTX
CSE240 Pointers
PDF
6. function
Operators1.pptx
Slides13.pdf
FDS- Basic Concepts.ppt
chapter1.ppt
7 functions
CSE240 Pointers
6. function

Similar to introduction numerical analysis lecture .ppt (20)

DOC
Slide07 repetitions
PPTX
L25-L26-Parameter passing techniques.pptx
DOCX
Dti2143 lab sheet 9
DOCX
Bti1022 lab sheet 9 10
PPTX
Algorithms-Flowcharts for programming fundamental
DOCX
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PDF
C programming
DOC
C lab-programs
DOCX
Dam31303 dti2143 lab sheet 7
DOCX
PPTX
functions
PPT
Lecture 13 Loops1 with C++ programming.PPT
PPTX
Algorithmsandflowcharts1
PPTX
01 - DAA - PPT.pptx
DOCX
Chapter 1 Programming Fundamentals Assignment.docx
PPTX
C programming Control Structure.pptx
DOCX
Workshop03.docx lap trinh C cho người mới bắt đầu
PDF
The following is the (incomplete) header file for the class Fracti.pdf
Slide07 repetitions
L25-L26-Parameter passing techniques.pptx
Dti2143 lab sheet 9
Bti1022 lab sheet 9 10
Algorithms-Flowcharts for programming fundamental
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
C programming
C lab-programs
Dam31303 dti2143 lab sheet 7
functions
Lecture 13 Loops1 with C++ programming.PPT
Algorithmsandflowcharts1
01 - DAA - PPT.pptx
Chapter 1 Programming Fundamentals Assignment.docx
C programming Control Structure.pptx
Workshop03.docx lap trinh C cho người mới bắt đầu
The following is the (incomplete) header file for the class Fracti.pdf
Ad

More from ahmedhussein561 (6)

PPT
NUMERICAL ANALYSIS CH4CISE301-Topic5.ppt
PPT
NUMERICAL ANALYSIS CH6CISE301-Topic6.ppt
PPT
NUMERICAL ANALYSIS CISE301-Topic3.ppt
PPT
numerical analysis CISE301-Topic1.ppt
PPT
numerical analysis CISE301-Topic2.ppt
PPT
introduction to numerical analysis .ppt
NUMERICAL ANALYSIS CH4CISE301-Topic5.ppt
NUMERICAL ANALYSIS CH6CISE301-Topic6.ppt
NUMERICAL ANALYSIS CISE301-Topic3.ppt
numerical analysis CISE301-Topic1.ppt
numerical analysis CISE301-Topic2.ppt
introduction to numerical analysis .ppt
Ad

Recently uploaded (20)

PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPT
Mechanical Engineering MATERIALS Selection
PDF
Well-logging-methods_new................
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
PPT on Performance Review to get promotions
PPTX
Geodesy 1.pptx...............................................
PPTX
Sustainable Sites - Green Building Construction
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPT
Project quality management in manufacturing
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
UNIT 4 Total Quality Management .pptx
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
Construction Project Organization Group 2.pptx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Mechanical Engineering MATERIALS Selection
Well-logging-methods_new................
Automation-in-Manufacturing-Chapter-Introduction.pdf
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Model Code of Practice - Construction Work - 21102022 .pdf
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPT on Performance Review to get promotions
Geodesy 1.pptx...............................................
Sustainable Sites - Green Building Construction
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Project quality management in manufacturing
Embodied AI: Ushering in the Next Era of Intelligent Systems
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
UNIT 4 Total Quality Management .pptx
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Construction Project Organization Group 2.pptx

introduction numerical analysis lecture .ppt

  • 1. Lectures on Numerical Methods 1 An Example  Compute Squares  Prints a table of squares as shown below 1 1 2 4 3 9 4 16 5 25 6 36 7 49 8 64 9 81 10 100 /* Compute Squares */ #include <stdio.h> main() { int n, n_square; int lower, upper, step; lower = 1; /* Lower imit */ upper = 10; /* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3dt%6dn”,n,n_square); n = n + 1; } }
  • 2. Lectures on Numerical Methods 2 An Example  Functions  C programs contain functions and variables  Function names are followed by a list of arguments enclosed in parenthesis.  Function definition consists of a group of statements enclosed in curly brackets.  printf is a library function. The information about this function is contained in a file named stdio.h. /* Compute Squares */ #include <stdio.h> main() { int n, n_square; int lower, upper, step; lower = 1; /* Lower imit */ upper = 10; /* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3dt%6dn”,n,n_square); n = n + 1; } }
  • 3. Lectures on Numerical Methods 3 An Example  Variables  Variables refer the objects that can be stored in computer’s memory locations. Variables are named (identifiers)  Variables can store data of various types. Some basic datatypes are char characters (1/2 bytes) int integers (2/4 bytes) float rationals (4 bytes) double rationals(8 bytes)  Variables must be declared and initialized. /* Compute Squares */ #include <stdio.h> main() { int n, n_square; int lower, upper, step; lower = 1; /* Lower imit */ upper = 10; /* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3dt%6dn”,n,n_square); n = n + 1; } }
  • 4. Lectures on Numerical Methods 4 An Example  Declarations  Declarations have form type var1, var2, var3;  Merely announces the data type to be associated with a variable.  Initial value of variable is not known.  Examples char name; int numStudents; float applePrice; double veryAccurateVar; /* Compute Squares */ #include <stdio.h> main() { int n, n_square; int lower, upper, step; lower = 1; /* Lower imit */ upper = 10; /* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3dt%6dn”,n,n_square); n = n + 1; } }
  • 5. Lectures on Numerical Methods 5 An Example  Assignments Statement  The assignment statements have the following form someVar = expression ;  Expressions consist of variables, functions and operators.  Examples c = 5 * ( f – 32 ) / 9 s = sin(x) / x y = log(x) + v0 I = P * R * T / 100 Tax = 0.3 * ( I – 60000 ) /* Compute Squares */ #include <stdio.h> main() { int n, n_square; int lower, upper, step; lower = 1; /* Lower imit */ upper = 10; /* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3dt%6dn”,n,n_square); n = n + 1; } }
  • 6. Lectures on Numerical Methods 6 An Example  Loops  The form of the while statement is While ( condition ) statement ; While ( condition ) { statements }  If the condition is true the statements in the body of the while loop are executed. At the end of the loop the the condition is tested again and executes the loop if the condition is true. This procedure continues till the condition fails to be true.  Conditions are usually logical expressions using operators like ==, <, <=, >, >= etc. These have boolean value /* Compute Squares */ #include <stdio.h> main() { int n, n_square; int lower, upper, step; lower = 1; /* Lower imit */ upper = 10; /* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3dt%6dn”,n,n_square); n = n + 1; } }
  • 7. Lectures on Numerical Methods 7 An Example  Execution /* Compute Squares */ #include <stdio.h> main() { int n, n_square; int lower, upper, step; lower = 1; /* Lower imit */ upper = 10; /* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3dt%6dn”,n,n_square); n = n + 1; } } Variable Before exeution After executio n n ?? ?? n_square ?? ?? lower ?? ?? upper ?? ?? step ?? ??
  • 8. Lectures on Numerical Methods 8 An Example  Execution /* Compute Squares */ #include <stdio.h> main() { int n, n_square; int lower, upper, step; lower = 1; /* Lower imit */ upper = 10; /* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3dt%6dn”,n,n_square); n = n + 1; } } Variable Before exeution After executio n n ?? ?? n_square ?? ?? lower ?? ?? upper ?? ?? step ?? ??
  • 9. Lectures on Numerical Methods 9 An Example  Execution /* Compute Squares */ #include <stdio.h> main() { int n, n_square; int lower, upper, step; lower = 1; /* Lower imit */ upper = 10; /* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3dt%6dn”,n,n_square); n = n + 1; } } Variable Before exeution After executio n n ?? ?? n_square ?? ?? lower ?? 1 upper ?? ?? step ?? ??
  • 10. Lectures on Numerical Methods 10 An Example  Execution /* Compute Squares */ #include <stdio.h> main() { int n, n_square; int lower, upper, step; lower = 1; /* Lower imit */ upper = 10; /* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3dt%6dn”,n,n_square); n = n + 1; } } Variable Before exeution After executio n n ?? ?? n_square ?? ?? lower 1 1 upper ?? 10 step ?? ??
  • 11. Lectures on Numerical Methods 11 An Example  Execution /* Compute Squares */ #include <stdio.h> main() { int n, n_square; int lower, upper, step; lower = 1; /* Lower imit */ upper = 10; /* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3dt%6dn”,n,n_square); n = n + 1; } } Variable Before exeution After executio n n ?? ?? n_square ?? ?? lower 1 1 upper 10 10 step ?? 1
  • 12. Lectures on Numerical Methods 12 An Example  Execution /* Compute Squares */ #include <stdio.h> main() { int n, n_square; int lower, upper, step; lower = 1; /* Lower imit */ upper = 10; /* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3dt%6dn”,n,n_square); n = n + 1; } } Variable Before exeution After executio n n ?? 1 n_square ?? ?? lower 1 1 upper 10 10 step 1 1
  • 13. Lectures on Numerical Methods 13 An Example  Execution  n <= upper is true. /* Compute Squares */ #include <stdio.h> main() { int n, n_square; int lower, upper, step; lower = 1; /* Lower imit */ upper = 10; /* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3dt%6dn”,n,n_square); n = n + 1; } } Variable Before exeution After executio n n 1 1 n_square ?? ?? lower 1 1 upper 10 10 step 1 1
  • 14. Lectures on Numerical Methods 14 An Example  Execution  n <= upper is true. /* Compute Squares */ #include <stdio.h> main() { int n, n_square; int lower, upper, step; lower = 1; /* Lower imit */ upper = 10; /* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3dt%6dn”,n,n_square); n = n + 1; } } Variable Before exeution After executio n n 1 1 n_square ?? 1 lower 1 1 upper 10 10 step 1 1
  • 15. Lectures on Numerical Methods 15 An Example  Execution  n <= upper is true. /* Compute Squares */ #include <stdio.h> main() { int n, n_square; int lower, upper, step; lower = 1; /* Lower imit */ upper = 10; /* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3dt%6dn”,n,n_square); n = n + 1; } } Variable Before exeution After executio n n 1 1 n_square 1 1 lower 1 1 upper 10 10 step 1 1
  • 16. Lectures on Numerical Methods 16 An Example  Execution  n <= upper is true. /* Compute Squares */ #include <stdio.h> main() { int n, n_square; int lower, upper, step; lower = 1; /* Lower imit */ upper = 10; /* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3dt%6dn”,n,n_square); n = n + 1; } } Variable Before exeution After executio n n 1 2 n_square 1 1 lower 1 1 upper 10 10 step 1 1
  • 17. Lectures on Numerical Methods 17 An Example  Execution  n <= upper is true. /* Compute Squares */ #include <stdio.h> main() { int n, n_square; int lower, upper, step; lower = 1; /* Lower imit */ upper = 10; /* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3dt%6dn”,n,n_square); n = n + 1; } } Variable Before exeution After executio n n 2 2 n_square 1 1 lower 1 1 upper 10 10 step 1 1
  • 18. Lectures on Numerical Methods 18 An Example  Execution  n <= upper is true. /* Compute Squares */ #include <stdio.h> main() { int n, n_square; int lower, upper, step; lower = 1; /* Lower imit */ upper = 10; /* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3dt%6dn”,n,n_square); n = n + 1; } } Variable Before exeution After executio n n 2 2 n_square 1 4 lower 1 1 upper 10 10 step 1 1
  • 19. Lectures on Numerical Methods 19 An Example  Execution  n <= upper is true. /* Compute Squares */ #include <stdio.h> main() { int n, n_square; int lower, upper, step; lower = 1; /* Lower imit */ upper = 10; /* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3dt%6dn”,n,n_square); n = n + 1; } } Variable Before exeution After executio n n 2 2 n_square 4 4 lower 1 1 upper 10 10 step 1 1
  • 20. Lectures on Numerical Methods 20 An Example  Execution  n <= upper is true. /* Compute Squares */ #include <stdio.h> main() { int n, n_square; int lower, upper, step; lower = 1; /* Lower imit */ upper = 10; /* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3dt%6dn”,n,n_square); n = n + 1; } } Variable Before exeution After executio n n 2 3 n_square 4 4 lower 1 1 upper 10 10 step 1 1
  • 21. Lectures on Numerical Methods 21 An Example  Execution  n <= upper is true. /* Compute Squares */ #include <stdio.h> main() { int n, n_square; int lower, upper, step; lower = 1; /* Lower imit */ upper = 10; /* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3dt%6dn”,n,n_square); n = n + 1; } } Variable Before exeution After executio n n 10 11 n_square 100 100 lower 1 1 upper 10 10 step 1 1
  • 22. Lectures on Numerical Methods 22 An Example  Execution  n <= upper is false. /* Compute Squares */ #include <stdio.h> main() { int n, n_square; int lower, upper, step; lower = 1; /* Lower imit */ upper = 10; /* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3dt%6dn”,n,n_square); n = n + 1; } } Variable Before exeution After executio n n 11 11 n_square 100 100 lower 1 1 upper 10 10 step 1 1
  • 23. Lectures on Numerical Methods 23 An Example  Execution  n <= upper is false.  Program ends /* Compute Squares */ #include <stdio.h> main() { int n, n_square; int lower, upper, step; lower = 1; /* Lower imit */ upper = 10; /* Upper limit */ step = 1; n = lower; while ( n <= upper ) { n_square = n * n; printf(“%3dt%6dn”,n,n_square); n = n + 1; } } Variable Before exeution After executio n n 11 11 n_square 100 100 lower 1 1 upper 10 10 step 1 1
  • 24. Lectures on Numerical Methods 24 Printf 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.
  • 25. Lectures on Numerical Methods 25 Printf 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
  • 26. Lectures on Numerical Methods 26 An Example  Area of a circle  The program produces the following output when 5.0 is input Enter the radius 5.0 Area = 78.539749 Peri = 31.415899  And produces the following when – 3.0 is input. Enter the radius -3.0 Negative radius /* Compute Area and Perimeter of a circle */ #include <stdio.h> main() { float rad; float area, peri; printf( “Enter the radius “ ); scanf(“%f” , &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %fn” , area ); printf( “Peri = %fn” , peri ); } else printf( “Negative radiusn”); }
  • 27. Lectures on Numerical Methods 27 An Example  Decisions  The form of if-else statement is as follows: if ( condition ) statements if ( condtion ) statements else statements  If condition is true then if-block of statements is executed otherwise else-block of statements is executed. /* Compute Area and Perimeter of a circle */ #include <stdio.h> main() { float rad; float area, peri; printf( “Enter the radius “ ); scanf(“%f” , &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %fn” , area ); printf( “Peri = %fn” , peri ); } else printf( “Negative radiusn”); }
  • 28. Lectures on Numerical Methods 28 An Example  Execution /* Compute Area and Perimeter of a circle */ #include <stdio.h> main() { float rad; float area, peri; printf( “Enter the radius “ ); scanf(“%f” , &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %fn” , area ); printf( “Peri = %fn” , peri ); } else printf( “Negative radiusn”); } Variable Before exeution After executio n rad ?? ?? area ?? ?? peri ?? ??
  • 29. Lectures on Numerical Methods 29 An Example  Execution /* Compute Area and Perimeter of a circle */ #include <stdio.h> main() { float rad; float area, peri; printf( “Enter the radius “ ); scanf(“%f” , &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %fn” , area ); printf( “Peri = %fn” , peri ); } else printf( “Negative radiusn”); } Variable Before exeution After executio n rad ?? 5.000000 area ?? ?? peri ?? ??
  • 30. Lectures on Numerical Methods 30 An Example  Execution  Condition rad > 0.0 is true /* Compute Area and Perimeter of a circle */ #include <stdio.h> main() { float rad; float area, peri; printf( “Enter the radius “ ); scanf(“%f” , &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %fn” , area ); printf( “Peri = %fn” , peri ); } else printf( “Negative radiusn”); } Variable Before exeution After executio n rad 5.000000 5.000000 area ?? ?? peri ?? ??
  • 31. Lectures on Numerical Methods 31 An Example  Execution /* Compute Area and Perimeter of a circle */ #include <stdio.h> main() { float rad; float area, peri; printf( “Enter the radius “ ); scanf(“%f” , &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %fn” , area ); printf( “Peri = %fn” , peri ); } else printf( “Negative radiusn”); } Variable Before exeution After executio n rad 5.00000 5.00000 area ?? 78.53975 peri ?? ??
  • 32. Lectures on Numerical Methods 32 An Example  Execution /* Compute Area and Perimeter of a circle */ #include <stdio.h> main() { float rad; float area, peri; printf( “Enter the radius “ ); scanf(“%f” , &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %fn” , area ); printf( “Peri = %fn” , peri ); } else printf( “Negative radiusn”); } Variable Before exeution After executio n rad 5.00000 5.00000 area 78.53975 78.53975 peri ?? 31.41590
  • 33. Lectures on Numerical Methods 33 An Example  Execution /* Compute Area and Perimeter of a circle */ #include <stdio.h> main() { float rad; float area, peri; printf( “Enter the radius “ ); scanf(“%f” , &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %fn” , area ); printf( “Peri = %fn” , peri ); } else printf( “Negative radiusn”); } Variable Before exeution After executio n rad 5.00000 5.00000 area 78.53975 78.53975 peri 31.41590 31.41590
  • 34. Lectures on Numerical Methods 34 An Example  Execution /* Compute Area and Perimeter of a circle */ #include <stdio.h> main() { float rad; float area, peri; printf( “Enter the radius “ ); scanf(“%f” , &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %fn” , area ); printf( “Peri = %fn” , peri ); } else printf( “Negative radiusn”); } Variable Before exeution After executio n rad 5.00000 5.00000 area 78.53975 78.53975 peri 31.41590 31.41590
  • 35. Lectures on Numerical Methods 35 An Example  Execution  Program ends /* Compute Area and Perimeter of a circle */ #include <stdio.h> main() { float rad; float area, peri; printf( “Enter the radius “ ); scanf(“%f” , &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %fn” , area ); printf( “Peri = %fn” , peri ); } else printf( “Negative radiusn”); } Variable Before exeution After executio n rad 5.00000 5.00000 area 78.53975 78.53975 peri 31.41590 31.41590
  • 36. Lectures on Numerical Methods 36 An Example  Execution /* Compute Area and Perimeter of a circle */ #include <stdio.h> main() { float rad; float area, peri; printf( “Enter the radius “ ); scanf(“%f” , &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %fn” , area ); printf( “Peri = %fn” , peri ); } else printf( “Negative radiusn”); } Variable Before exeution After executio n rad ?? -3.00000 area ?? ?? peri ?? ??
  • 37. Lectures on Numerical Methods 37 An Example  Execution  The condition rad > 0.0 is false /* Compute Area and Perimeter of a circle */ #include <stdio.h> main() { float rad; float area, peri; printf( “Enter the radius “ ); scanf(“%f” , &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %fn” , area ); printf( “Peri = %fn” , peri ); } else printf( “Negative radiusn”); } Variable Before exeution After executio n rad -3.00000 -3.00000 area ?? ?? peri ?? ??
  • 38. Lectures on Numerical Methods 38 An Example  Execution  The condition rad > 0.0 is false  Prints “Negative radius” and  Program ends /* Compute Area and Perimeter of a circle */ #include <stdio.h> main() { float rad; float area, peri; printf( “Enter the radius “ ); scanf(“%f” , &rad); if ( rad > 0.0 ) { area = 3.14159 * rad * rad; peri = 6.28318 * rad; printf( “Area = %fn” , area ); printf( “Peri = %fn” , peri ); } else printf( “Negative radiusn”); } Variable Before exeution After executio n rad -3.00000 -3.00000 area ?? ?? peri ?? ??
  • 39. Lectures on Numerical Methods 39 Scanf 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.
  • 40. Lectures on Numerical Methods 40 Scanf Function Format “%d%f” “%3d%6f” Input Intvar Fltvar Intvar Fltvar 3 4.5 3 4.5 3 4.5 3t4.5 (tab) 3 4.5 3 4.5 3n4.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 ??  scanf ( format, &intvar, &fltvar );
  • 41. Lectures on Numerical Methods 41 Algorithms  Problem  Let S be a finite sequence of positive nonzero integers ak, except the last number which is always 0. Find the largest number in the sequence.  Example  7, 9, 4, 6, 2, 5, 8, 0  The largest is 9.  If Sk is a subsequence of first k numbers of S and mk is the largest term of Sk then  mk+1 = max { mk , ak+1 } Algorithm 1. The m1 be the largest in S1. Clearly m1 = a1. 2. Let I = 1. 3. If aI+1 is zero, go to step 7. 4. mI+1 = max { mI, aI+1}. 5. Increment I. 6. Go to step 3. 7. Print mI.
  • 42. Lectures on Numerical Methods 42 An Example  Find Largest  The program produces the following output Enter a number 7 Enter a number 9 Enter a number 4 Enter a number 6 Enter a number 2 Enter a number 0 Largest is 9 /* Find the largest number */ #include <stdio.h> main() { int number, largest; printf( "Enter a number " ); scanf( "%d" , &largest ); printf( "Enter a number " ); scanf( "%d" , &number ); while ( number > 0 ) { if ( number > largest ) largest = number; printf( "Enter a number " ); scanf( "%d" , &number ); } printf( "Largest is %dn" , largest ); }
  • 43. Lectures on Numerical Methods 43 Lab Assignment 1  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.

Editor's Notes

  • #3: Variables are not initialized by the compiler. Undeclared variables are cause compilation errors.
  • #6: Note the indentations