0% found this document useful (0 votes)
19 views60 pages

NMCP20 With 20 C

The document outlines the learning objectives and key concepts of a course on Numerical Methods and Computer Programming, focusing on spreadsheet usage and C programming. It covers fundamental programming elements such as keywords, identifiers, variables, data types, input/output functions, operators, control structures, and conditional statements. Additionally, it explains various loop constructs and control flow mechanisms in C programming.

Uploaded by

prajwal dehankar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views60 pages

NMCP20 With 20 C

The document outlines the learning objectives and key concepts of a course on Numerical Methods and Computer Programming, focusing on spreadsheet usage and C programming. It covers fundamental programming elements such as keywords, identifiers, variables, data types, input/output functions, operators, control structures, and conditional statements. Additionally, it explains various loop constructs and control flow mechanisms in C programming.

Uploaded by

prajwal dehankar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

1

5CE03: Numerical Methods and


Computer Programming
Learning Objectives of Subject:
 To learn the basics of spreadsheets.
 To learn the basic concepts of computing.
 To know the methodology of problem solving.
 To develop skills in programming using C language.

Learning Objectives of Subject: At the end of subject the students will able –
 To use spreadsheet software for solving civil engineering problems.
 To impart knowledge to analyze, solve, design and code numerical method
problems using C language.
 To impart knowledge to analyze, solve, design and code civil engineering
problems using C language.
SECTION – A
SECTION-B
Introduction to C
 Solving any problem in the computers needs software.
Software is the program written in language like FOTRON, C,
C++ etc. in the computer.
 C is a powerful programming language. It can be used to
develop software like operating systems, databases, compilers,
etc.
 For beginners C programming is an excellent language to learn
to program.
 The C Programming language was developed by Dennis
Ritchie. C has been standardized by ANSI since 1989 (ANSI
C) and by the International Organization for
Standardization (ISO).
Keywords
 Keywords are pre-defined, reserved words used in C
programming that have special meanings to the compiler.
Keywords are part of the syntax and they cannot be used
as an identifier. As C is a case sensitive language, all
keywords must be written in lowercase. Here is a list of all
keywords allowed in ANSI C.
A list of all keywords allowed in
ANSI C
Identifiers
Identifier is the name given to entities such as variables, functions,
structures etc. Identifiers must be distinctive. They are created to
give a unique name to an entity to identify it during the execution of
the program. Identifier names should be different from
keywords. Rules for naming identifiers
 A valid identifier can have letters (both uppercase and lowercase
letters), digits and underscores.
 The first letter of an identifier should be either a letter or an
underscore.
 There is no rule on how long an identifier can be. However, one may
run into problems in some compilers if the identifier is longer than
31 characters.
 One can make any name as an identifier by using the above rule.
Variables
 In C programming, a variable is a storage area to hold
data. To indicate the storage area, each variable should be
given a unique name (identifier). Variable names are just
the symbolic representation of a memory location. For
example:
 int StudentMark = 75;
 Here, StudentMark is a variable of int type. Here, the
variable is assigned an integer value 75. The value of a
variable can be changed, hence the name variable.
Rules for naming a variable
 A variable name can only have letters (both uppercase and lowercase
letters), digits and underscore. The first letter of a variable should be
either a letter or an underscore. There is no rule on how long a
variable name (identifier) can be. However, you may run into
problems in some compilers if the variable name is longer than 31
characters.
C is a strongly typed language. This means that the variable type
cannot be changed once it is declared. For example:
int number = 5; // integer variable
number = 5.5; // error
double number; // error
Here, the type of number variable is int. You cannot assign a
floating-point (decimal) value 5.5 to this variable. Also, you cannot
redefine the data type of the variable to double. By the way, to store
the decimal values in C, you need to declare its type to
either double or float.
Literals
 Literals are data used for representing fixed values. They
can be used directly in the code. For
example: 1, 2.5, 'c' etc. Here, 1, 2.5 and 'c' are literals.
Why? You cannot assign different values to these terms.
1. Integers
2. Floating-point Literals
3. Characters
4. String Literals
1. Integers
 An integer is a numeric literal (associated with numbers)
without any decimal or exponential part. There are three types
of integer literals in C programming: 1) decimal (base 10), 2)
octal (base 8), hexadecimal (base 16).
 Decimal integer constants can be any combination of digits.
For example: 0, -6, 24 etc
 Octal integer constants can be any combination of digits from
the set 0 through 7. However, the first digit must be 0. For
example: 024, 076, 0765, etc
 Hexadecimal integer constants should start with 0x. It can then
be any combination of digits taken from the set of 0 through
9and a through f: 0x2f, 0x7a, 0x541 etc
2. Floating-point Literals
 A floating-point literal is a numeric literal that has either a
decimal form or an exponential part. For example: -28.0,
0.002684, -0.22E-6
 Note: E-6 = 10-6
3. Characters
 A character literal is created by enclosing a single
character inside single quotation marks. For
example: 'a', 'm', 'F', '2', '}' etc.
 Addition to these constant, C recognizes special backslash
(Sometimes, it is necessary to use characters that cannot
be typed or has special meaning in C programming. For
example: newline (enter), tab, question mark etc. In order
to use these characters, escape sequences are used. For
example: \n is used for start at newline. The
backslash \ causes escape from the normal way the
characters are handled by the compiler.
4. String Literals
A string literal is a sequence of alphanumeric characters
enclosed in double-quote marks. For example: “Radius”, “
My Name is Ram”

Constants
 Constants are used to define a variable whose value
cannot be changed, one can use the const keyword. This
will create a constant. For example,
const double PI = 3.1416;
Notice, we have added keyword const. Here, PI is a
symbolic constant; its value cannot be changed.
Data Type
In C programming, data types are declarations for variables.
This determines the type and size of data associated with
variables. The different data types used in C, its size and
format specifier used in writing program are as follow.
C Input Output
C Output
Printf() is the main output function used in C. The main use
of this function is to send formatted output to the screen.
Ex:
C Input
 In C programming, scanf() is one of the commonly used
function to take input from the user. The scanf() function
reads formatted input from the standard input such as
keyboards. Ex
C Programming Operators
Different programming operators are used in C language for
performing different operation. It mainly includes
Arithmetic operators, Assignment Operators, Comparison
& Logical operators, Bitwise Logical Operator and some
special operators.
C Arithmetic Operators
 An arithmetic operator performs mathematical operations
such as addition, subtraction, multiplication, division etc
on numerical values (constants and variables).
C Assignment Operators
 An assignment operator is used for assigning a value to
a variable. The most common assignment operator
is =. Table shows different Assignment operators used
in C.
C Comparison & Logical operators
 Comparison and logical operators can be grouped in to two, mainly
relational operators and Logical operators. A relational operator
checks the relationship between two expressions. If the relation is
true, it returns 1; if the relation is false, it returns value 0. Relational
operators are used in decision making and loops.
 An expression containing logical operator returns either 0
or 1 depending upon whether expression results true or
false. Logical operators are commonly used in decision
making in C programming.
C Bitwise Operators
 During computation, mathematical operations like:
addition, subtraction, multiplication, division, etc are
converted to bit-level which makes processing faster and
saves power. Bitwise operators are used in C
programming to perform bit-level operations.
C special operators

 C also use some special operators for simplicity of


program like Incrementer (++), Decrementer (--), comma,
sizeof, etc.
Control Structures
 All modern structured programming languages including
FORTRAN, C, C++ use Control Structures for
Conditional Flow Control. Control Structures represent
structured Conditional Flow Control. C also uses
structured Control Statements called as Control Structures.
 These are structured processes used for conditional flow
control. Control Structures are used for
1 Selection / Selective Execution
2 Iteration / Iterative (Repetitive) Execution
 Selection/ Selective Execution
This process is used for selectively executing a statement or
group of statements based on specified condition.
 Iteration/ Iterative Execution
This process is used for executing a statement or group of
statements repeatedly based on specified condition. Two
forms of Iterative Structures are used. One form has the
decision block at the top of the loop. The other form has
decision block at the bottom of the loop. The first form is
commonly used. The second form is used in special cases.
Conditional statements
The conditional expressions are mainly used for decision
making in the program. Following are the statements used
in C-language.
if statement
The “if” statement is used to evaluate conditional expression
in the program. The “if” statement evaluates the test
expression inside the bracketc (). If the test expression
evaluated is true, statements inside the body
of if statement are executed. And if the test expression
evaluated is false, statements inside the body of if
statement is not executed. The Syntax of ‘if’ statement is
if (conditional expression)
{
//statement;
}
The flow chart to represent the if statement is as follow
If-else statement
 The “if-else” statement is used to evaluate conditional expression. If
the test expression evaluated is true, statements inside the body of “if
statement ” is executed and statements inside the body of “else” are
skipped from the execution. If the test expression evaluated is false,
statements inside the body of “else” are executed and statements
inside the body of “if” are skipped from execution. The Syntax of ‘if-
else’ statement is
if (conditional expression)
{
//Statement 1;
}
else
{
//Statement 2;
}
The flow chart is
Output:
Enter an integer:65
65 is an odd number
If-else Ladder
 When we have to made choice between more than two options, one can
use if-else ladder statement. The if-else ladder allows checking between
multiple test expressions and executing different statements. The Syntax
of ‘if-else ladder’ statement is
if (conditional expression)
{
//Statement 1;
}
else if (conditional expression)
{
//Statement 2;
}
else
{
//Statement 3;
The flow chart for if-else ladder
statement is
Nested if-else
Nested if-else statement can be use to examine condition inside the
other condition. The nested if-else allows checking multiple test
expressions and executing different statements. The Syntax of
‘nested if-else’ statement is
 if (conditional expression)
{ Statements 1;
if (sub conditional expression)
{ Statements 2;
}
else
{ Statements 3;
}
}
else
{Statement 4;
}
The flow chart for Nested if-else
statement is
Loop Statements
 Loop statements are used to solve block statement for
numerous times with specific condition. C language uses
different loop statement to achieve this task. It mainly
include
 for loop
 while loop
 do...while loop
for loop

The most commonly loop statement used in C language is “for


loop”. This loop consists of three expressions.
 The first expression is used to initialize the index value,
 the second to check whether or not the loop is to be continued
again and
 the third change the index value for further iteration.
During execution of “for loop”, the initialization statement is
executed only once. Then, the test expression is evaluated. If
the test expression is evaluated to true, statements inside the
body of the “for” loop are executed and the expression is
updated. Again the test expression is evaluated. This process
goes on until the test expression is false. When the test
expression is false, the loop terminates.
The syntax & Flow chart of the for loop is:

for(initialization Statement (initial condition); test


Expression (test); update Statement (increment or
decrement))
{
// Block statements
}
while loop

 The second type of loop, the while loop is used to repeat a


block of code until a specified condition is met. When the test
expression is false, the loop terminates.
 The while loop evaluates the test condition inside the
brackets (). If test condition is true, statements inside the body
of while loop are executed.
 The while loop does not explicitly contain the initialization and
increment / decrement parts of the loop. These two statements
are normally provided by programmers. Then, test condition is
evaluated again. The process goes on until test condition is
evaluated to false. If test condition is false, the loop terminates
(ends).
The syntax of the while loop is:
while (test condition)
{
// Block statements
}
do-while loop
 The another loop for conditional repetitive execution of
statement is do while. In this the body of do-while loop is
executed once. Only then, the test condition is evaluated. If test
condition is true, the body of the loop is executed again and test
condition is evaluated once more. This process goes on
until test condition becomes false. If test condition is false, the
loop ends. The syntax of the do-while loop is:
do
{
// Block statements
}
while (test condition)
break statement
 Break statement is used to immediate end the loop as per
requirement or condition. Its syntax is
break;
Continue statement

 Continue statement is used to immediate skipping current


iteration in the loop as per requirement or condition. Its
syntax is
continue;
switch Statement

 The switch statement is a special multiway decision maker


that test whether an expression matches one of the number
of constant values, and braces accordingly.
 The expression is evaluated once and compared with the
values of each case label. If there is a match, the
corresponding statements after the matching label are
executed.
 For example, if the value of the expression is equal
to constant2, statements after case constant2: are executed
until break is encountered. If there is no match, the default
statements are executed.
The syntax and Flow chart of the
switch statement is:
switch (expression)
{
case constant1:
// statements
break;

case constant2:
// statements
break;
.
.
default:
// default statements
}
goto Statement

The go to statement is used to alter the normal sequence


of program execution by transferring control to some
other part of the program. In general form the go to
statement is written as
go to label;
where label is an identifier that is used to label the target
statement to which control will be transferred.

You might also like