SlideShare ist ein Scribd-Unternehmen logo
1 von 284
Downloaden Sie, um offline zu lesen
LOGO

Higher Technological Institute
10th of Ramadan City
6th of October Branch
Electrical and Computer Engineering Department

Lecture Notes in

Prepaid By: Eng. Ibrahim Elewah
Main Reference
HTI Student Book and “C For Dummies”

by Dan Gookin 2nd Edition

1
Course Contents
1

Introduction

2

Program Development

3

The Essential of C Programs

4

Manipulating Data with Operators

5

Reading from and Writing to Standard I/O

6

Decision

7

Iteration

8

Arrays

9

C Functions
C Programming

2

Higher Technological Institute
Course Contents
Introduction

1

 Computer Program
 Programming Language


Machine Language



Assembly Language



High Level Language

 The C Programming Language
 Interpreter, Compiler and Assembler
C Programming

3

Higher Technological Institute
Computer Program
A Computer Program is a sequence of instructions written to
perform a specified task with a computer. The program has an
executable form that the computer can use directly to execute the
instructions.
The Executable program in its human-readable form is called the
source code. Computer source code is often written by computer
programmers. It is written in a programming language.
Source Code may be converted into an executable file ( an
executable program or a binary) by a compiler and later executed
by a central processing unit. Alternatively, computer programs may
be executed with the aid of an interpreter, or may be embedded
directly into hardware.

C Programming

4

Higher Technological Institute
Programming Language
A Programming language is an artificial language
designed to communicate instructions to a computer.
Most programming language are purely textual;
they use sequences of text including words, numbers and
punctuation.
The programming languages are divided into three
groups:

1. Machine Language
2. Assembly Language
3. High Level Language
C Programming

5

Higher Technological Institute
Machine Language
Essentially, computers really understand only
one language, which consists of zeroes and ones,
also known as machine language.

But
o Difficult and take a Long Time to Read and Write
o Difficult to Detect and Correct the Errors
o It is also called
C Programming

Binary Code
Assembly Language
o It uses some Mnemonic English codes instead of
Binary Codes.
o Assembly language programs are easier to Read
and Write than machine language programs.
o It Needs an Assembler to translate Mnemonic
codes into Machine Language.

Example
ADD 5,9
SUB 7,3
C Programming

7

Higher Technological Institute
Assembly Language
o It uses some Mnemonic English codes instead of
Binary Codes.
o Assembly language programs are easier to Read
and Write than machine language programs.
o It Needs an Assembler to translate Mnemonic
codes into Machine Language.

Example
ADD 5,9
SUB 7,3
C Programming

But in Large Programs
Writing a program in assembly
language can prove extremely
Time-consuming, and complicated.
8

Higher Technological Institute
High Level Language
Because

writing machine- or assembly-language
programs was so Difficult and Confusing, people
developed additional languages that look more like
human languages

With names such as JAVA, BASIC, and C.
o High Level Language needs Compiler or Interpreter.

C Programming

9

Higher Technological Institute
High Level Language
Readability

Programs are easy to Read

Maintainability Programs are easy to Maintain
Portability

Programs are easy to port across
Different computer Platforms

Reusability

Saving your programs into library
and invoke them in next programs

C Programming

10

Higher Technological Institute
C Programming Language
1. High Level Language.
2. C allows you to get control of computer Hardware.
3. Many other high level language developed based on C.
4. The American National Standard Institute ANSI
define the standard for the C programming language.
5. Your HTI Book focuses on the functions defined in ANSI

See Page

C Programming

11

3

Higher Technological Institute
Interpreter, Compiler and Assembler
Lower Level Language
High Level
Language

Interpreter

Assembly Language
Binary Language

High Level
Language

Compiler

Computer
Instruction

Assembler

Lower Level Language

Binary 0’s and 1’s

Read more Pages 3 and 4
C Programming

12

Higher Technological Institute
Your First C Program

/* This is my First C Program */
#include<stdio.h>
int main ()
{
printf(" Hello World! n ") ;
return 0 ;
}
C Programming

13

Higher Technological Institute
Comments

/* This is my First C Program */
o The compiler ignore everything between two
slashes and asterisks.
o Just to help you document your program.
o C compilers allows to write comments like

/*
This comment does not increase the size of the executable file
(Binary Code), nor does it affect the performance speed

*/
C Programming

14

Higher Technological Institute
Your First C Program

/* This is my First C Program */
#include<stdio.h>
int main ()
{
printf(" Hello World! n ") ;
return 0 ;
}
C Programming

15

Higher Technological Institute
Header Files
stdio.h Header File

#include<stdio.h>
o This Header Files required by include directive
o Header Files extensions .h means header
o The stdio.h header file numerous prototypes and
macros to perform input or output I/O for C Program

C Programming

16

Higher Technological Institute
Your First C Program

/* This is my First C Program */
#include<stdio.h>
int main ()
{
printf(" Hello World! n ") ;
return 0 ;
}
C Programming

17

Higher Technological Institute
Main Function
o Very special function
o Each C Program MUST have a main() function
o The main() function syntax as follows

int main ()
{
return 0 ;
}
C Programming

18

Higher Technological Institute
Your First C Program

/* This is my First C Program */
#include<stdio.h>
int main ()
{
printf(" Hello World! n ") ;
return 0 ;
}
C Programming

19

Higher Technological Institute
Printing On Screen
o To print a text or numbers printf is used.
o Syntax printf(“
”);
o The new line character n
o To print Hello World! write the following statement

printf(" Hello World! n ") ;

C Programming

20

Higher Technological Institute
Your First C Program

/* This is my First C Program */
#include<stdio.h>
int main ()
{
printf(" Hello World! n ") ;
return 0 ;
}
C Programming

21

Higher Technological Institute
References
C Programming

22

Higher Technological Institute
References

Logic Design A

23

Higher Technological Institute
Course Contents
1

Introduction

2

Program Development

3

The Essential of C Programs

4

Manipulating Data with Operators

5

Reading from and Writing to Standard I/O

6

Decision

7

Iteration

8

Arrays

9

C Functions
C Programming

24

Higher Technological Institute
Course Contents
Program Development

2

 Phases of Computer Program Development


Problem Definition and Analysis



Algorithms and Flowcharts



Program Coding



Program Execute and Testing

 Exercise
C Programming

25

Higher Technological Institute
Problem Definition and Analysis
o The problem which you write a program for should be
completely understood by gathering information
about it.
o This information is the program input.
o How information is processed to give the required
output.

oExample
𝟐

𝒂𝒙 + 𝒃𝒙 + 𝒄 = 𝟎
C Programming

26

Higher Technological Institute
Problem Definition and Analysis
o The General Quadratic Equation

𝟐

𝒂𝒙 + 𝒃𝒙 + 𝒄 = 𝟎
C Programming

27

Higher Technological Institute
Problem Definition and Analysis
o The General Quadratic Equation
𝒙=

−𝒃 ±

𝒃 𝟐 − 𝟒𝒂𝒄
𝟐𝒂

𝟐

𝒂𝒙 + 𝒃𝒙 + 𝒄 = 𝟎
C Programming

28

Higher Technological Institute
Problem Definition and Analysis
o The General Quadratic Equation
𝒙=
𝒙𝟏 =

−𝒃 +

𝒃 𝟐 − 𝟒𝒂𝒄
𝟐𝒂

−𝒃 ±

𝒃 𝟐 − 𝟒𝒂𝒄
𝟐𝒂

𝒂𝒏𝒅

𝒙𝟐 =

−𝒃 −

𝒃 𝟐 − 𝟒𝒂𝒄
𝟐𝒂

𝟐

𝒂𝒙 + 𝒃𝒙 + 𝒄 = 𝟎
C Programming

29

Higher Technological Institute
Problem Definition and Analysis
o The General Quadratic Equation
𝒙=
𝒙𝟏 =

−𝒃 +

𝒃 𝟐 − 𝟒𝒂𝒄
𝟐𝒂

−𝒃 ±

𝒃 𝟐 − 𝟒𝒂𝒄
𝟐𝒂

Discriminator

𝒂𝒏𝒅

𝒙𝟐 =

−𝒃 −

𝒃 𝟐 − 𝟒𝒂𝒄
𝟐𝒂

𝟐

𝒂𝒙 + 𝒃𝒙 + 𝒄 = 𝟎
C Programming

30

Higher Technological Institute
Problem Definition and Analysis
o The General Quadratic Equation
𝒙=

𝒃 𝟐 − 𝟒𝒂𝒄
𝟐𝒂

−𝒃 ±

𝒃 𝟐 − 𝟒𝒂𝒄
𝒙𝟏 =
𝟐𝒂
𝑫 = 𝒃 𝟐 − 𝟒𝒂𝒄
𝒊𝒇 𝑫 < 𝟎 𝒙 𝟏 𝒂𝒏𝒅 𝒙 𝟐
𝒊𝒇 𝑫 > 𝟎 𝒙 𝟏 𝒂𝒏𝒅 𝒙 𝟐
−𝒃 +

Discriminator

𝒂𝒏𝒅

𝒙𝟐 =

−𝒃 −

𝒃 𝟐 − 𝟒𝒂𝒄
𝟐𝒂

𝒂𝒓𝒆
𝑰𝒎𝒂𝒈𝒊𝒏𝒂𝒓𝒚
𝒄𝒂𝒏 𝒃𝒆 𝑪𝒂𝒍𝒄𝒖𝒍𝒂𝒕𝒆𝒅

𝟐

𝒂𝒙 + 𝒃𝒙 + 𝒄 = 𝟎
C Programming

31

Higher Technological Institute
Problem Definition and Analysis
o How information is processed to give the required
output ?

𝒂 , 𝒃 𝒂𝒏𝒅 𝒄 𝒂𝒓𝒆 𝑮𝒊𝒗𝒆𝒏 =
𝑰𝒏𝒑𝒖𝒕𝒔
𝟐
𝑫 = 𝒃 − 𝟒𝒂𝒄
Question
D >0 ?
YES 𝒙 𝟏 =
NO

−𝒃+

𝒙 𝟏 𝒂𝒏𝒅

𝒃 𝟐 −𝟒𝒂𝒄
𝟐𝒂

𝒙𝟐

𝒂𝒏𝒅

𝒂𝒓𝒆

𝒙𝟐 =

−𝒃−

𝒃 𝟐 −𝟒𝒂𝒄
𝟐𝒂

𝑰𝒎𝒂𝒈𝒊𝒏𝒂𝒓𝒚

𝟐

𝒂𝒙 + 𝒃𝒙 + 𝒄 = 𝟎
C Programming

32

Higher Technological Institute
Algorithms and Flowcharts

C Programming

33

Higher Technological Institute
Flowchart
Start, Stop ( Begin , End)
Input , Output
Processing Program Instruction

Decision
Connector
Comment
Preparation
C Programming

34

( Instruction that
change the program)
Higher Technological Institute
Flowcharts

𝑫 = 𝒃 𝟐 − 𝟒𝒂𝒄

𝒙 𝟏,𝟐 =

C Programming

−𝒃 ±

𝒃 𝟐 − 𝟒𝒂𝒄
𝟐𝒂

35

Higher Technological Institute
Flowcharts

𝑫 = 𝒃 𝟐 − 𝟒𝒂𝒄

𝒙 𝟏,𝟐 =

C Programming

−𝒃 ±

𝒃 𝟐 − 𝟒𝒂𝒄
𝟐𝒂

36

Higher Technological Institute
Flowcharts

𝑫 = 𝒃 𝟐 − 𝟒𝒂𝒄

𝒙 𝟏,𝟐 =

C Programming

−𝒃 ±

𝒃 𝟐 − 𝟒𝒂𝒄
𝟐𝒂

37

Higher Technological Institute
Flowcharts

𝑫 = 𝒃 𝟐 − 𝟒𝒂𝒄
YES

𝒙 𝟏,𝟐 =

C Programming

−𝒃 ±

NO

𝒃 𝟐 − 𝟒𝒂𝒄
𝟐𝒂

38

Higher Technological Institute
Flowcharts

𝑫 = 𝒃 𝟐 − 𝟒𝒂𝒄
YES

𝒙 𝟏,𝟐 =

C Programming

−𝒃 ±

NO

𝒃 𝟐 − 𝟒𝒂𝒄
𝟐𝒂

39

Higher Technological Institute
Flowcharts

𝑫 = 𝒃 𝟐 − 𝟒𝒂𝒄
YES

𝒙 𝟏,𝟐 =

C Programming

−𝒃 ±

NO

𝒃 𝟐 − 𝟒𝒂𝒄
𝟐𝒂

40

Higher Technological Institute
Flowcharts Examples
Example 1
Draw a flow chart to
read two numbers and
compare between both
if they are equal or not.

C Programming

41

Higher Technological Institute
Flowcharts Examples
Example 1
Draw a flow chart to
read two numbers and
compare between both
if they are equal or not.

C Programming

42

Higher Technological Institute
Flowcharts Examples
Example 2
Draw a flow chart to generate
a table of squares and cubes
of integers from 1 to 9.

C Programming

43

Higher Technological Institute
Flowcharts Examples
Example 2
Draw a flow chart to generate
a table of squares and cubes
of integers from 1 to 9.

C Programming

44

Higher Technological Institute
Flowcharts Examples
Example 3
Draw a flow chart to
determine the minimum
value of three input
numbers.

C Programming

45

Higher Technological Institute
Flowcharts Examples
Example 3
Draw a flow chart to
determine the minimum
value of three input
numbers.

C Programming

46

Higher Technological Institute
Flowcharts Examples
Example 4
Draw a flow chart to generates the famous sequence
of numbers called the Fibonacci series.
Here are the first few terms of the series:
0 1 1 2 3 5 8 13 21 34 55

C Programming

47

Higher Technological Institute
Flowcharts Examples
Example 4
Draw a flow chart to generates the famous sequence
of numbers called the Fibonacci series.
Here are the first few terms of the series:
0 1 1 2 3 5 8 13 21 34 55
Each term is found by adding the two previous
ones:
1+1 is 2, 1+2 is 3, 2+3 is 5, 3+5 is 8, and so on.

C Programming

48

Higher Technological Institute
Flowcharts Examples
Example 4
Draw a flow chart to generates the famous sequence
of numbers called the Fibonacci series.
Here are the first few terms of the series:
0 1 1 2 3 5 8 13 21 34 55
Each term is found by adding the two previous
ones:
1+1 is 2, 1+2 is 3, 2+3 is 5, 3+5 is 8, and so on.

𝒇𝟎 = 𝟎

, 𝒇𝟏 = 𝟏

𝒂𝒏𝒅 𝒇 𝒏 = 𝒇 𝒏−𝟏 + 𝒇 𝒏−𝟐

Terminate the program after calculation the first 10
numbers of the Fibonacci series and print them.
C Programming

49

Higher Technological Institute
Flowcharts Examples
Example 4

0
1
𝒇𝟎 = 𝟎
1
𝒇𝟏 = 𝟏
2
𝒇 𝒏 = 𝒇 𝒏−𝟏 + 𝒇 𝒏−𝟐
3
5
8
13
21
34
55
C Programming

50

Higher Technological Institute
Flowcharts Examples
Example 5
It is desired to compute income Tax for a given
income as per the following table:
Income

Amount of tax

Up to Rs 35000

Nil

From Rs 35001
To Rs 60000

20% of income
in Excess of Rs 35000

From Rs 60001
To Rs 120000

Rs 5000 + 30% of income in
Excess of Rs 60000

More than Rs 120000

Rs 23000+ 40% of income in
Excess of Rs 120000

C Programming

51

Higher Technological Institute
Flowcharts Examples

C Programming

52

Higher Technological Institute
Flowcharts Examples
Example 6
It is desired to sum 20 values read from a data
statement. This can be done by means of a loop. It
uses the following algorithm for summation.

Sj+1 = Sj + A
Where: j = 1,2,3,..20
Sj+1 : The new sum
Sj : The old sum
A : The variable whose values are to be added
C Programming

53

Higher Technological Institute
Flowcharts Examples

C Programming

54

Higher Technological Institute
Flowcharts Examples
Example 7
Draw a flowchart of Program
that Calculate and Display
The Factorial of

C Programming

n

55

Higher Technological Institute
Flowcharts Examples
Example 7
Draw a flowchart of Program
that Calculate and Display
The Factorial of

n

𝒏! = 𝒏 × 𝒏 − 𝟏 × 𝒏 − 𝟐 … × 𝟑 × 𝟐 × 𝟏

C Programming

56

Higher Technological Institute
Flowcharts Examples
Example 7
Draw a flowchart of Program
that Calculate and Display
The Factorial of

n

𝒏! = 𝒏 × 𝒏 − 𝟏 × 𝒏 − 𝟐 … × 𝟑 × 𝟐 × 𝟏
C Programming

57

Higher Technological Institute
Flowcharts Examples
Example 8
Draw a flowchart to compute all possible products of
X and Y
X and Y varying from 0 through 9
(100 products)
0x0=0
0x1=0
0x2=0
0x3=0
.
.
0x9=0

C Programming

1x0=0 …
1x1=1 …
1x2=2 …
1x3=3 …
.
.
1x9=9 …

9x0=0
9x1=9
9x2=18
9x3=27

Home Work ..!

9x9=81

58

Higher Technological Institute
Flowcharts Examples
Example 8

0x0=0
0x1=0
0x2=0
0x3=0
.
.
0x9=0

C Programming

1x0=0 …
1x1=1 …
1x2=2 …
1x3=3 …
.
.
1x9=9 …

9x0=0
9x1=9
9x2=18
9x3=27
9x9=81

59

Higher Technological Institute
Algorithms

C Programming

60

Higher Technological Institute
Program Coding
o In this phase, the algorithm ( or Flowchart ) is
transferred into program using codes of one of
programming language.
o Machine Language ( Binary Code ).
o Assembly Language.
o High Level Language.

C Programming

61

Higher Technological Institute
Program Executing and Testing

Editing
Compiling
Linking

Executing

C Programming

62

Higher Technological Institute
Program Executing and Testing

Editing

C Programming

Means to use any editing
program to convert the program into
electronic form and save it with a
suitable and valid name with the
extension (.c)
The program becomes an
electronic source code

63

Higher Technological Institute
Program Executing and Testing

Compiling

C Programming

Means to use a compiler
program
matches
with
the
programming language the source
program written with, to detect the
linguistic errors and convert the
source code program into a binary
codes.
The program becomes an object
program, saved with the source code
name but with the extension (.0).

64

Higher Technological Institute
Program Executing and Testing

Linking

C Programming

The program when it is written
contains some functions, as printf( )
function which is saved in the library of
the compiler and may be some
external functions written to do certain
jobs by the program after linking with
libraries functions is converted to an
executable program, with the same
name entered by the user but with the
extension .exe (dot exe).

65

Higher Technological Institute
Program Executing and Testing
The program in this step i.e.,
after linking is ready to be run or
debugged on the computer.

Executing

C Programming

66

Higher Technological Institute
Program Executing and Testing

Editing
Compiling
Linking

Executing

C Programming

67

Higher Technological Institute
Steps of Obtaining an Executable Program

C Programming

68

Higher Technological Institute
Course Contents
1

Introduction

2

Program Development

3

The Essential of C Programs

4

Manipulating Data with Operators

5

Reading from and Writing to Standard I/O

6

Decision

7

Iteration

8

Arrays

9

C Functions
C Programming

69

Higher Technological Institute
Course Contents
3

The Essential of C Programs

 Constants and variables
 Expressions

 Arithmetic operators
 Statements

 Statement blocks
 Data Types and Names in C
 Naming a Variable
C Programming

70

Higher Technological Institute
Expressions
o An expression is a combination of constants,
variables, and operators that are used to
denote computations

𝑨= 𝟐∗ 𝑨 − 𝟏
1. Taking the value contained in the drawer
(variable) A
2. Multiply this value by 2
3. Subtract 1 from result obtained from 2
4. The value contained in drawer A is omitted, then
putting the result obtained from 3 into drawer A.
C Programming

71

Higher Technological Institute
Arithmetic Operations
Addition

Subtraction

Multiplication

Division

Reminder

The Reminder operator % is used to obtain
the reminder of the first operand divided by
the second operand
C Programming

72

Higher Technological Institute
Arithmetic Operations
Addition

Subtraction

Multiplication

Division

Reminder

Example
𝟔% 𝟒= 𝟐
C Programming

𝟏𝟎𝟎𝟏%𝟐 = 𝟏
73

𝟒𝟖%𝟓 =
Higher Technological Institute
Arithmetic Operations
Addition

Subtraction

Multiplication

Division

Reminder

Example
𝟔% 𝟒= 𝟐
C Programming

𝟏𝟎𝟎𝟏%𝟐 = 𝟏
74

𝟒𝟖%𝟓 = 𝟑
Higher Technological Institute
Constants and Variables
o As its name implies, a constant is a value that never
changes.
o A variable, on the other hand, can be used to present
different values.
o For instance, consider the following:

𝒊 = 𝟏 ;
o Where the symbol i is a constant because it always has
the same value (1) and the symbol i is assigned the
constant 1.
o In other words, i contains the value of 1 after the
statement is executed.
C Programming

75

Higher Technological Institute
Data Types and Names
o The C language reserves some keywords
words that have special meanings to the
language.
o Those reserved words should not be used
as variables, constants, or function names
in your program.
o All C keywords must be written in
lowercase letters, for instance INT will not
be treated as a keyword, it must be written
as int.
C Programming

76

Higher Technological Institute
The computer list of C keywords
auto
const
double
float
int
short
struct
unsigned
C Programming

break
continue
else
for
long
signed
switch
void

case
default
enum
goto
register
sizeof
typedef
volatile
77

char
do
extern
if
return
static
union
while
Higher Technological Institute
Naming a Variable
Valid Variable Name Can Use
o Characters A through Z and a through z
o Digit characters 0 through 9, which can be
used in any position except the first of a
variable name.
o The underscore character _

Examples
stop_sign
C Programming

loop3
78

and_pause
Higher Technological Institute
Naming a Variable
Invalid Variable Name Can NOT be Used
o
o
o
o

A variable name can’t contain any C arithmetic signs.
A variable name can’t contain any dots.
A variable name can’t contain any apostrophes.
A variable name can’t contain any other special
symbols such as *, @, #, and so on.

Examples
4flags
return
C Programming

sum-result
what_size?
79

method*4
ahmed.ali
Higher Technological Institute
Data Types

C Data Type
char

int

float

double

a, B, $, #

5, 17, 128

2.5 , 0.3

23433.3455

C Programming

80

Higher Technological Institute
Declaration Statement

Type

Name Value

char
int
float
double

c1
N1
F1
d1

char c1

= ‘&’ ;

int

‘&’
100
32/10
5e3

= 100 ;

n1

C Programming

81

Higher Technological Institute
Declaration Statement

Type
char
int
float
double

Name Value
‘&’
100
32/10
5e3

c1
N1
F1
d1

char c1

;

c1

= ‘&’ ;

int

;

n1

= 100 ;

n1

C Programming

82

Higher Technological Institute
Declaration Statement

Type

Name Value

char
int
float
double

float

c1
n1
f1
d1

‘&’
100
32/10
5e3

f1= 32/100 ;

double
C Programming

d1=5e3 ;
83

Higher Technological Institute
Declaration Statement

Type

Name Value

char
int
float
double

‘&’
100
32/10
5e3

c1
n1
f1
d1

float

f1

;

f1

=

32/100 ;

double

d1 ;

d1

=

5e3

C Programming

84

;

Higher Technological Institute
Some special characters in C

b

Backspace

f

Form feed

n

New line

r

Return

t

Tab

C Programming

Moves the cursor to the left
one character
Goes to the top of a new
page
Carriage return and line
feeds
Returns to the beginning of
the current line
Advances to the next tab
stop
85

Higher Technological Institute
Examples
/* Example1 : Printing out characters */
# include <stdio.h>
/* the header file for the printf () function */
main ( )
{
/* the main function body till line 13 */
char c1;
/* declaration of the character variable c1 */
char c2;
/* declaration of the character variable c2 */
c1 = ‘A’; /* assigning c1 with the character constant A */
c2 = ‘a’; /* assigning c2 with the character constant a */
return 0;
}

C Programming

86

Higher Technological Institute
Examples
/* Example1 : Printing out characters */
# include <stdio.h>
/* the header file for the printf () function */
main ( )
{
/* the main function body till line 13 */
char c1;
/* declaration of the character variable c1 */
char c2;
/* declaration of the character variable c2 */
c1 = ‘A’;
/* assigning c1 with the character constant A */
c2 = ‘a’;
/* assigning c2 with the character constant a */
printf ( “ The character c1 is : %c n ”, c1);
printf ( “ The character c1 is : %c ” , c1);
printf ( “ while the character c2 is : %c n”, c2);
return 0;
}
C Programming

87

Higher Technological Institute
Examples
/* Example1 : Printing out characters */
# include <stdio.h>
/* the header file for the printf () function */
main ( )
{
/* the main function body till line 13 */
char c1;
/* declaration of the character variable c1 */
printf ( “ The character c1 is : variablen */”, c1);
%c c2
char c2;
/* declaration of the character
c1 = ‘A’;
/* assigning c1 with the character constant A */
c2 = ‘a’;
/* assigning c2 with the character constant a */
printf ( “ The character c1 is : %c n ”, c1);
printf ( “ The character c1 is : %c ” , c1);
printf ( “ while the character c2 is : %c n”, c2);
return 0;
}
C Programming

88

Higher Technological Institute
Examples
/* Example1 : Printing out characters */
# include <stdio.h>
/* the header file for the printf () function */
main ( )
{
/* the main function body till line 13 */
char c1;
/* declaration of the character variable c1 */
printf ( “ The character c1 is : variablen */”, c1);
%c c2
char c2;
/* declaration of the character
c1 = ‘A’;
/* assigning c1 with the character constant A */
c2 = ‘a’;
/* assigning c2 with the character constant a */
printf ( “ The character c1 is : %c n ”, c1);
printf ( “ The character c1 is : %c ” , c1);
printf ( “ while the character c2 is : %c n”, c2);
return 0;
}
C Programming

89

Higher Technological Institute
Examples
/* Example1 : Printing out characters */
# include <stdio.h>
/* the header file for the printf () function */
main ( )
{
/* the main function body till line 13 */
char c1;
/* declaration of the character variable c1 */
printf ( “ The character c1 is : variablen */”, c1);
%c c2
char c2;
/* declaration of the character
c1 = ‘A’;
/* assigning c1 with the character constant A */
c2 = ‘a’;
/* assigning c2 with the character constant a */
printf ( “ The character c1 is : %c n ”, c1);
printf ( “ The character c1 is : %c ” , c1);
printf ( “ while the character c2 is : %c n”, c2);
return 0;
}
C Programming

90

Higher Technological Institute
Examples
/* Example1 : Printing out characters */
# include <stdio.h>
/* the header file for the printf () function */
main ( )
{
/* the main function body till line 13 */
char c1;
/* declaration of the character variable c1 */
printf ( “ The character c1 is : variablen */”, c1);
%c c2
char c2;
/* declaration of the character
c1 = ‘A’;
/* assigning c1 with the character constant A */
c2 = ‘a’;
/* assigning c2 with the character constant a */
printf ( “ The character c1 is : %c n ”, c1);
printf ( “ The character c1 is : %c ” , c1);
printf ( “ while the character c2 is : %c n”, c2);
return 0;
}

New Line

Specifier
for
Character Data Type

C Programming

91

Higher Technological Institute
Examples
/* Example1 : Printing out characters */
# include <stdio.h>
/* the header file for the printf () function */
main ( )
{
/* the main function body till line 13 */
char c1;
/* declaration of the character variable c1 */
char c2;
/* declaration of the character variable c2 */
c1 = ‘A’;
/* assigning c1 with the character constant A */
c2 = ‘a’;
/* assigning c2 with the character constant a */
printf ( “ The character c1 is : %c n ”, c1);
printf ( “ The character c1 is : %c ” , c1);
printf ( “ while the character c2 is : %c n”, c2);
return 0;
}
C Programming

92

Higher Technological Institute
You Have To Know about Data Types

Type

Name

Value

Specifier

char

c1

‘&’

%c

int

n1

100

%d

float

f1

32/10

%f

double

d1

5e3

%e,%E

C Programming

93

Higher Technological Institute
Examples
/* The arithmetic operations on integers */
# include<stdio.h>
/* the header file for the printf () function */
main ( )
{
/* the main function body till line 15 */
int m = 3;
int n=2;
printf ( "The summation of %d and %d is : %d.n", m, n, m+n);
printf ( "The difference between %d and %d is : %d.n", m, n, m-n);
printf ( "The multiplication of %d by %d is : %d.n", m, n, m*n);
printf ( "The division of %d by %d is : %d.n", m, n, m/n);
printf ( "The remainder of division of %d by %d is : %d.n", m, n, m%n);
return 0 ;
}

C Programming

94

Higher Technological Institute
Operators Precedence
Operator Sign
( )
/
*
+
-

C Programming

Name
Brackets
Division
Multiplication
Addition
Subtraction

95

Higher Technological Institute
Logic Operators

Operator Sign
||
&&
!=

C Programming

Name
OR
AND
NOT

96

Higher Technological Institute
Examples
/* Example3 : Integer vs. floating point divisions */
# include <stdio.h>
/* the header file for the printf ()
function */
main ( )
{
int n1,n2,n3;
float m1,m2,m3;
n1 = 32/10;
m1= 32/10;
n2 = 32.0/10;
m2= 32.0/10;
n3 = 32/10.0;
m3 = 32/10.0;
return 0;
}
C Programming

97

Higher Technological Institute
Examples
/* Example3 : Integer vs. floating point divisions */
# include <stdio.h>
/* the header file for the printf () function */
main ( )
{
int n1,n2,n3;
float m1,m2,m3;
n1 = 32/10;
m1= 32/10;
n2 = 32.0/10;
m2= 32.0/10;
n3 = 32/10.0;
m3 = 32/10.0;
printf ( “ The integer division of 32/10 is :
%d n”, n1);
printf ( “The floating point division of 32/10 is :
%f n”, m1);
printf ( “The integer division of 32.0/10 is :
%d n”, n2);
printf ( “The floating point division of 32.0/10 is :
%f n”, m2);
printf ( “The integer division of 32/10.0 is :
%d n”, n3);
printf ( “The floating point division of 32/10.0 is :
%f n”, m3);
return 0;
}
C Programming

98

Higher Technological Institute
Double Data Type
o Here are two examples:
[mantissa] e [exponent]
[mantissa] E [exponent]

Example
5000
-300
0.0025

5e3.
-3e2
2.5e-3.

Specifier %e or %E
With printf ( )
C Programming

99

Higher Technological Institute
Precedence Example
# include<stdio.h>
main ( )
{
int A,B,C,D,E, X, Y, Z;
A = 20 ; B = 6 ;
C = 3 ; D = 10 ; E = 2 ;
X= E*D – B/C + A ;
Y=(E*D)–(B/C)+ A ;
Z= A*(D–B)/(C+ E);
printf ( “ X= %d n Y= %d n Z=%d n”, X,Y,Z);
return 0 ;
}
C Programming

100

Higher Technological Institute
Precedence Example
# include<stdio.h>
main ( )
{
int A,B,C,D,E, X, Y, Z;
A = 20 ; B = 6 ;
C = 3 ; D = 10 ; E = 2 ;
X= E*D – B/C + A ;
Y=(E*D)–(B/C)+ A ;
Z= A*(D–B)/(C+ E);
printf ( “ X= %d n Y= %d n Z=%d n”, X,Y,Z);
return 0 ;
}
C Programming

101

Higher Technological Institute
Precedence Example
# include<stdio.h>
X(
B/C + A ;
main = ) E * D –
{
int A,B,C,D,E, X, Y, Z;
A = 20 ; B = 6 ;
C = 3 ; D = 10 ; E = 2 ;
X= E*D – B/C + A ;
Y=(E*D)–(B/C)+ A ;
Z= A*(D–B)/(C+ E);
printf ( “ X= %d n Y= %d n Z=%d n”, X,Y,Z);
return 0 ;
}
C Programming

102

Higher Technological Institute
Precedence Example

X= E*D –

X= E*D –

C Programming

B/C

B/C

+ A ;

+ A ;

103

Higher Technological Institute
Precedence Example

X= E*D –

C Programming

B/C + A ;

104

Higher Technological Institute
Precedence Example

X= E*D –

B/C + A ;

A = 20
B=6
C=3
D = 10
E=2

C Programming

( )
*
/
+-

105

Higher Technological Institute
Precedence Example

X= E*D –
2 * 10 –
A = 20

B/C + A ;
6 / 3 + 20

B=6
C=3
D = 10
E=2

C Programming

106

( )
*
/
+-

Higher Technological Institute
Precedence Example

X= E*D –
2 * 10 –
A = 20
B=6
C=3
D = 10
E=2

C Programming

20

B/C + A ;
6 / 3 + 20

–

2

107

+ 20

( )
*
/
+-

Higher Technological Institute
Precedence Example

X= E*D –
2 * 10 –
A = 20
B=6
C=3
D = 10
E=2

C Programming

20

B/C + A ;
6 / 3 + 20

–

2
38

108

+ 20

( )
*
/
+-

Higher Technological Institute
Precedence Example
# include<stdio.h>
main ( )
{
int A,B,C,D,E, X, Y, Z;
A = 20 ; B = 6 ;
C = 3 ; D = 10 ; E = 2 ;
X= E*D – B/C + A ;
Y=(E*D)–(B/C)+ A ;
Z= A*(D–B)/(C+ E);
printf ( “ X= %d n Y= %d n Z=%d n”, X,Y,Z);
return 0 ;
}
C Programming

109

Higher Technological Institute
Precedence Example
# include<stdio.h>
main ( )
{
int A,B,C,D,E, X, Y, Z;
A = 20 ; B = 6 ;
C = 3 ; D = 10 ; E = 2 ;
X= E*D – B/C + A ;
Y=(E*D)–(B/C)+ A ;
Z= A*(D–B)/(C+ E);
printf ( “ X= %d n Y= %d n Z=%d n”, X,Y,Z);
return 0 ;
}
C Programming

110

Higher Technological Institute
Precedence Example
# include<stdio.h>
main = )( E * D ) – ( B / C ) + A ;
Y (
{
int A,B,C,D,E, X, Y, Z;
A = 20 ; B = 6 ;
C = 3 ; D = 10 ; E = 2 ;
X= E*D – B/C + A ;
Y=(E*D)–(B/C)+ A ;
Z= A*(D–B)/(C+ E);
printf ( “ X= %d n Y= %d n Z=%d n”, X,Y,Z);
return 0 ;
}
C Programming

111

Higher Technological Institute
Precedence Example

Y=(E*D)–(B/C)+ A ;

.
.
.

C Programming

112

Higher Technological Institute
Precedence Example

Y=(E*D)–(B/C)+ A ;

.
.
.
38

C Programming

113

Higher Technological Institute
Precedence Example
# include<stdio.h>
main ( )
{
int A,B,C,D,E, X, Y, Z;
A = 20 ; B = 6 ;
C = 3 ; D = 10 ; E = 2 ;
X= E*D – B/C + A ;
Y=(E*D)–(B/C)+ A ;
Z= A*(D–B)/(C+ E);
printf ( “ X= %d n Y= %d n Z=%d n”, X,Y,Z);
return 0 ;
}
C Programming

114

Higher Technological Institute
Precedence Example
# include<stdio.h>
Z( = A * ( D – B ) / ( C + E ) ;
main )
{
int A,B,C,D,E, X, Y, Z;
A = 20 ; B = 6 ;
C = 3 ; D = 10 ; E = 2 ;
X= E*D – B/C + A ;
Y=(E*D)–(B/C)+ A ;
Z= A*(D–B)/(C+ E);
printf ( “ X= %d n Y= %d n Z=%d n”, X,Y,Z);
return 0 ;
}
C Programming

115

Higher Technological Institute
Precedence Example

Z= A*(D–B)/(C+ E);
A = 20
B=6
C=3
D = 10
E=2

C Programming

( )
*
/
+-

116

Higher Technological Institute
Precedence Example

Z= A*(D–B)/(C+ E);
A = 20
B=6
C=3
D = 10
E=2

C Programming

( )
*
/
+-

117

Higher Technological Institute
Precedence Example

Z= A*(D–B)/(C+ E);
A = 20
B=6
C=3
D = 10
E=2

C Programming

20 *

4

/

118

5

( )
*
/
+-

Higher Technological Institute
Precedence Example

Z= A*(D–B)/(C+ E);
A = 20
B=6
C=3
D = 10
E=2

C Programming

20 *

4

/

16

119

5

( )
*
/
+-

Higher Technological Institute
Precedence Example
# include<stdio.h>
main ( )
{
int A,B,C,D,E, X, Y, Z;
A = 20 ; B = 6 ;
C = 3 ; D = 10 ; E = 2 ;
X= E*D – B/C + A ;
Y=(E*D)–(B/C)+ A ;
Z= A*(D–B)/(C+ E);
printf ( “ X= %d n Y= %d n Z=%d n”, X,Y,Z);
return 0 ;
}
C Programming

120

Higher Technological Institute
Precedence Example
# include<stdio.h>
main ( )
{
int A,B,C,D,E, X, Y, Z;
A = 20 ; B = 6 ;
C = 3 ; D = 10 ; E = 2 ;
X= E*D – B/C + A ;
Y=(E*D)–(B/C)+ A ;
Z= A*(D–B)/(C+ E);
printf ( “ X= %d n Y= %d n Z=%d n”, X,Y,Z);
return 0 ;
}
C Programming

121

Higher Technological Institute
Home Work ..!!
Exercises 3 Page 44
C Programming

122

Higher Technological Institute
Course Contents
1

Introduction

2

Program Development

3

The Essential of C Programs

4

Manipulating Data with Operators

5

Reading from and Writing to Standard I/O

6

Decision

7

Iteration

8

Arrays

9

C Functions
C Programming

123

Higher Technological Institute
Course Contents

4

Manipulating Data with Operators

C Programming

124

Higher Technological Institute
Course Contents
4

Manipulating Data with Operators

 Errors Types
 Arithmetic Assignment Operators

 Unary Minus Operator
 The Cast Operator

C Programming

125

Higher Technological Institute
Error Types

Syntax

Execution
Logic

C Programming

126

Higher Technological Institute
Error Types

Syntax
Example

C Programming

They are errors that you
have made in the form
Or syntax of the language
o Spelling a keyword incorrectly
o Errors that are detected
during running of a program
are shown in a dialog box that
appears and the error is
highlighted in the program.
127

Higher Technological Institute
Error Types

Execution
Example

C Programming

If the program has no syntax errors.
The computer can execute it.
During execution, execution errors
may be detected.
o If a number is divided by zero
causes an execution error.
Whenever an execution error
is detected, the computer displays a
dialog box with an error message
and stops executing the program.
128

Higher Technological Institute
Error Types

Logic

C Programming

If the output of the program
does not agree with what is
expected, this is logic error.
The computer cannot detect
such an error because it does not
know the logic of the program
should be.
So, it is your responsibility to
detect logic errors in the program.

129

Higher Technological Institute
Error Types

Syntax

Execution
Logic

C Programming

130

Higher Technological Institute
Arithmetic Assignment Operators
o Here the statement causes the value of the right-handoperand to be assigned to the memory location of the
left-hand-operand.

left-hand-operand = right-hand-operand
a=5 ;
o Statement writes the value of the right-hand-operand
(5) into the memory location of the integer variable a
(which is the left-hand-operand in this case).

b=a=5 ;
C Programming

131

Higher Technological Institute
Assignment and Arithmetic Operators
Operator
+=
-=
*=
/=
%=

C Programming

Description
Addition Assignment Operator
Subtraction Assignment Operator
Multiplication Assignment Operator
Division Assignment Operator
Remainder Assignment Operator

132

Higher Technological Institute
Equivalence of Statements
Statement
x+ = y;
x- = y;
x* = y;
x/ = y;
x% = y;

z=z*x+y;
C Programming

Equivalence
x = x + y;
x = x - y;
x = x * y;
x = x / y;
x = x % y;

?
133

z *= x + y ;
Higher Technological Institute
Equivalence of Statements
Statement
x+ = y;
x- = y;
x* = y;
x/ = y;
x% = y;

z=z*x+y;
C Programming

Equivalence
x = x + y;
x = x - y;
x = x * y;
x = x / y;
x = x % y;

≠
134

z *= x + y ;
Higher Technological Institute
Example
# include <stdio.h>
main ( )
{
int x, y, z;
x =1;
y =3;
printf (“Given x = %d, y = %d, and z = %d,n”, x, y, z);
x= x + y;
printf ( “ x= x +y assigns %d to x;n”, x);
x = 1;
x+= y ;
printf ( “ x+= y assigns %d to x;n”, x);
x = 1;
z = z* x + y;
printf ( “z = z*x+y assigns %d to z;n”, z);
z = 10;
z = z* (x + y);
printf ( “z = z*( x+ y) assigns %d to z;n”, z);
z = 10;
z *= x + y;
printf ( “z *= x+ y assigns %d to z;n”, z);
return 0; }
C Programming

135

z= 10;

Higher Technological Institute
Unary Minus Operator
o Given an integer, you can get its negation by changing
the sign of the integer by using, -, the minus operator,
which is called the unary minus operator.

x = 1.234 ;

-x equals -1.234

o Differ between the unary minus operator and the
subtraction operator.

z = x- -y;

OR

z = x- (-y);

o The first – symbol is used as the subtraction operator
while the second –symbol is the unary minus operator.
C Programming

136

Higher Technological Institute
X++

++X

Increment

X--

--X

Decrement

Post-

Pre-

Incrementing or Decrementing by ONE

C Programming

137

Higher Technological Institute
Example
# include <stdio.h>
main ( )
{
int x=5;
printf (“ X++ %d ,n”, x++);
printf (“++X %d ,n”, ++x);
printf (“ X-- %d ,n”, x--);
printf (“ --X %d ,n”, --x);
return 0;
}
C Programming

138

Higher Technological Institute
Relational Operators
Operator

Description

==
!=
>
<
>=
<=

Equal to
Not equal to
Greater than
Less than
Greater than or equal to
Less than or equal to

C Programming

139

Higher Technological Institute
Relational Operators
x*y < z+3

(x * y) < ( z + 3 )

oAll relational expressions produce a
result of either 0 or 1.
oGiven x = 3 and y = 5, for instance,
the relational expression x < y gives a
result of 1
C Programming

140

Higher Technological Institute
Example
# include <stdio.h>
main ( )
{ int x, y;
double z;
x = 7; y = 25; z = 24.46;
printf (“ Given x = %d, y = %d and z = %2f, n”, x, y, z);
printf ( “ x > = y produces: %d n”, x > =y);
printf ( “x = = y produces: %d n”, x = =y);
printf ( “x < y produces: %d n”, x < y);
printf ( “x > y produces: %d n”, x > y);
printf ( “x != y - 18 produces: %d n”, x ! =y -18);
printf ( “x + y ! = z produces: %d n”, x +y ! = z);
return 0; }
C Programming

141

Higher Technological Institute
Relational Operators
1/2+1/2==1

1 Or 0

This is algebraically True and is supposed to
return 1.
o However, the expression returns 0 which means
that the equal to relationship does not hold.
o This is because the truncation of the integer
division – that is , 1 /2 – produces 0 not 0.5.

C Programming

142

Higher Technological Institute
The Cast Operator
o You can convert one data type to a different one
by prefixing and cast operator to the operand.
o The general form of the cast operator is

(data-type) x
o Here data-type specifies the data type you want
to convert to x is a variable ( or, expression)
that contains the value of the current data type.
o You have to include the parentheses (and) to
make up a cast operator.
C Programming

143

Higher Technological Institute
Example
# include <stdio.h>
main ( )
{
int x, y;
x = 7;
y = 5;
printf ( “ Given x = %d, y = %d, n”, x, y);
printf ( “ x / y produces: %d n”, x /y);
printf ( “(float) x / y produces: %f n”, (float) x / y);
return 0 ;
}

C Programming

144

Higher Technological Institute
Course Contents
1

Introduction

2

Program Development

3

The Essential of C Programs

4

Manipulating Data with Operators

5

Reading from and Writing to Standard I/O

6

Decision

7

Iteration

8

Arrays

9

C Functions
C Programming

145

Higher Technological Institute
Course Contents

5

Reading from and Writing to Standard I/O

C Programming

146

Higher Technological Institute
scanf ( ) function
o The scanf ( ) function is a very important function in C.
o It is used to read data from the standard input device;
the keyboard.
o The syntax for the scanf ( ) function is :

scanf(“

%

% ”,

&

&

);

o Example : to input two variables integer x and float y

scanf(“

C Programming

%d

%f ”,

147

&x

&y );

Higher Technological Institute
scanf ( ) function
o The scanf ( ) function is a very important function in C.
o It is used to read data from the standard input device;
the keyboard.
o One thing you need to be aware of; is that the scanf ( )
function doesn’t actually start reading the input until
the Enter key is pressed.
o Data entered from the keyboard is placed in an input
buffer.
o When the Enter key is pressed, the scanf () function
looks for its input in the buffer.

C Programming

148

Higher Technological Institute
printf( ) function
o The printf ( ) function is a very important function in C.
o It is used to print out messages on the screen.
o The syntax for the printf ( ) function is

printf(“ %

% ”,

);

o Example : to print two variables integer x and float y

printf(“ %d

C Programming

%f ”,

149

x

y

);

Higher Technological Institute
printf( ) function
o The printf ( ) function is a very important function in C.
o It is used to print out messages on the screen.
o The number of expressions is determined by the
number of the format specifiers inside the first
argument.
o The format specifiers and the expressions are
matched in order from left to right, and you should
use exactly the same number of expressions as the
number of format specifiers within the format string.

C Programming

150

Higher Technological Institute
Adding the Minimum Field Width
o A integer is added between the percent sign
(%) and the letter in a format specifier to
specify the minimum field width and ensures
that the output reaches the minimum width.
o For example in %10f

printf(“ %10f ”, x

);

10 is a minimum field width specifier that
ensures that the output is at least 10 character
spacers wide.
C Programming

151

Higher Technological Institute
Example
# include <stdio.h>
main ( )
{
int x=12;
int y=12345;
printf (“%d n”, x);
printf (“%d n”, y);
printf (“%5d n”, x);
printf (“%05d n”, x);
printf (“%2d n”, y);
return 0;
}

C Programming

152

Higher Technological Institute
Example
# include <stdio.h>
main ( )
{
int x=12;
int y=12345;
printf (“%d n”, x);
printf (“%d n”, y);
printf (“%5d n”, x);
printf (“%05d n”, x);
printf (“%2d n”, y);
return 0;
}

C Programming

153

12
12345
12
00012
12345

Higher Technological Institute
Aligning Output
o By default, all output is placed on the Right edge of
the field, as long as the field width is longer than the
width of the output.
o You can change this and force output to be leftjustified.
o To do so, you need to prefix the minimum field
specifier with the minus sign (-).

%-12d
o Specifies the minimum field width as 12, and justifies
the output from the left edge of the field.
C Programming

154

Higher Technological Institute
Example
# include <stdio.h>
main ( )
{
int x,y,z,m,n;
m=1234;
printf (“%8d %-8d
printf (“%8d %-8d
printf (“%8d %-8d
printf (“%8d %-8d
printf (“%8d %-8d
return 0;
}

C Programming

x=1;
y=12; z=123;
n=12345;
n”, x, x );
n”, y, y );
n”, z, z );
n”, m, m );
n”, n, n );

155

Higher Technological Institute
Example
# include <stdio.h>
main ( )
{
int x,y,z,m,n;
m=1234;
printf (“%8d %-8d
printf (“%8d %-8d
printf (“%8d %-8d
printf (“%8d %-8d
printf (“%8d %-8d
return 0;
}

C Programming

x=1;
y=12; z=123;
n=12345;
n”, x, x );
n”, y, y );
n”, z, z );
n”, m, m );
n”, n, n );

1
12
123
1234
12345
156

1
12
123
1234
12345
Higher Technological Institute
The Precision Specifier
o You can put a period (.) and an integer right after the
minimum field width specifier.
o The combination of the period and the integer make
up a precision specifier.
o The precision specifier is another important specifier
you can use to determine the number of decimal
places for floating-point numbers, or to specify the
maximum field width for integers or strings.

%10.3f
o The minimum field width length is specified as 10
character long, and the number of decimal places is
set to 3.
C Programming

157

Higher Technological Institute
The Precision Specifier
o You can put a period (.) and an integer right after the
minimum field width specifier.
o The combination of the period and the integer make
up a precision specifier.
o The precision specifier is another important specifier
you can use to determine the number of decimal
places for floating-point numbers, or to specify the
maximum field width for integers or strings.
o Remember, the default number of decimal places is 6.

C Programming

158

Higher Technological Institute
Example
# include <stdio.h>
main ( )
{
int x=123;
float y=123.456789;
printf (“Default integer format n
printf (“With precision specifier n
printf (“Default float format
n
printf (“With precision specifier n
printf (“With precision specifier n
return 0;
}

C Programming

159

%d
n”, x );
%2.8d n”, x );
%f
n”, y );
%10.2f n”, y );
%-10.2f n”, y );

Higher Technological Institute
Example
# include <stdio.h>
main ( )
{
int x=123;
float y=123.456789;
printf (“Default integer format n
printf (“With precision specifier n
printf (“Default float format
n
printf (“With precision specifier n
printf (“With precision specifier n
return 0;
}

C Programming

160

%d
n”, x );
%2.8d n”, x );
%f
n”, y );
%10.2f n”, y );
%-10.2f n”, y );

Higher Technological Institute
Course Contents
1

Introduction

2

Program Development

3

The Essential of C Programs

4

Manipulating Data with Operators

5

Reading from and Writing to Standard I/O

6

Decision

7

Iteration

8

Arrays

9

C Functions
C Programming

161

Higher Technological Institute
Course Contents

6

Decision

C Programming

162

Higher Technological Institute
Course Contents
Decision

6

 The if statement


Flowchart of if statement



The if else statement and flowchart



Nested if statements and their flowcharts

 The switch statement
 The break and continue statement
 The goto statements
C Programming

163

Higher Technological Institute
The if statement
o The if statement is the most popular conditional
branching statement.
o It can be used to evaluate the conditions as well as to
make the decision whether the block of code
controlled by the statement is going to be executed.
o Here if condition is logical TRUE, the statements inside
the braces are executed.
o If condition is logical FALSE, then the statements are
skipped.

C Programming

164

Higher Technological Institute
The if statement Flowchart
IF Statement Syntax
…..
if ( Test Expression)
{
statement1;
statement2;
…
}
….
C Programming

165

Higher Technological Institute
The if statement Flowchart
IF Statement Syntax
…
if ( Test Expression)
statement;
…

Example
if ( x>5)
printf( “ X > 5 ” );
…
C Programming

166

Higher Technological Institute
Example
# include <stdio.h>
main ( )
{
int i;
printf (“Integers that can be divided by 3 n ” );
printf (“Enter a positive number : n ”,
x );
scanf(“%d”, &i );
if ( i %3 == 0 )
printf (“The number %d is divisible by 3 n ”, i );
return 0;
}
C Programming

167

Higher Technological Institute
Output
Integers that can be divided by 3
Enter a positive number : 12
The number 12 is divisible by 3
Integers that can be divided by 3
Enter a positive number : 7

C Programming

168

Higher Technological Institute
The if else statement
if else Statement Syntax
…
if ( Test Expression) {
statement1;
statement2;
…
}
else {
statement_A
statement_B
…
}
…
C Programming

169

Higher Technological Institute
The if else statement
if else Statement Syntax
…
if ( Test Expression)
statement1;
else
statementA;
…

Example
if ( X > 5)
printf( “ X > 5 ” );
else
printf( “ X < 5 ” );
C Programming

170

Higher Technological Institute
if else Example
Draw a flow chart to
read two numbers and
compare between both
if they are equal or not.

C Programming

171

Higher Technological Institute
if else Example
# include <stdio.h>
main ( )
{
int x,y;
printf (“Please Enter X and Y n ” );
scanf(“%d %d”, &x , &y );
if ( x == y )
printf (“ X = Y n ” );
else
printf (“ X != Y n ” );
return 0; }
C Programming

172

Higher Technological Institute
Nested if Flowchart
Nest IF Statement Syntax
if ( Test Expression1)
{
if ( Test Expression2)
{
statement1;
statement2;
}
else
{
statementA;
statementB;
}
}else
{
statement X ;
}
C Programming

173

Higher Technological Institute
Nested if Flowchart
Draw a flow chart to
determine the minimum
value of three input
numbers.

C Programming

174

Higher Technological Institute
Nested if Example
# include <stdio.h>
main ( )
{
int A,B,C;
printf (“Please Enter A,B and C n ” );
scanf(“%d
%d
%d”, &A , &B , &C );
if ( A<B )
{
if ( A<C )
printf (“ A The is the Smallest n ” );
else
printf (“ C The is the Smallest n ” );
} else if ( B<C )
printf (“ B The is the Smallest n ” );
else
printf (“ C The is the Smallest n ” );
return 0; }
C Programming

175

Higher Technological Institute
The switch statement
o The nested if statements will become very complex if
there are many decisions that need to be made.
o The switch statement, can be used to make unlimited
decisions or choices based on the value of a conditional
expression and specified cases.
o The conditional expression is evaluated first, if the
return value of the conditional expression is equal to
the constant expression expression 1, the statement
statement 1 is executed.
o the value of the conditional expression is not equal to
any values of the constant expressions labeled by the
case keyword, the statement (statement-default)
following by the default keyword is executed.
C Programming

176

Higher Technological Institute
The Switch Statement Syntax
switch ( Conditional Expression )
{
case expression1;
statment1;
case expression2;
statment2;
…
default;
statement-default;
}
C Programming

177

Higher Technological Institute
The Switch Statement Syntax
switch ( Conditional Expression ) {
case expression1;
statment1;
case expression2;
statment2;
…
default;
statement-default;
}

o You have to use the case keyword to label each case.
o The default keyword is recommended to be used for
the default case.
o Note that no constant expressions are identical in the
switch statement.
C Programming

178

Higher Technological Institute
The break statement
o An important feature of the switch statement is that
the computer continues to execute the statements
following the selected case until the end of the switch
statement.
o You can add a break statement at the end of the
statement list following every case label, if you want
to exit the switch construct after the statements
within a selected case are executed

C Programming

179

Higher Technological Institute
The Break Statement Syntax
switch ( Conditional Expression )
{
case expression1;
statment1;
break ;
case expression2;
statment2;
break;
…
default;
statement-default;
}
C Programming

180

Higher Technological Institute
Example
Write Program that compute the grade for a student, the
grade is based on Total Score T
( from 0 to 100) The Grading Scale Is Illustrated In The
Following Table:
Total Score
85 <= T <= 100
80 <= T < 85
75 <= T < 80
70 <= T < 75
65 <= T < 70
55 <= T < 65
50 <= T < 55
T < 50
C Programming

Letter Grade
A
B+
B
C+
C
D+
D
F
181

Higher Technological Institute
Example
# include <stdio.h>
main ( )
{ int score;
printf (" Enter Scorre: n");
scanf ("%d", &score);
switch (score / 10 ) {
case 10:
printf ("your grade is A n");
break;
case 9:
printf ("your grade is A n");
break;
case 8:
printf ("your grade is B n");
break;
case 7:
printf ("your grade is C n");
break;
C Programming

case 6:
printf ("your grade is D n");
break;
case 5:
printf ("your grade is D n");
break;
case 4:
printf ("your grade is F n");
break;
case 3:
printf ("your grade is F n");
break;
case 2:
printf ("your grade is F n");
break;
case 1:
printf ("your grade is F n");
break;

182

case 0:
printf ("your grade is F n");
break;
default:
printf ( "illegal input n");
break;
}
printf ( "GOODBYEn" );
return 0;
}

Higher Technological Institute
The Continue statement
o There are times when you want to stay in a loop
but skip over some statements within the loop.
o To do this, the continue statement causes
execution to jump to the top of the loop
immediately.
o The statement can be used within a while,
do…while or for statement to terminate the
current iteration of the loop and begin the
next.

C Programming

183

Higher Technological Institute
The goto statement
o Do not use the goto statement unless it’s absolutely
necessary because its usage may make the C program
unreliable and hard to debug.
o The Syntax of goto Statement:

…
lablename :
statment1;
statment2;
..
goto
lablename ;
…
C Programming

184

Higher Technological Institute
goto Example
# include <stdio.h>
main ( )
{
int x;
guess_lbl:
printf (“Guess integer number in the range 1 to 10 n”);
scanf (“%d”, &x );
if ( x == 7)
goto rght_lbl;
printf (“NO, wrong guess, try again n”);
goto guess_lbl;
rght_lbl:
printf (“WELL DONE your guess = %d is rightn” , x);
return 0;
}
C Programming
Higher Technological Institute
185
goto Example
# include <stdio.h>
main ( )
{
int x;
guess_lbl:
printf (“Guess integer number in the range 1 to 10 n”);
scanf (“%d”, &x );
if ( x == 7) goto rght_lbl;
printf (“NO, wrong guess, try again n”);
goto guess_lbl;
rght_lbl:
printf (“WELL DONE your guess = %d is rightn” , x);
return 0;
}
C Programming

186

Higher Technological Institute
Course Contents
1

Introduction

2

Program Development

3

The Essential of C Programs

4

Manipulating Data with Operators

5

Reading from and Writing to Standard I/O

6

Decision

7

Iteration

8

Arrays

9

C Functions
C Programming

187

Higher Technological Institute
Course Contents

7

Iteration

C Programming

188

Higher Technological Institute
Course Contents
7

Iteration

 The for statement
 The Null Statement

 The while statement
 The infinite while Loop

 The do-while Statement
 The Nested Loop
 Examples
C Programming

189

Higher Technological Institute
Iteration
o Iteration (looping) is used in programming
to perform the same set of statements
over and over until certain specified
conditions are met.

Repeat the same statement(s)
until certain condition

C Programming

190

Higher Technological Institute
Iteration
Example
Draw a flow chart to generate
a table of squares and cubes
of integers from 1 to 9.

C Programming

191

Higher Technological Institute
Iteration
Example
Draw a flow chart to generate
a table of squares and cubes
of integers from 1 to 9.

From
1 to 9
Step 1
C Programming

192

Higher Technological Institute
The for statement
for Statement Syntax One Statement
for ( initialization ; Test Expression ; Step)
statement;
…

for Statement Syntax Multiple Statements
for ( initialization ; Test Expression ; Step)
{
statement1;
statement2;
…
}
C Programming

193

Higher Technological Institute
The for statement
for ( i=1 ; i <= 10 ; i++ )
printf (“ Hello n ”);
printf (“ Goodbye n ”);
for ( i=1 ; i <= 10 ; i++ )
{
printf (“ Hello n ”);
printf (“ Goodbye n ”);
}
C Programming

194

Will Print
Hello
10 Times
Then Print
Goodbye
Will Print
Hello
Goodbye
10 Times
Higher Technological Institute
Example
# include <stdio.h>
main ( )
{
int i=1;
int j,k;
for ( i=1 ; i <= 9 ; i++ )
{
j=i*i;
k=j*i;
printf (“ i = %d ”, i ) ;
printf (“ j = %d ”, j ) ;
printf (“ k = %d n”, k ) ;
}
return 0;
}
C Programming

195

Higher Technological Institute
Null statement

for ( i=1 ; i <= 10 ; i++ ) ;
printf (“ Hello n ”);
printf (“ Goodbye n ”);

C Programming

196

Higher Technological Institute
The while statement
while Statement Syntax
Initialization
while ( Test Expression )
{
…
Increment Or Decrement ;
}

C Programming

197

Higher Technological Institute
The while statement
int w = 1;
while ( w <= 10 )
{
printf (“ Hello n ”);
w = w++;
}
printf (“ Goodbye n ”);

C Programming

198

Will Print
Hello
10 Times
Then Print
Goodbye

Higher Technological Institute
While Loop Example
# include <stdio.h>
main ( )
{
int i=1;
int j,k;
while (i<=10)
{
j=i*i; k=j*i;
printf (“ i = %d ”, i ) ;
printf (“ j = %d ”, j ) ;
printf (“ k = %d n”, k ) ;
i=i+1;
}
return 0; }
C Programming

199

Higher Technological Institute
The Infinite while loop
while(1)
{
Statement1;
Statement2;
}
Always returns 1, the statements inside the
statement block will be executed over and
over- that is, the while loop will continue
forever.
You can set certain conditions inside the while loop
to break the infinite loop as soon as the
conditions are met.
C Programming

200

Higher Technological Institute
The do-while statement
do-while Statement Syntax
Initialization
do
{
…
Increment Or Decrement ;

;

} while ( Test Expression )

C Programming

201

Higher Technological Institute
The do-while statement
do-while Statement Syntax
Initialization
do
{
…
Increment Or Decrement ;

;

} while ( Test Expression )

Only after do-while loop
Don’t forget to write
C Programming

;

202

Higher Technological Institute
The do-while statement
int w = 1;
do
{
printf (“ Hello n ”);
w = w++;
} while ( w <= 10 ) ;

Will Print
Hello
10 Times
Then Print
Goodbye

printf (“ Goodbye n ”);
C Programming

203

Higher Technological Institute
do-while Loop Example
# include <stdio.h>
main ( )
{
int i=1; int j,k;
do
{
j=i*i;
k=j*i;
printf (“ i = %d ”, i ) ;
printf (“ j = %d ”, j ) ;
printf (“ k = %d n”, k ) ;
i=i+1;
} while (i<=10);
return 0; }
C Programming

204

Higher Technological Institute
Examples
Example 1
Write a program to calculate the
summation and Average of 20 values
read from a data statement.

C Programming

205

Higher Technological Institute
Examples
# include <stdio.h>
main ( )
{
int i,x,sum;
float av;
sum=0;
for ( i=1 ; i<=20 ; i++ )
{
printf(“Please enter new no n”);
scanf(“%d”,&x);
sum=sum+x;
}
av=sum/20;
printf(“Sum=%d n Av=%fn”, sum , av);
return 0; }

C Programming

206

Higher Technological Institute
Examples
Example 2
Write a program to calculate the
summation and Average of N numbers
read from a data statement.

C Programming

207

Higher Technological Institute
Examples
# include <stdio.h>
main ( )
{
int i,n,x,sum;
float av;
sum=0;
printf(“How many Numbers ? n”);
scanf(“%d”,&n);
for ( i=1 ; i<=n ; i++ )
{
printf(“Please enter new no n”);
scanf(“%d”,&x);
sum=sum+x;
}
av=sum/n;
printf(“Sum=%d n Av=%fn”, sum , av);
return 0; }
C Programming
208

Higher Technological Institute
Flowcharts Examples
Example 3 Nested Loop
Write a program and Draw its flowchart to compute all
possible products of X and Y
X and Y varying from 1 through 9
(100 products)
1x0=0
1x1=0
1x2=0
1x3=0
.
.
1x9=0
C Programming

2x0=0
2x1=1
2x2=2
2x3=3
.
.
2x9=9
209

…
…
…
…

9x0=0
9x1=9
9x2=18
9x3=27

…

9x9=81
Higher Technological Institute
Flowcharts Examples
Example 3 Nested Loop
# include <stdio.h>
int main ( )
{
int x,y;
for ( x=1 ; x <= 9 ; x++)
for ( y=1 ; y <= 9 ; y++)
printf(“%d x %d = %dn”,x,y,x*y);
return 0;
}

C Programming

210

Higher Technological Institute
Flowcharts Examples
Example 4
Draw a flowchart and write a Program that
Calculate and Display The Factorial of

n

𝒏! = 𝒏 × 𝒏 − 𝟏 × 𝒏 − 𝟐 … × 𝟑 × 𝟐 × 𝟏

C Programming

211

Higher Technological Institute
Flowcharts Examples
# include <stdio.h>
int main ( )
{
int n,fact;
fact=1;
printf(“Enter your N n”);
scanf(“%d”,&n);
for( int i=1;i<=n;i++)
fact=fact*i;
printf(“ N! =%d n”,fact);
return 0;
}
C Programming

212

Higher Technological Institute
Home Work..!!

Rewrite The previous
examples using:
a) while Loop
b) do-while Loop

C Programming

213

Higher Technological Institute
Course Contents
1

Introduction

2

Program Development

3

The Essential of C Programs

4

Manipulating Data with Operators

5

Reading from and Writing to Standard I/O

6

Decision

7

Iteration

8

Arrays

9

C Functions
C Programming

214

Higher Technological Institute
Course Contents

8

Arrays

C Programming

215

Higher Technological Institute
Examples
Example 2
Write a program to calculate the
summation and Average of N numbers
read from a data statement.

C Programming

216

Higher Technological Institute
Examples
# include <stdio.h>
main ( )
{
int i,n,x,sum;
float av;
sum=0;
printf(“How many Numbers ? n”);
scanf(“%d”,&n);
for ( i=1 ; i<=n ; i++ )
{
printf(“Please enter new no n”);
scanf(“%d”,&x);
sum=sum+x;
}
av=sum/n;
printf(“Sum=%d n Av=%fn”, sum , av);
return 0; }
C Programming
217

Higher Technological Institute
Arrays
 An array is a collection of variables that are of the same
data type.
 Each item in an array is called an element.
 All elements in an array are referenced by the name of
the array and are stored in a set of consecutive memory
slots.
 The array takes two forms :


One dimensional array (1-D) ; where the data is arranged
horizontally or vertically.



Multi-dimensional array; where the data is arranged in two
dimensional (2-D), three dimensional (3-D),…etc.

C Programming

218

Higher Technological Institute
Arrays
 The following is the syntax form to declare an array:

data-type

Array-Name [Array-Size];

 The data-type is the type specifier that indicates what
data type the declared array will be.
 Array-Name is the name of the declared array. When
choosing a name for an array, you should follow the
same rules of naming a variable.
 Array-Size defines how many elements the array can
contain. Note that the brackets are required in
declaring an array.
C Programming

219

Higher Technological Institute
Arrays Example

int array_int [8];
 Where int specifies the data type of the
array whose name is arrat_int.
 The size of the array is 8, which means that
the array can store 8 elements

C Programming

220

Higher Technological Institute
Indexing Arrays
 You can access each of the elements in the array
separately.
 The following declaration declares an array
called day of integer numbers:

int day [7];
 You can access the elements in the array of day
one after another: day[0], day[1], …, day[6].
 The important thing to remember is that all
arrays in C are indexed starting at 0.

 The first element in the array of day is day[0].
C Programming

221

Higher Technological Institute
Initializing Arrays
 You can initialize each element in an array by
two way.
 For instance if we have the array :

int number [5];
 Initialize the first element in the array: like this:

number[0] = 24;
 Here the value 24 is assigned to the first element
of the array number, number[0].
C Programming

222

Higher Technological Institute
Initializing Arrays
 The second way to initialize an array is to initialize
all elements in the array together.
 For instance, the following statement initializes all
elements of the array, number:

int number [5]= {100, 4, 24, 34,5, 16};
 Here the integers inside the braces are assigned to
the corresponding elements of the array number.
 That is, 100 is given to the first element
(number[0]), 4 to the second element (number[1]),
and so on.
C Programming

223

Higher Technological Institute
Initializing Arrays
 The second way to initialize an array is to initialize
all elements in the array together.
 For instance, the following statement initializes all
elements of the array, number:

int number [5]= {100, 4, 24};
 Here the integers inside the braces are assigned to
the corresponding elements of the array number.
 That is, 100 is given to the first element
(number[0]), 4 to the second element (number[1]),
and so on.
C Programming

224

Higher Technological Institute
Initializing Arrays
# include <stdio.h>
main ( )
{
int i;
int A[10];
for ( i=0 ; i<10 ; i++ )
{
A[i]=i+1;
printf(“A[%d]=%d n”,i,A[i]);
}
return 0; }
C Programming

225

Higher Technological Institute
Initializing Arrays
# include <stdio.h>
main ( )
{
int i;
int A[10];
for ( i=0 ; i<10 ; i++ )
{
A[i]=i+1;
printf(“A[%d]=%d n”,i,A[i]);
}
return 0; }
C Programming

226

Higher Technological Institute
Example
Example
Write a program to calculate the
Summation and Average of 10 numbers read
from a data statement.( Using Array )

C Programming

227

Higher Technological Institute
Example
# include <stdio.h>
main ( )
{
int i; int A[10];
int sum=0;
float av;
for ( i=0 ; i<10 ; i++ )
{
printf(“Enter the value of Element [%d] n”,i);
scanf(“%d”,A[i]);
sum=sum+A[i]
}
av=sum/10;
printf(“Sum=%d n Av=%fn”, sum , av);
return 0;
}
C Programming

228

Higher Technological Institute
Two Dimensional Arrays

Rows

int A[3][4] ;
Columns

C Programming

229

Higher Technological Institute
Two Dimensional Arrays

Assigning Static Value

int A[3][4] = { { 1 , 2 , 3 , 4 },
{ 5 , 6 , 7 , 8 },
{ 9 , 8 , 7 , 6 }} ;
OR
A[3][4] = { { 1 , 2 , 3 , 4 }, { 5 , 6 , 7 , 8 }, { 9 , 8 , 7 , 6 }} ;
C Programming

230

Higher Technological Institute
Two Dimensional Arrays
Example 1
Write a program to calculate and display
the Summation of two Matrices A and B.

A=

𝟏
𝟒
𝟕

C Programming

𝟐
𝟓
𝟖

𝟑
𝟔
𝟗

,B=

231

𝟗
𝟔
𝟑

𝟖
𝟓
𝟐

𝟕
𝟒
𝟏

Higher Technological Institute
Example
# include <stdio.h>
main ( )
{
int A[3][3] = {
{1,2,3},
{4,5,6},
{ 7 , 8, 9 } } ;
int B[3][3] = {
{9,8,7},
{6,5,4},
{ 3 , 2, 1 } } ;
return 0; }

C Programming

232

Higher Technological Institute
Example
# include <stdio.h>
main ( )
{
int A[3][3] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8, 9 } } ;
int B[3][3] = { { 9 , 8 , 7 } , { 6 , 5 , 4 } , { 3 , 2, 1 } } ;

return 0;

C Programming

}
233

Higher Technological Institute
Example
# include <stdio.h>
main ( )
{
int A[3][3] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8, 9 } } ;
int B[3][3] = { { 9 , 8 , 7 } , { 6 , 5 , 4 } , { 3 , 2, 1 } } ;
int S[3][3] ; int i,j;
for ( i=0 ; i<3 ; i++ ) /* Rows*/
for ( j=0 ; j<3 ; j++ ) /* Columns*/
{
S[i][j]=A[i][j]+B[i][j];
printf(“S[%d][%d] = %d n ”, i , j , S [ i ][ j ] ) ; }
return 0; }
C Programming

234

Higher Technological Institute
Example
S3,3 = A3,3 + B3,3
=

𝟏
𝟒
𝟕

𝟐
𝟓
𝟖

𝟑
𝟗
𝟔 + 𝟔
𝟗
𝟑

𝟖
𝟓
𝟐

𝟕
𝟒 =
𝟏

𝟏𝟎
𝟏𝟎
𝟏𝟎

𝟏𝟎
𝟏𝟎
𝟏𝟎

𝟏𝟎
𝟏𝟎
𝟏𝟎

at i = 0
for ( i=0 ; i<3 ; i++ ) /* Rows*/
j=0
for ( j=0 ; j<3 ; j++ ) /* Columns*/
{
S[i][j]=A[i][j]+B[i][j];
printf(“S[%d][%d] = %d n ”, i , j , S [ i ][ j ] ) ; }
return 0; }
C Programming

235

Higher Technological Institute
Example
S3,3 = A3,3 + B3,3
=

𝟏
𝟒
𝟕

𝟐
𝟓
𝟖

𝟑
𝟗
𝟔 + 𝟔
𝟗
𝟑

𝟖
𝟓
𝟐

𝟕
𝟒 =
𝟏

𝟏𝟎
𝟏𝟎
𝟏𝟎

𝟏𝟎
𝟏𝟎
𝟏𝟎

𝟏𝟎
𝟏𝟎
𝟏𝟎

at i = 0
for ( i=0 ; i<3 ; i++ ) /* Rows*/
j=1
for ( j=0 ; j<3 ; j++ ) /* Columns*/
{
S[i][j]=A[i][j]+B[i][j];
printf(“S[%d][%d] = %d n ”, i , j , S [ i ][ j ] ) ; }
return 0; }
C Programming

236

Higher Technological Institute
Example
S3,3 = A3,3 + B3,3
=

𝟏
𝟒
𝟕

𝟐
𝟓
𝟖

𝟑
𝟗
𝟔 + 𝟔
𝟗
𝟑

𝟖
𝟓
𝟐

𝟕
𝟒 =
𝟏

𝟏𝟎
𝟏𝟎
𝟏𝟎

𝟏𝟎
𝟏𝟎
𝟏𝟎

𝟏𝟎
𝟏𝟎
𝟏𝟎

at i = 1
for ( i=0 ; i<3 ; i++ ) /* Rows*/
j=1
for ( j=0 ; j<3 ; j++ ) /* Columns*/
{
S[i][j]=A[i][j]+B[i][j];
printf(“S[%d][%d] = %d n ”, i , j , S [ i ][ j ] ) ; }
return 0; }
C Programming

237

Higher Technological Institute
Example
S3,3 = A3,3 + B3,3
=

𝟏
𝟒
𝟕

𝟐
𝟓
𝟖

𝟑
𝟗
𝟔 + 𝟔
𝟗
𝟑

𝟖
𝟓
𝟐

𝟕
𝟒 =
𝟏

𝟏𝟎
𝟏𝟎
𝟏𝟎

𝟏𝟎
𝟏𝟎
𝟏𝟎

𝟏𝟎
𝟏𝟎
𝟏𝟎

at i = 2
for ( i=0 ; i<3 ; i++ ) /* Rows*/
j=0
for ( j=0 ; j<3 ; j++ ) /* Columns*/
{
S[i][j]=A[i][j]+B[i][j];
printf(“S[%d][%d] = %d n ”, i , j , S [ i ][ j ] ) ; }
return 0; }
C Programming

238

Higher Technological Institute
Example
S3,3 = A3,3 + B3,3
=

𝟏
𝟒
𝟕

𝟐
𝟓
𝟖

𝟑
𝟗
𝟔 + 𝟔
𝟗
𝟑

𝟖
𝟓
𝟐

𝟕
𝟒 =
𝟏

𝟏𝟎
𝟏𝟎
𝟏𝟎

𝟏𝟎
𝟏𝟎
𝟏𝟎

𝟏𝟎
𝟏𝟎
𝟏𝟎

at i = 2
for ( i=0 ; i<3 ; i++ ) /* Rows*/
j=2
for ( j=0 ; j<3 ; j++ ) /* Columns*/
{
S[i][j]=A[i][j]+B[i][j];
printf(“S[%d][%d] = %d n ”, i , j , S [ i ][ j ] ) ; }
return 0; }
C Programming

239

Higher Technological Institute
Example
# include <stdio.h>
main ( )
{
int A[3][3] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8, 9 } } ;
int B[3][3] = { { 9 , 8 , 7 } , { 6 , 5 , 4 } , { 3 , 2, 1 } } ;
int S[3][3] ; int i,j;
for ( i=0 ; i<3 ; i++ ) /* Rows*/
for ( j=0 ; j<3 ; j++ ) /* Columns*/
{
S[i][j]=A[i][j]+B[i][j];
printf(“S[%d][%d] = %d n ”, i , j , S [ i ][ j ] ) ; }
return 0; }
C Programming

240

Higher Technological Institute
Two Dimensional Arrays
Example 2
Write a program to calculate and display
the Summation of any two 3x3 Matrices.

C Programming

241

Higher Technological Institute
Example 2
# include <stdio.h>
main ( )
{
int A[3][3] , B[3][3] , S[3][3]; int i,j ;
.
.
.
.
for ( i=0 ; i<3 ; i++ ) /* Rows*/
for ( j=0 ; j<3 ; j++ ) /* Columns*/
{
S[i][j]=A[i][j]+B[i][j];
printf(“S[%d][%d] = %d n ”, i , j , S [ i ][ j ] ) ; }
return 0;
}
C Programming

242

Higher Technological Institute
Example 2
# include <stdio.h>
main ( )
{
int A[3][3] , B[3][3] , S[3][3]; int i,j ;
for ( i=0 ; i<3 ; i++ ) /* Rows of Metrics A*/
for ( j=0 ; j<3 ; j++ ) /* Columns of Metrics A */
{
printf(“Please Enter A[%d][%d] = n ”, i , j ) ;
scanf(“%d”,A[i][j]);
}
for ( i=0 ; i<3 ; i++ ) /* Rows*/
for ( j=0 ; j<3 ; j++ ) /* Columns*/
{
S[i][j]=A[i][j]+B[i][j];
printf(“S[%d][%d] = %d n ”, i , j , S [ i ][ j ] ) ; }
return 0;
}
C Programming

243

Higher Technological Institute
Example 2
# include <stdio.h>
main ( )
{
int A[3][3] , B[3][3] , S[3][3];
int i,j ;
for ( i=0 ; i<3 ; i++ ) /* Rows of Metrics A*/
for ( j=0 ; j<3 ; j++ ) /* Columns of Metrics A */
{
printf(“Please Enter A[%d][%d] = n ”, i , j ) ;
scanf(“%d”,A[i][j]);
for ( i=0 ; i<3 ; i++ ) /* Rows of Metrics B*/
for ( j=0 ; j<3 ; j++ ) /* Columns of Metrics B */
{
printf(“Please Enter B[%d][%d] = n ”, i , j ) ;
scanf(“%d”, B[i][j]);
for ( i=0 ; i<3 ; i++ ) /* Rows*/
for ( j=0 ; j<3 ; j++ ) /* Columns*/
{
S[i][j]=A[i][j]+B[i][j];
printf(“S[%d][%d] = %d n ”, i , j , S [ i ][ j ] ) ; }
return 0;
}
C Programming

244

}

}

Higher Technological Institute
Two Dimensional Arrays
Example 3
Write a program to calculate and display
the Summation of any two 3x3 Matrices.
Then from Summation Matrix Find :
a) The Largest and Smallest element.
b) The average
c) Matrix Transpose

C Programming

245

Higher Technological Institute
Example 3
# include <stdio.h>
main ( )
{
int A[3][3] , B[3][3] , S[3][3];
int i,j ;
for ( i=0 ; i<3 ; i++ ) /* Rows of Metrics A*/
for ( j=0 ; j<3 ; j++ ) /* Columns of Metrics A */
{
printf(“Please Enter A[%d][%d] = n ”, i , j ) ;
scanf(“%d”,A[i][j]);
for ( i=0 ; i<3 ; i++ ) /* Rows of Metrics B*/
for ( j=0 ; j<3 ; j++ ) /* Columns of Metrics B */
{
printf(“Please Enter B[%d][%d] = n ”, i , j ) ;
scanf(“%d”, B[i][j]);
for ( i=0 ; i<3 ; i++ ) /* Rows*/
for ( j=0 ; j<3 ; j++ ) /* Columns*/
S[i][j]=A[i][j]+B[i][j];
return 0;
}
C Programming

246

}

}

Higher Technological Institute
Example 3
# include <stdio.h>
main ( )
{
int A[3][3] , B[3][3] , S[3][3];
{

{

int i,j ;

for ( i=0 ; i<3 ; i++ ) /* Rows of Metrics A*/
for ( j=0 ; j<3 ; j++ ) /* Columns of Metrics A */
printf(“Please Enter A[%d][%d] = n ”, i , j ) ;
scanf(“%d”,A[i][j]);
for ( i=0 ; i<3 ; i++ ) /* Rows of Metrics B*/
for ( j=0 ; j<3 ; j++ ) /* Columns of Metrics B */
printf(“Please Enter B[%d][%d] = n ”, i , j ) ;
scanf(“%d”, B[i][j]);
for ( i=0 ; i<3 ; i++ ) /* Rows*/
for ( j=0 ; j<3 ; j++ ) /* Columns*/

}

}

S[i][j]=A[i][j]+B[i][j];

int L=S[0][0], M=S[0][0];
for ( i=0 ; i<3 ; i++ ) /* Rows*/
for ( j=0 ; j<3 ; j++ ) /* Columns*/
if (L<S[i][j])
L=S[i][j];
return 0;
}
C Programming

247

Higher Technological Institute
Example 3 (a)
# include <stdio.h>
main ( )
{ int A[3][3] , B[3][3] , S[3][3];
int i,j ;
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
{ printf(“Please Enter A[%d][%d] = n ”, i , j ) ;
scanf(“%d”,A[i][j]);
}
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
{ printf(“Please Enter B[%d][%d] = n ”, i , j ) ;
scanf(“%d”, B[i][j]);
}
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
S[i][j]=A[i][j]+B[i][j];
int L=S[0][0], M=S[0][0];
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
if (L<S[i][j])
L=S[i][j];
C Programming

248

for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
if (M>S[i][j])
M=S[i][j];
printf(“Largest Element = %d n ”, L ) ;
printf(“Smallest Element = %d n ”, M ) ;
return 0;
}

Higher Technological Institute
Example 3 (a) (b)
# include <stdio.h>
main ( )
{ int A[3][3] , B[3][3] , S[3][3];
int i,j ;
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
{ printf(“Please Enter A[%d][%d] = n ”, i , j ) ;
scanf(“%d”,A[i][j]);
}
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
{ printf(“Please Enter B[%d][%d] = n ”, i , j ) ;
scanf(“%d”, B[i][j]);
}
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
S[i][j]=A[i][j]+B[i][j];
int L=S[0][0], M=S[0][0];
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
if (L<S[i][j])
L=S[i][j];
C Programming

249

for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
if (M>S[i][j])
M=S[i][j];
printf(“Largest Element = %d n ”, L ) ;
printf(“Smallest Element = %d n ”, M ) ;
int sum = 0 ;
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
sum=sum + S[i][j];
float average =sum/9 ;
printf(“Average = %d n ”, average ) ;
return 0;
}

Higher Technological Institute
Example 3 (a) (b) (c)
# include <stdio.h>
main ( )
{ int A[3][3] , B[3][3] , S[3][3];
int i,j ;
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
{ printf(“Please Enter A[%d][%d] = n ”, i , j ) ;
scanf(“%d”,A[i][j]);
}
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
{ printf(“Please Enter B[%d][%d] = n ”, i , j ) ;
scanf(“%d”, B[i][j]);
}
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
S[i][j]=A[i][j]+B[i][j];
int L=S[0][0], M=S[0][0];
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
if (L<S[i][j])
L=S[i][j];
C Programming

250

for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
if (M>S[i][j])
M=S[i][j];
printf(“Largest Element = %d n ”, L ) ;
printf(“Smallest Element = %d n ”, M ) ;
int sum = 0 ;
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
sum=sum + S[i][j];
float average =sum/9 ;
printf(“Average = %d n ”, average ) ;
int ST[3][3];
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
ST[i][j]=S[j][i];
return 0;
}

Higher Technological Institute
Example 3 (a) (b) (c)
# include <stdio.h>
main ( )
{ int A[3][3] , B[3][3] , S[3][3];
int i,j ;
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
{ printf(“Please Enter A[%d][%d] = n ”, i , j ) ;
scanf(“%d”,A[i][j]);
}
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
{ printf(“Please Enter B[%d][%d] = n ”, i , j ) ;
scanf(“%d”, B[i][j]);
}
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
S[i][j]=A[i][j]+B[i][j];
int L=S[0][0], M=S[0][0];
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
if (L<S[i][j])
L=S[i][j];
C Programming

251

for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
if (M>S[i][j])
M=S[i][j];
printf(“Largest Element = %d n ”, L ) ;
printf(“Smallest Element = %d n ”, M ) ;
int sum = 0 ;
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
sum=sum + S[i][j];
float average =sum/9 ;
printf(“Average = %d n ”, average ) ;
int ST[3][3];
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ ) Do Not Forget to
ST[i][j]=S[j][i];
add the display
statements for ST
return 0;
}

Higher Technological Institute
Example 3 (a) (b) (c)
# include <stdio.h>
main ( )
{ int A[3][3] , B[3][3] , S[3][3];
int i,j ;
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
{ printf(“Please Enter A[%d][%d] = n ”, i , j ) ;
scanf(“%d”,A[i][j]);
}
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
{ printf(“Please Enter B[%d][%d] = n ”, i , j ) ;
scanf(“%d”, B[i][j]);
}
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
S[i][j]=A[i][j]+B[i][j];
int L=S[0][0], M=S[0][0];
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
if (L<S[i][j])
L=S[i][j];
C Programming

252

for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
if (M>S[i][j])
M=S[i][j];
printf(“Largest Element = %d n ”, L ) ;
printf(“Smallest Element = %d n ”, M ) ;
int sum = 0 ;
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
sum=sum + S[i][j];
float average =sum/9 ;
printf(“Average = %d n ”, average ) ;
int ST[3][3];
for ( i=0 ; i<3 ; i++ )
for ( j=0 ; j<3 ; j++ )
{ printf (“n”); ST[i][j]=S[j][i];
printf (“%d”, ST[i][j]);
}
return 0;
}
Higher Technological Institute
Matrix Multiplication
Example 4
Write a program to calculate and display
the Multiplication of any two 3x3 Matrices.

C Programming

253

Higher Technological Institute
Two Dimensional Arrays
Remember

=
New Matrix
Dimensions
C Programming

254

Higher Technological Institute
Example 4
# include <stdio.h>
main ( )
{
int A[4][3]={ 2 , 3 , 4 ,
1, 2 ,1,
2 , 1 ,0,
1 , 0 , 2 };
int B[3][4]={ 10 , 0 , 20 , 10 ,
0 , 20 , 0 , 20,
20 , 10 , 10 , 0 };
int M[4][4];
int ra = 4 , ca = 3 , rb = 3, cb = 4 ;
int i , j , k ;
for (i=0;i<ra;i++)
for (j=0;j<cb;j++)
{
M[i][j]=0;
for(k=0;k<ca;k++)
M[i][j]= M[i][j]+ A[i][k]*B[k][j];
}
C Programming

255

printf("n The M is n");
for(i=0;i<ra;i++)
{
printf("n");
for (j=0;j<cb;j++)
printf("%4d",M[i][j]);
}
return 0 ;
}

Higher Technological Institute
Home Work ..!!

Write a program to calculate the
determent of 3x3 Matrix.

C Programming

256

Higher Technological Institute
Course Contents
1

Introduction

2

Program Development

3

The Essential of C Programs

4

Manipulating Data with Operators

5

Reading from and Writing to Standard I/O

6

Decision

7

Iteration

8

Arrays

9

C Functions
C Programming

257

Higher Technological Institute
Course Contents

9

C Functions
C Programming

258

Higher Technological Institute
Functions

Why Function
o To make large programs Manageable,
Programmers modularize them into
subprograms.
o These subprograms are called

Functions
C Programming

259

Higher Technological Institute
Functions

C Standard Functions
OR

Built in Function

C Programming

260

Higher Technological Institute
C Standard Functions
 The C language has a standard library which is a
collection of predefined functions and other program
elements which are accessed through header files.
 One of the most important header file <math.h>
contains useful mathematical functions.

Note
Every mathematical function return double
data type
C Programming

261

Higher Technological Institute
Some Mathematical in <math.h>
Function

Description

cos(x)
exp(x)
log(x)
log10(x)
pow(x,p)
sin(x)
sqrt(x)
tan(x)

Cosine of x in Radian
Exponential of x (base e)
Natural Logarithm of x (base e)
Common Logarithm of x (base e)
x to the power p = xP
Sine of x in Radian
Square root of x = 𝒙
Tangent of x in Radian

C Programming

262

Higher Technological Institute
Example 1
Write a program to calculate the following:

a) cos (x)
b) e x
c) x 3/2
d) 𝒙

Test your program with
x = 8, 16, 32

C Programming

263

Higher Technological Institute
Example 1
# include <stdio.h>
# include <math.h>
main ( )
{
int x=8;
double a,b,c,d;
do
{
a = cos(x*180/(22/7));
b=exp(x);
c=pow(x, 3/2);
d=sqrt(x);
printf(“At x = %d n A = %f n B= %f n C= %f n D=%f n ”,x,a,b,c,d);
x=x*2;
} while ( x<=32 );
return 0;
}

C Programming

264

Higher Technological Institute
Example 1
# include <stdio.h>
# include <math.h>
main ( )
{
int x=8;
double a,b,c,d;
do
{
a = cos(x*180/(22/7));
b=exp(x);
c=pow(x, 3/2);
d=sqrt(x);
printf(“At x = %d n A = %f n B= %f n C= %f n D=%f n ”,x,a,b,c,d);
x=x*2;
} while ( x<=32 );
return 0;
}

C Programming

265

Higher Technological Institute
Example 1
# include <stdio.h>
# include <math.h>
main ( )
{
int x=8;
double

math

b

Function 1
Function 2
…
…
exponential Function
…
…

;

b=exp(x);

main
x

8

b
return 0;
}

C Programming

266

Higher Technological Institute
Example 1
# include <stdio.h>
# include <math.h>
main ( )
{
int x=8;
double

math

b

;

8

Function 1
Function 2
…
…
exponential Function
…
…

b=exp(x);

main
x

8

b
return 0;
}

C Programming

267

Higher Technological Institute
Example 1
# include <stdio.h>
# include <math.h>
main ( )
{
int x=8;
double

math

b

;

8

Function 1
Function 2
…
…
exponential Function
…
…

b=exp(x);

main
x

8

b 8886110.520508
return 0;
}

C Programming

268

Higher Technological Institute
Some Header Files in C Library
Header
Description
File
<ctype.h> Defines function to test characters
<float.h> Defines constant to float
Defines the integer limits on your local
<limits.h> computer

<math.h>
<stdio.h>
<stdlib.h>
<string.h>
<time.h>
C Programming

Defines mathematical function
Defines functions for standard input and output

Defines utility functions
Defines functions for processing string
Defines time and date functions
269

Higher Technological Institute
User Defined Function

You Have to Know
o Function Type
o Function Name
o Arguments to the function

Type

Name

Arguments

void

Func_1

with

returnable

n1

without

C Programming

270

Higher Technological Institute
Build Your Function
Func_Type Func_name( type1 Arg1, type2 Arg2 )
{
statment1 ;
statment2 ;
.
.
.

Function’s
Body

return variable;
}

C Programming

271

Higher Technological Institute
Where I can put my Function ??
# include <stdio.h>
func_type
Func_1( Argument1, Argument2 )
{
statment1 ;
statment2 ;
…
…
return ;
}
main ( )
{
…
Func1 Call ;
…
return 0;
}

Function’s Body

Main Function

C Programming

272

Higher Technological Institute
Where I can put my Function ??
OR

Function Declaration

# include <stdio.h>

func_type
main ( )
{
…
Func1 Call ;
…
return 0;
}
func_type

{
statment1 ;
statment2 ;
…
…
return ;
}
C Programming

Func_1( typeArgument1, typeArgument2 )

;

Main Function
Func_1( Argument1, Argument2 )

Function’s Body
273

Higher Technological Institute
Where I can put my Function ??
Function Declaration
# include <stdio.h>

func_type
main ( )
{
…
Func1 Call ;
…
return 0;
}
func_type

{
statment1 ;
statment2 ;
…
…
return ;
}
C Programming

Func_1( typeArgument1, typeArgument2 )

;

Main Function
Func_1( Argument1, Argument2 )

Please Do not
forget ; after
Declaration Statement

Function’s Body
274

Higher Technological Institute
Simple Example
# include <stdio.h>
int sum ( int x, int y)
{
z = x + y;
return z ;
}
main ( )
{
int a, b, c;
a = 3;
b = 5;
c = sum( a , b );
printf("C=%d n", c);
return 0;
}
C Programming

275

Higher Technological Institute
Simple Example
# include <stdio.h>
int sum ( int x, int y)
{
z = x + y;
return z ;
}
main ( )
{
int a, b, c;
a = 3;
b = 5;
c = sum( a , b );
printf("C=%d n", c);
return 0;
}
C Programming

276

Higher Technological Institute
Simple Example
# include <stdio.h>
int sum ( int x, int y)
{
z = x + y;
return z ;
}
main ( )
{
int a, b, c;
a = 3;
b = 5;
c = sum( a , b );
printf("C=%d n", c);
return 0;
}
C Programming

277

Higher Technological Institute
Simple Example
# include <stdio.h>
int sum ( int x, int y)
{
z = x + y;
return z ;
}
• Why we use x, y,z in Function’s Body
main ( )
While use a,b,c in main Function …?
{
• Effect of use the same variable name.
int a, b, c;
a = 3;
b = 5;
c = sum( a , b );
printf("C=%d n", c);
return 0;
}
C Programming

278

Higher Technological Institute
Simple Example by Method 2
# include <stdio.h>
int sum ( int, int ) ;
main ( )
{
int a, b, c;
a = 3;
b = 5;
c = sum( a , b );
printf("C=%d n", c);
return 0;
}
int sum ( int x, int y)
{
z = x + y;
return z ; }
C Programming

279

Higher Technological Institute
Simple Example by Method 2
# include <stdio.h>
int sum ( int, int ) ;
main ( )
Again Do not
{
forget ; after
Declaration Statement
int a, b, c;
a = 3;
b = 5;
c = sum( a , b );
printf("C=%d n", c);
return 0;
}
int sum ( int x, int y)
{
z = x + y;
return z ; }
C Programming

280

Higher Technological Institute
Why Functions

Example
Write a program to calculate the
factorial of any integer number. Then use
your program to calculate the value of Y.

𝒏! − 𝒌!
𝒀=
𝒏− 𝒌 !

C Programming

281

Higher Technological Institute
Example
# include <stdio.h>
main ( )
{
int n ;
printf(“Please Enter The Value of n n");
scanf(“%d”,&n);
int factorial =1;
for ( int i=n ; i>=1 ; i-- )
factorial = factorial * i ;
printf(“Factorial =%d n", factorial);
return 0;
}

C Programming

282

Higher Technological Institute
Example
# include <stdio.h>
int fact ( int ) ;
main ( )
{
int n, k, y;
printf(“Please Enter The Value of n and k n");
scanf(“%d %d”,&n,&k);
y = ( fact(n) * fact(k) ) / fact( n – k ) ;
printf(“Y=%d n", y);
return 0;
}
int fact ( int fc )
{
int factorial =1;
for ( int i=fc ; i>=1 ; i-- )
factorial = factorial * i ;
return factorial ;
}
C Programming

283

Higher Technological Institute
LOGO

Higher Technological Institute
10th of Ramadan City
6th of October Branch
Electrical and Computer Engineering Department

Eng. Ibrahim Elewah
HTI Student Book
Main Reference “C For Dummies”

by Dan Gookin 2nd Edition

28

Weitere ähnliche Inhalte

Was ist angesagt?

Algorithm and flowchart
Algorithm and flowchartAlgorithm and flowchart
Algorithm and flowchartSachin Goyani
 
1. over view and history of c
1. over view and history of c1. over view and history of c
1. over view and history of cHarish Kumawat
 
An overview of computers and programming languages
An overview of computers and programming languages An overview of computers and programming languages
An overview of computers and programming languages Ahmad Idrees
 
Generations of programming_language.kum_ari11-1-1-1
Generations of programming_language.kum_ari11-1-1-1Generations of programming_language.kum_ari11-1-1-1
Generations of programming_language.kum_ari11-1-1-1lakshmi kumari neelapu
 
Programming Fundamentals lecture 1
Programming Fundamentals lecture 1Programming Fundamentals lecture 1
Programming Fundamentals lecture 1REHAN IJAZ
 
Programming aids- Algorithm, Flowchart, Pseudocodes and Decision table
Programming aids- Algorithm, Flowchart, Pseudocodes and Decision tableProgramming aids- Algorithm, Flowchart, Pseudocodes and Decision table
Programming aids- Algorithm, Flowchart, Pseudocodes and Decision tableAnjali Technosoft
 
Introduction to Assembly Language Programming
Introduction to Assembly Language ProgrammingIntroduction to Assembly Language Programming
Introduction to Assembly Language ProgrammingRahul P
 
Generations of programming language
Generations of programming languageGenerations of programming language
Generations of programming languageJAIDEVPAUL
 
Introduction to Computer
Introduction to ComputerIntroduction to Computer
Introduction to Computerzaheeriqbal41
 
Chapter 1 - An Introduction to Programming
Chapter 1 - An Introduction to ProgrammingChapter 1 - An Introduction to Programming
Chapter 1 - An Introduction to Programmingmshellman
 
C programming interview questions
C programming interview questionsC programming interview questions
C programming interview questionsadarshynl
 
What is keyword in c programming
What is keyword in c programmingWhat is keyword in c programming
What is keyword in c programmingRumman Ansari
 
Language translator
Language translatorLanguage translator
Language translatorasmakh89
 
Introduction to c_language
Introduction to c_languageIntroduction to c_language
Introduction to c_languageWay2itech
 
notes on Programming fundamentals
notes on Programming fundamentals notes on Programming fundamentals
notes on Programming fundamentals ArghodeepPaul
 

Was ist angesagt? (20)

Algorithm and flowchart
Algorithm and flowchartAlgorithm and flowchart
Algorithm and flowchart
 
1. over view and history of c
1. over view and history of c1. over view and history of c
1. over view and history of c
 
An overview of computers and programming languages
An overview of computers and programming languages An overview of computers and programming languages
An overview of computers and programming languages
 
Generations of programming_language.kum_ari11-1-1-1
Generations of programming_language.kum_ari11-1-1-1Generations of programming_language.kum_ari11-1-1-1
Generations of programming_language.kum_ari11-1-1-1
 
Programming Fundamentals lecture 1
Programming Fundamentals lecture 1Programming Fundamentals lecture 1
Programming Fundamentals lecture 1
 
Programming aids- Algorithm, Flowchart, Pseudocodes and Decision table
Programming aids- Algorithm, Flowchart, Pseudocodes and Decision tableProgramming aids- Algorithm, Flowchart, Pseudocodes and Decision table
Programming aids- Algorithm, Flowchart, Pseudocodes and Decision table
 
C language
C languageC language
C language
 
COMPUTER PROGRAMMING
COMPUTER PROGRAMMINGCOMPUTER PROGRAMMING
COMPUTER PROGRAMMING
 
C tutorial
C tutorialC tutorial
C tutorial
 
C notes
C notesC notes
C notes
 
Introduction to Assembly Language Programming
Introduction to Assembly Language ProgrammingIntroduction to Assembly Language Programming
Introduction to Assembly Language Programming
 
Batch FIles
Batch FIlesBatch FIles
Batch FIles
 
Generations of programming language
Generations of programming languageGenerations of programming language
Generations of programming language
 
Introduction to Computer
Introduction to ComputerIntroduction to Computer
Introduction to Computer
 
Chapter 1 - An Introduction to Programming
Chapter 1 - An Introduction to ProgrammingChapter 1 - An Introduction to Programming
Chapter 1 - An Introduction to Programming
 
C programming interview questions
C programming interview questionsC programming interview questions
C programming interview questions
 
What is keyword in c programming
What is keyword in c programmingWhat is keyword in c programming
What is keyword in c programming
 
Language translator
Language translatorLanguage translator
Language translator
 
Introduction to c_language
Introduction to c_languageIntroduction to c_language
Introduction to c_language
 
notes on Programming fundamentals
notes on Programming fundamentals notes on Programming fundamentals
notes on Programming fundamentals
 

Ähnlich wie Computer programming all chapters

Fundamentals of programming with C++
Fundamentals of programming with C++Fundamentals of programming with C++
Fundamentals of programming with C++Seble Nigussie
 
Introduction
IntroductionIntroduction
IntroductionKamran
 
Chapter 1 - Basic concepts of programming.pdf
Chapter 1 - Basic concepts of programming.pdfChapter 1 - Basic concepts of programming.pdf
Chapter 1 - Basic concepts of programming.pdfKirubelWondwoson1
 
What is turbo c and how it works
What is turbo c and how it worksWhat is turbo c and how it works
What is turbo c and how it worksMark John Lado, MIT
 
C notes by m v b reddy(gitam)imp notes all units notes 5 unit order
C notes by m v b  reddy(gitam)imp  notes  all units notes  5 unit orderC notes by m v b  reddy(gitam)imp  notes  all units notes  5 unit order
C notes by m v b reddy(gitam)imp notes all units notes 5 unit orderMalikireddy Bramhananda Reddy
 
ch01_an overview of computers and programming languages
ch01_an overview of computers and programming languagesch01_an overview of computers and programming languages
ch01_an overview of computers and programming languagesLiemLe21
 
L1. Basic Programming Concepts.pdf
L1. Basic Programming Concepts.pdfL1. Basic Programming Concepts.pdf
L1. Basic Programming Concepts.pdfMMRF2
 
C++ language basic
C++ language basicC++ language basic
C++ language basicWaqar Younis
 
C language introduction geeksfor geeks
C language introduction   geeksfor geeksC language introduction   geeksfor geeks
C language introduction geeksfor geeksAashutoshChhedavi
 

Ähnlich wie Computer programming all chapters (20)

Introduction to c language
Introduction to c language Introduction to c language
Introduction to c language
 
Fundamentals of programming with C++
Fundamentals of programming with C++Fundamentals of programming with C++
Fundamentals of programming with C++
 
Introduction
IntroductionIntroduction
Introduction
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
Chapter 1 - Basic concepts of programming.pdf
Chapter 1 - Basic concepts of programming.pdfChapter 1 - Basic concepts of programming.pdf
Chapter 1 - Basic concepts of programming.pdf
 
C AND DATASTRUCTURES PREPARED BY M V B REDDY
C AND DATASTRUCTURES PREPARED BY M V B REDDYC AND DATASTRUCTURES PREPARED BY M V B REDDY
C AND DATASTRUCTURES PREPARED BY M V B REDDY
 
What is turbo c and how it works
What is turbo c and how it worksWhat is turbo c and how it works
What is turbo c and how it works
 
C notes by m v b reddy(gitam)imp notes all units notes 5 unit order
C notes by m v b  reddy(gitam)imp  notes  all units notes  5 unit orderC notes by m v b  reddy(gitam)imp  notes  all units notes  5 unit order
C notes by m v b reddy(gitam)imp notes all units notes 5 unit order
 
C programming Tutorial Session 1
C programming Tutorial Session 1C programming Tutorial Session 1
C programming Tutorial Session 1
 
ch01_an overview of computers and programming languages
ch01_an overview of computers and programming languagesch01_an overview of computers and programming languages
ch01_an overview of computers and programming languages
 
Session1 c1
Session1 c1Session1 c1
Session1 c1
 
trial
trialtrial
trial
 
Book ppt
Book pptBook ppt
Book ppt
 
Microsoft C# programming basics
Microsoft C# programming basics  Microsoft C# programming basics
Microsoft C# programming basics
 
chapter 1.pptx
chapter 1.pptxchapter 1.pptx
chapter 1.pptx
 
L1. Basic Programming Concepts.pdf
L1. Basic Programming Concepts.pdfL1. Basic Programming Concepts.pdf
L1. Basic Programming Concepts.pdf
 
CS3251-_PIC
CS3251-_PICCS3251-_PIC
CS3251-_PIC
 
Abc c program
Abc c programAbc c program
Abc c program
 
C++ language basic
C++ language basicC++ language basic
C++ language basic
 
C language introduction geeksfor geeks
C language introduction   geeksfor geeksC language introduction   geeksfor geeks
C language introduction geeksfor geeks
 

Kürzlich hochgeladen

Unveiling the Intricacies of Leishmania donovani: Structure, Life Cycle, Path...
Unveiling the Intricacies of Leishmania donovani: Structure, Life Cycle, Path...Unveiling the Intricacies of Leishmania donovani: Structure, Life Cycle, Path...
Unveiling the Intricacies of Leishmania donovani: Structure, Life Cycle, Path...Dr. Asif Anas
 
How to Create a Toggle Button in Odoo 17
How to Create a Toggle Button in Odoo 17How to Create a Toggle Button in Odoo 17
How to Create a Toggle Button in Odoo 17Celine George
 
KARNAADA.pptx made by - saransh dwivedi ( SD ) - SHALAKYA TANTRA - ENT - 4...
KARNAADA.pptx  made by -  saransh dwivedi ( SD ) -  SHALAKYA TANTRA - ENT - 4...KARNAADA.pptx  made by -  saransh dwivedi ( SD ) -  SHALAKYA TANTRA - ENT - 4...
KARNAADA.pptx made by - saransh dwivedi ( SD ) - SHALAKYA TANTRA - ENT - 4...M56BOOKSTORE PRODUCT/SERVICE
 
Optical Fibre and It's Applications.pptx
Optical Fibre and It's Applications.pptxOptical Fibre and It's Applications.pptx
Optical Fibre and It's Applications.pptxPurva Nikam
 
Over the counter (OTC)- Sale, rational use.pptx
Over the counter (OTC)- Sale, rational use.pptxOver the counter (OTC)- Sale, rational use.pptx
Over the counter (OTC)- Sale, rational use.pptxraviapr7
 
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptxSandy Millin
 
What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?TechSoup
 
ARTICULAR DISC OF TEMPOROMANDIBULAR JOINT
ARTICULAR DISC OF TEMPOROMANDIBULAR JOINTARTICULAR DISC OF TEMPOROMANDIBULAR JOINT
ARTICULAR DISC OF TEMPOROMANDIBULAR JOINTDR. SNEHA NAIR
 
10 Topics For MBA Project Report [HR].pdf
10 Topics For MBA Project Report [HR].pdf10 Topics For MBA Project Report [HR].pdf
10 Topics For MBA Project Report [HR].pdfJayanti Pande
 
How to Solve Singleton Error in the Odoo 17
How to Solve Singleton Error in the  Odoo 17How to Solve Singleton Error in the  Odoo 17
How to Solve Singleton Error in the Odoo 17Celine George
 
A gentle introduction to Artificial Intelligence
A gentle introduction to Artificial IntelligenceA gentle introduction to Artificial Intelligence
A gentle introduction to Artificial IntelligenceApostolos Syropoulos
 
How to Send Emails From Odoo 17 Using Code
How to Send Emails From Odoo 17 Using CodeHow to Send Emails From Odoo 17 Using Code
How to Send Emails From Odoo 17 Using CodeCeline George
 
How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17Celine George
 
The basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptxThe basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptxheathfieldcps1
 
Drug Information Services- DIC and Sources.
Drug Information Services- DIC and Sources.Drug Information Services- DIC and Sources.
Drug Information Services- DIC and Sources.raviapr7
 
CapTechU Doctoral Presentation -March 2024 slides.pptx
CapTechU Doctoral Presentation -March 2024 slides.pptxCapTechU Doctoral Presentation -March 2024 slides.pptx
CapTechU Doctoral Presentation -March 2024 slides.pptxCapitolTechU
 
Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...raviapr7
 
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptxClinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptxraviapr7
 
Prescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptxPrescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptxraviapr7
 

Kürzlich hochgeladen (20)

Unveiling the Intricacies of Leishmania donovani: Structure, Life Cycle, Path...
Unveiling the Intricacies of Leishmania donovani: Structure, Life Cycle, Path...Unveiling the Intricacies of Leishmania donovani: Structure, Life Cycle, Path...
Unveiling the Intricacies of Leishmania donovani: Structure, Life Cycle, Path...
 
How to Create a Toggle Button in Odoo 17
How to Create a Toggle Button in Odoo 17How to Create a Toggle Button in Odoo 17
How to Create a Toggle Button in Odoo 17
 
KARNAADA.pptx made by - saransh dwivedi ( SD ) - SHALAKYA TANTRA - ENT - 4...
KARNAADA.pptx  made by -  saransh dwivedi ( SD ) -  SHALAKYA TANTRA - ENT - 4...KARNAADA.pptx  made by -  saransh dwivedi ( SD ) -  SHALAKYA TANTRA - ENT - 4...
KARNAADA.pptx made by - saransh dwivedi ( SD ) - SHALAKYA TANTRA - ENT - 4...
 
Optical Fibre and It's Applications.pptx
Optical Fibre and It's Applications.pptxOptical Fibre and It's Applications.pptx
Optical Fibre and It's Applications.pptx
 
Over the counter (OTC)- Sale, rational use.pptx
Over the counter (OTC)- Sale, rational use.pptxOver the counter (OTC)- Sale, rational use.pptx
Over the counter (OTC)- Sale, rational use.pptx
 
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
2024.03.23 What do successful readers do - Sandy Millin for PARK.pptx
 
What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?
 
ARTICULAR DISC OF TEMPOROMANDIBULAR JOINT
ARTICULAR DISC OF TEMPOROMANDIBULAR JOINTARTICULAR DISC OF TEMPOROMANDIBULAR JOINT
ARTICULAR DISC OF TEMPOROMANDIBULAR JOINT
 
10 Topics For MBA Project Report [HR].pdf
10 Topics For MBA Project Report [HR].pdf10 Topics For MBA Project Report [HR].pdf
10 Topics For MBA Project Report [HR].pdf
 
How to Solve Singleton Error in the Odoo 17
How to Solve Singleton Error in the  Odoo 17How to Solve Singleton Error in the  Odoo 17
How to Solve Singleton Error in the Odoo 17
 
A gentle introduction to Artificial Intelligence
A gentle introduction to Artificial IntelligenceA gentle introduction to Artificial Intelligence
A gentle introduction to Artificial Intelligence
 
March 2024 Directors Meeting, Division of Student Affairs and Academic Support
March 2024 Directors Meeting, Division of Student Affairs and Academic SupportMarch 2024 Directors Meeting, Division of Student Affairs and Academic Support
March 2024 Directors Meeting, Division of Student Affairs and Academic Support
 
How to Send Emails From Odoo 17 Using Code
How to Send Emails From Odoo 17 Using CodeHow to Send Emails From Odoo 17 Using Code
How to Send Emails From Odoo 17 Using Code
 
How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17
 
The basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptxThe basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptx
 
Drug Information Services- DIC and Sources.
Drug Information Services- DIC and Sources.Drug Information Services- DIC and Sources.
Drug Information Services- DIC and Sources.
 
CapTechU Doctoral Presentation -March 2024 slides.pptx
CapTechU Doctoral Presentation -March 2024 slides.pptxCapTechU Doctoral Presentation -March 2024 slides.pptx
CapTechU Doctoral Presentation -March 2024 slides.pptx
 
Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...Patient Counselling. Definition of patient counseling; steps involved in pati...
Patient Counselling. Definition of patient counseling; steps involved in pati...
 
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptxClinical Pharmacy  Introduction to Clinical Pharmacy, Concept of clinical pptx
Clinical Pharmacy Introduction to Clinical Pharmacy, Concept of clinical pptx
 
Prescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptxPrescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptx
 

Computer programming all chapters

  • 1. LOGO Higher Technological Institute 10th of Ramadan City 6th of October Branch Electrical and Computer Engineering Department Lecture Notes in Prepaid By: Eng. Ibrahim Elewah Main Reference HTI Student Book and “C For Dummies” by Dan Gookin 2nd Edition 1
  • 2. Course Contents 1 Introduction 2 Program Development 3 The Essential of C Programs 4 Manipulating Data with Operators 5 Reading from and Writing to Standard I/O 6 Decision 7 Iteration 8 Arrays 9 C Functions C Programming 2 Higher Technological Institute
  • 3. Course Contents Introduction 1  Computer Program  Programming Language  Machine Language  Assembly Language  High Level Language  The C Programming Language  Interpreter, Compiler and Assembler C Programming 3 Higher Technological Institute
  • 4. Computer Program A Computer Program is a sequence of instructions written to perform a specified task with a computer. The program has an executable form that the computer can use directly to execute the instructions. The Executable program in its human-readable form is called the source code. Computer source code is often written by computer programmers. It is written in a programming language. Source Code may be converted into an executable file ( an executable program or a binary) by a compiler and later executed by a central processing unit. Alternatively, computer programs may be executed with the aid of an interpreter, or may be embedded directly into hardware. C Programming 4 Higher Technological Institute
  • 5. Programming Language A Programming language is an artificial language designed to communicate instructions to a computer. Most programming language are purely textual; they use sequences of text including words, numbers and punctuation. The programming languages are divided into three groups: 1. Machine Language 2. Assembly Language 3. High Level Language C Programming 5 Higher Technological Institute
  • 6. Machine Language Essentially, computers really understand only one language, which consists of zeroes and ones, also known as machine language. But o Difficult and take a Long Time to Read and Write o Difficult to Detect and Correct the Errors o It is also called C Programming Binary Code
  • 7. Assembly Language o It uses some Mnemonic English codes instead of Binary Codes. o Assembly language programs are easier to Read and Write than machine language programs. o It Needs an Assembler to translate Mnemonic codes into Machine Language. Example ADD 5,9 SUB 7,3 C Programming 7 Higher Technological Institute
  • 8. Assembly Language o It uses some Mnemonic English codes instead of Binary Codes. o Assembly language programs are easier to Read and Write than machine language programs. o It Needs an Assembler to translate Mnemonic codes into Machine Language. Example ADD 5,9 SUB 7,3 C Programming But in Large Programs Writing a program in assembly language can prove extremely Time-consuming, and complicated. 8 Higher Technological Institute
  • 9. High Level Language Because writing machine- or assembly-language programs was so Difficult and Confusing, people developed additional languages that look more like human languages With names such as JAVA, BASIC, and C. o High Level Language needs Compiler or Interpreter. C Programming 9 Higher Technological Institute
  • 10. High Level Language Readability Programs are easy to Read Maintainability Programs are easy to Maintain Portability Programs are easy to port across Different computer Platforms Reusability Saving your programs into library and invoke them in next programs C Programming 10 Higher Technological Institute
  • 11. C Programming Language 1. High Level Language. 2. C allows you to get control of computer Hardware. 3. Many other high level language developed based on C. 4. The American National Standard Institute ANSI define the standard for the C programming language. 5. Your HTI Book focuses on the functions defined in ANSI See Page C Programming 11 3 Higher Technological Institute
  • 12. Interpreter, Compiler and Assembler Lower Level Language High Level Language Interpreter Assembly Language Binary Language High Level Language Compiler Computer Instruction Assembler Lower Level Language Binary 0’s and 1’s Read more Pages 3 and 4 C Programming 12 Higher Technological Institute
  • 13. Your First C Program /* This is my First C Program */ #include<stdio.h> int main () { printf(" Hello World! n ") ; return 0 ; } C Programming 13 Higher Technological Institute
  • 14. Comments /* This is my First C Program */ o The compiler ignore everything between two slashes and asterisks. o Just to help you document your program. o C compilers allows to write comments like /* This comment does not increase the size of the executable file (Binary Code), nor does it affect the performance speed */ C Programming 14 Higher Technological Institute
  • 15. Your First C Program /* This is my First C Program */ #include<stdio.h> int main () { printf(" Hello World! n ") ; return 0 ; } C Programming 15 Higher Technological Institute
  • 16. Header Files stdio.h Header File #include<stdio.h> o This Header Files required by include directive o Header Files extensions .h means header o The stdio.h header file numerous prototypes and macros to perform input or output I/O for C Program C Programming 16 Higher Technological Institute
  • 17. Your First C Program /* This is my First C Program */ #include<stdio.h> int main () { printf(" Hello World! n ") ; return 0 ; } C Programming 17 Higher Technological Institute
  • 18. Main Function o Very special function o Each C Program MUST have a main() function o The main() function syntax as follows int main () { return 0 ; } C Programming 18 Higher Technological Institute
  • 19. Your First C Program /* This is my First C Program */ #include<stdio.h> int main () { printf(" Hello World! n ") ; return 0 ; } C Programming 19 Higher Technological Institute
  • 20. Printing On Screen o To print a text or numbers printf is used. o Syntax printf(“ ”); o The new line character n o To print Hello World! write the following statement printf(" Hello World! n ") ; C Programming 20 Higher Technological Institute
  • 21. Your First C Program /* This is my First C Program */ #include<stdio.h> int main () { printf(" Hello World! n ") ; return 0 ; } C Programming 21 Higher Technological Institute
  • 23. References Logic Design A 23 Higher Technological Institute
  • 24. Course Contents 1 Introduction 2 Program Development 3 The Essential of C Programs 4 Manipulating Data with Operators 5 Reading from and Writing to Standard I/O 6 Decision 7 Iteration 8 Arrays 9 C Functions C Programming 24 Higher Technological Institute
  • 25. Course Contents Program Development 2  Phases of Computer Program Development  Problem Definition and Analysis  Algorithms and Flowcharts  Program Coding  Program Execute and Testing  Exercise C Programming 25 Higher Technological Institute
  • 26. Problem Definition and Analysis o The problem which you write a program for should be completely understood by gathering information about it. o This information is the program input. o How information is processed to give the required output. oExample 𝟐 𝒂𝒙 + 𝒃𝒙 + 𝒄 = 𝟎 C Programming 26 Higher Technological Institute
  • 27. Problem Definition and Analysis o The General Quadratic Equation 𝟐 𝒂𝒙 + 𝒃𝒙 + 𝒄 = 𝟎 C Programming 27 Higher Technological Institute
  • 28. Problem Definition and Analysis o The General Quadratic Equation 𝒙= −𝒃 ± 𝒃 𝟐 − 𝟒𝒂𝒄 𝟐𝒂 𝟐 𝒂𝒙 + 𝒃𝒙 + 𝒄 = 𝟎 C Programming 28 Higher Technological Institute
  • 29. Problem Definition and Analysis o The General Quadratic Equation 𝒙= 𝒙𝟏 = −𝒃 + 𝒃 𝟐 − 𝟒𝒂𝒄 𝟐𝒂 −𝒃 ± 𝒃 𝟐 − 𝟒𝒂𝒄 𝟐𝒂 𝒂𝒏𝒅 𝒙𝟐 = −𝒃 − 𝒃 𝟐 − 𝟒𝒂𝒄 𝟐𝒂 𝟐 𝒂𝒙 + 𝒃𝒙 + 𝒄 = 𝟎 C Programming 29 Higher Technological Institute
  • 30. Problem Definition and Analysis o The General Quadratic Equation 𝒙= 𝒙𝟏 = −𝒃 + 𝒃 𝟐 − 𝟒𝒂𝒄 𝟐𝒂 −𝒃 ± 𝒃 𝟐 − 𝟒𝒂𝒄 𝟐𝒂 Discriminator 𝒂𝒏𝒅 𝒙𝟐 = −𝒃 − 𝒃 𝟐 − 𝟒𝒂𝒄 𝟐𝒂 𝟐 𝒂𝒙 + 𝒃𝒙 + 𝒄 = 𝟎 C Programming 30 Higher Technological Institute
  • 31. Problem Definition and Analysis o The General Quadratic Equation 𝒙= 𝒃 𝟐 − 𝟒𝒂𝒄 𝟐𝒂 −𝒃 ± 𝒃 𝟐 − 𝟒𝒂𝒄 𝒙𝟏 = 𝟐𝒂 𝑫 = 𝒃 𝟐 − 𝟒𝒂𝒄 𝒊𝒇 𝑫 < 𝟎 𝒙 𝟏 𝒂𝒏𝒅 𝒙 𝟐 𝒊𝒇 𝑫 > 𝟎 𝒙 𝟏 𝒂𝒏𝒅 𝒙 𝟐 −𝒃 + Discriminator 𝒂𝒏𝒅 𝒙𝟐 = −𝒃 − 𝒃 𝟐 − 𝟒𝒂𝒄 𝟐𝒂 𝒂𝒓𝒆 𝑰𝒎𝒂𝒈𝒊𝒏𝒂𝒓𝒚 𝒄𝒂𝒏 𝒃𝒆 𝑪𝒂𝒍𝒄𝒖𝒍𝒂𝒕𝒆𝒅 𝟐 𝒂𝒙 + 𝒃𝒙 + 𝒄 = 𝟎 C Programming 31 Higher Technological Institute
  • 32. Problem Definition and Analysis o How information is processed to give the required output ? 𝒂 , 𝒃 𝒂𝒏𝒅 𝒄 𝒂𝒓𝒆 𝑮𝒊𝒗𝒆𝒏 = 𝑰𝒏𝒑𝒖𝒕𝒔 𝟐 𝑫 = 𝒃 − 𝟒𝒂𝒄 Question D >0 ? YES 𝒙 𝟏 = NO −𝒃+ 𝒙 𝟏 𝒂𝒏𝒅 𝒃 𝟐 −𝟒𝒂𝒄 𝟐𝒂 𝒙𝟐 𝒂𝒏𝒅 𝒂𝒓𝒆 𝒙𝟐 = −𝒃− 𝒃 𝟐 −𝟒𝒂𝒄 𝟐𝒂 𝑰𝒎𝒂𝒈𝒊𝒏𝒂𝒓𝒚 𝟐 𝒂𝒙 + 𝒃𝒙 + 𝒄 = 𝟎 C Programming 32 Higher Technological Institute
  • 33. Algorithms and Flowcharts C Programming 33 Higher Technological Institute
  • 34. Flowchart Start, Stop ( Begin , End) Input , Output Processing Program Instruction Decision Connector Comment Preparation C Programming 34 ( Instruction that change the program) Higher Technological Institute
  • 35. Flowcharts 𝑫 = 𝒃 𝟐 − 𝟒𝒂𝒄 𝒙 𝟏,𝟐 = C Programming −𝒃 ± 𝒃 𝟐 − 𝟒𝒂𝒄 𝟐𝒂 35 Higher Technological Institute
  • 36. Flowcharts 𝑫 = 𝒃 𝟐 − 𝟒𝒂𝒄 𝒙 𝟏,𝟐 = C Programming −𝒃 ± 𝒃 𝟐 − 𝟒𝒂𝒄 𝟐𝒂 36 Higher Technological Institute
  • 37. Flowcharts 𝑫 = 𝒃 𝟐 − 𝟒𝒂𝒄 𝒙 𝟏,𝟐 = C Programming −𝒃 ± 𝒃 𝟐 − 𝟒𝒂𝒄 𝟐𝒂 37 Higher Technological Institute
  • 38. Flowcharts 𝑫 = 𝒃 𝟐 − 𝟒𝒂𝒄 YES 𝒙 𝟏,𝟐 = C Programming −𝒃 ± NO 𝒃 𝟐 − 𝟒𝒂𝒄 𝟐𝒂 38 Higher Technological Institute
  • 39. Flowcharts 𝑫 = 𝒃 𝟐 − 𝟒𝒂𝒄 YES 𝒙 𝟏,𝟐 = C Programming −𝒃 ± NO 𝒃 𝟐 − 𝟒𝒂𝒄 𝟐𝒂 39 Higher Technological Institute
  • 40. Flowcharts 𝑫 = 𝒃 𝟐 − 𝟒𝒂𝒄 YES 𝒙 𝟏,𝟐 = C Programming −𝒃 ± NO 𝒃 𝟐 − 𝟒𝒂𝒄 𝟐𝒂 40 Higher Technological Institute
  • 41. Flowcharts Examples Example 1 Draw a flow chart to read two numbers and compare between both if they are equal or not. C Programming 41 Higher Technological Institute
  • 42. Flowcharts Examples Example 1 Draw a flow chart to read two numbers and compare between both if they are equal or not. C Programming 42 Higher Technological Institute
  • 43. Flowcharts Examples Example 2 Draw a flow chart to generate a table of squares and cubes of integers from 1 to 9. C Programming 43 Higher Technological Institute
  • 44. Flowcharts Examples Example 2 Draw a flow chart to generate a table of squares and cubes of integers from 1 to 9. C Programming 44 Higher Technological Institute
  • 45. Flowcharts Examples Example 3 Draw a flow chart to determine the minimum value of three input numbers. C Programming 45 Higher Technological Institute
  • 46. Flowcharts Examples Example 3 Draw a flow chart to determine the minimum value of three input numbers. C Programming 46 Higher Technological Institute
  • 47. Flowcharts Examples Example 4 Draw a flow chart to generates the famous sequence of numbers called the Fibonacci series. Here are the first few terms of the series: 0 1 1 2 3 5 8 13 21 34 55 C Programming 47 Higher Technological Institute
  • 48. Flowcharts Examples Example 4 Draw a flow chart to generates the famous sequence of numbers called the Fibonacci series. Here are the first few terms of the series: 0 1 1 2 3 5 8 13 21 34 55 Each term is found by adding the two previous ones: 1+1 is 2, 1+2 is 3, 2+3 is 5, 3+5 is 8, and so on. C Programming 48 Higher Technological Institute
  • 49. Flowcharts Examples Example 4 Draw a flow chart to generates the famous sequence of numbers called the Fibonacci series. Here are the first few terms of the series: 0 1 1 2 3 5 8 13 21 34 55 Each term is found by adding the two previous ones: 1+1 is 2, 1+2 is 3, 2+3 is 5, 3+5 is 8, and so on. 𝒇𝟎 = 𝟎 , 𝒇𝟏 = 𝟏 𝒂𝒏𝒅 𝒇 𝒏 = 𝒇 𝒏−𝟏 + 𝒇 𝒏−𝟐 Terminate the program after calculation the first 10 numbers of the Fibonacci series and print them. C Programming 49 Higher Technological Institute
  • 50. Flowcharts Examples Example 4 0 1 𝒇𝟎 = 𝟎 1 𝒇𝟏 = 𝟏 2 𝒇 𝒏 = 𝒇 𝒏−𝟏 + 𝒇 𝒏−𝟐 3 5 8 13 21 34 55 C Programming 50 Higher Technological Institute
  • 51. Flowcharts Examples Example 5 It is desired to compute income Tax for a given income as per the following table: Income Amount of tax Up to Rs 35000 Nil From Rs 35001 To Rs 60000 20% of income in Excess of Rs 35000 From Rs 60001 To Rs 120000 Rs 5000 + 30% of income in Excess of Rs 60000 More than Rs 120000 Rs 23000+ 40% of income in Excess of Rs 120000 C Programming 51 Higher Technological Institute
  • 53. Flowcharts Examples Example 6 It is desired to sum 20 values read from a data statement. This can be done by means of a loop. It uses the following algorithm for summation. Sj+1 = Sj + A Where: j = 1,2,3,..20 Sj+1 : The new sum Sj : The old sum A : The variable whose values are to be added C Programming 53 Higher Technological Institute
  • 55. Flowcharts Examples Example 7 Draw a flowchart of Program that Calculate and Display The Factorial of C Programming n 55 Higher Technological Institute
  • 56. Flowcharts Examples Example 7 Draw a flowchart of Program that Calculate and Display The Factorial of n 𝒏! = 𝒏 × 𝒏 − 𝟏 × 𝒏 − 𝟐 … × 𝟑 × 𝟐 × 𝟏 C Programming 56 Higher Technological Institute
  • 57. Flowcharts Examples Example 7 Draw a flowchart of Program that Calculate and Display The Factorial of n 𝒏! = 𝒏 × 𝒏 − 𝟏 × 𝒏 − 𝟐 … × 𝟑 × 𝟐 × 𝟏 C Programming 57 Higher Technological Institute
  • 58. Flowcharts Examples Example 8 Draw a flowchart to compute all possible products of X and Y X and Y varying from 0 through 9 (100 products) 0x0=0 0x1=0 0x2=0 0x3=0 . . 0x9=0 C Programming 1x0=0 … 1x1=1 … 1x2=2 … 1x3=3 … . . 1x9=9 … 9x0=0 9x1=9 9x2=18 9x3=27 Home Work ..! 9x9=81 58 Higher Technological Institute
  • 59. Flowcharts Examples Example 8 0x0=0 0x1=0 0x2=0 0x3=0 . . 0x9=0 C Programming 1x0=0 … 1x1=1 … 1x2=2 … 1x3=3 … . . 1x9=9 … 9x0=0 9x1=9 9x2=18 9x3=27 9x9=81 59 Higher Technological Institute
  • 61. Program Coding o In this phase, the algorithm ( or Flowchart ) is transferred into program using codes of one of programming language. o Machine Language ( Binary Code ). o Assembly Language. o High Level Language. C Programming 61 Higher Technological Institute
  • 62. Program Executing and Testing Editing Compiling Linking Executing C Programming 62 Higher Technological Institute
  • 63. Program Executing and Testing Editing C Programming Means to use any editing program to convert the program into electronic form and save it with a suitable and valid name with the extension (.c) The program becomes an electronic source code 63 Higher Technological Institute
  • 64. Program Executing and Testing Compiling C Programming Means to use a compiler program matches with the programming language the source program written with, to detect the linguistic errors and convert the source code program into a binary codes. The program becomes an object program, saved with the source code name but with the extension (.0). 64 Higher Technological Institute
  • 65. Program Executing and Testing Linking C Programming The program when it is written contains some functions, as printf( ) function which is saved in the library of the compiler and may be some external functions written to do certain jobs by the program after linking with libraries functions is converted to an executable program, with the same name entered by the user but with the extension .exe (dot exe). 65 Higher Technological Institute
  • 66. Program Executing and Testing The program in this step i.e., after linking is ready to be run or debugged on the computer. Executing C Programming 66 Higher Technological Institute
  • 67. Program Executing and Testing Editing Compiling Linking Executing C Programming 67 Higher Technological Institute
  • 68. Steps of Obtaining an Executable Program C Programming 68 Higher Technological Institute
  • 69. Course Contents 1 Introduction 2 Program Development 3 The Essential of C Programs 4 Manipulating Data with Operators 5 Reading from and Writing to Standard I/O 6 Decision 7 Iteration 8 Arrays 9 C Functions C Programming 69 Higher Technological Institute
  • 70. Course Contents 3 The Essential of C Programs  Constants and variables  Expressions  Arithmetic operators  Statements  Statement blocks  Data Types and Names in C  Naming a Variable C Programming 70 Higher Technological Institute
  • 71. Expressions o An expression is a combination of constants, variables, and operators that are used to denote computations 𝑨= 𝟐∗ 𝑨 − 𝟏 1. Taking the value contained in the drawer (variable) A 2. Multiply this value by 2 3. Subtract 1 from result obtained from 2 4. The value contained in drawer A is omitted, then putting the result obtained from 3 into drawer A. C Programming 71 Higher Technological Institute
  • 72. Arithmetic Operations Addition Subtraction Multiplication Division Reminder The Reminder operator % is used to obtain the reminder of the first operand divided by the second operand C Programming 72 Higher Technological Institute
  • 73. Arithmetic Operations Addition Subtraction Multiplication Division Reminder Example 𝟔% 𝟒= 𝟐 C Programming 𝟏𝟎𝟎𝟏%𝟐 = 𝟏 73 𝟒𝟖%𝟓 = Higher Technological Institute
  • 74. Arithmetic Operations Addition Subtraction Multiplication Division Reminder Example 𝟔% 𝟒= 𝟐 C Programming 𝟏𝟎𝟎𝟏%𝟐 = 𝟏 74 𝟒𝟖%𝟓 = 𝟑 Higher Technological Institute
  • 75. Constants and Variables o As its name implies, a constant is a value that never changes. o A variable, on the other hand, can be used to present different values. o For instance, consider the following: 𝒊 = 𝟏 ; o Where the symbol i is a constant because it always has the same value (1) and the symbol i is assigned the constant 1. o In other words, i contains the value of 1 after the statement is executed. C Programming 75 Higher Technological Institute
  • 76. Data Types and Names o The C language reserves some keywords words that have special meanings to the language. o Those reserved words should not be used as variables, constants, or function names in your program. o All C keywords must be written in lowercase letters, for instance INT will not be treated as a keyword, it must be written as int. C Programming 76 Higher Technological Institute
  • 77. The computer list of C keywords auto const double float int short struct unsigned C Programming break continue else for long signed switch void case default enum goto register sizeof typedef volatile 77 char do extern if return static union while Higher Technological Institute
  • 78. Naming a Variable Valid Variable Name Can Use o Characters A through Z and a through z o Digit characters 0 through 9, which can be used in any position except the first of a variable name. o The underscore character _ Examples stop_sign C Programming loop3 78 and_pause Higher Technological Institute
  • 79. Naming a Variable Invalid Variable Name Can NOT be Used o o o o A variable name can’t contain any C arithmetic signs. A variable name can’t contain any dots. A variable name can’t contain any apostrophes. A variable name can’t contain any other special symbols such as *, @, #, and so on. Examples 4flags return C Programming sum-result what_size? 79 method*4 ahmed.ali Higher Technological Institute
  • 80. Data Types C Data Type char int float double a, B, $, # 5, 17, 128 2.5 , 0.3 23433.3455 C Programming 80 Higher Technological Institute
  • 81. Declaration Statement Type Name Value char int float double c1 N1 F1 d1 char c1 = ‘&’ ; int ‘&’ 100 32/10 5e3 = 100 ; n1 C Programming 81 Higher Technological Institute
  • 82. Declaration Statement Type char int float double Name Value ‘&’ 100 32/10 5e3 c1 N1 F1 d1 char c1 ; c1 = ‘&’ ; int ; n1 = 100 ; n1 C Programming 82 Higher Technological Institute
  • 83. Declaration Statement Type Name Value char int float double float c1 n1 f1 d1 ‘&’ 100 32/10 5e3 f1= 32/100 ; double C Programming d1=5e3 ; 83 Higher Technological Institute
  • 85. Some special characters in C b Backspace f Form feed n New line r Return t Tab C Programming Moves the cursor to the left one character Goes to the top of a new page Carriage return and line feeds Returns to the beginning of the current line Advances to the next tab stop 85 Higher Technological Institute
  • 86. Examples /* Example1 : Printing out characters */ # include <stdio.h> /* the header file for the printf () function */ main ( ) { /* the main function body till line 13 */ char c1; /* declaration of the character variable c1 */ char c2; /* declaration of the character variable c2 */ c1 = ‘A’; /* assigning c1 with the character constant A */ c2 = ‘a’; /* assigning c2 with the character constant a */ return 0; } C Programming 86 Higher Technological Institute
  • 87. Examples /* Example1 : Printing out characters */ # include <stdio.h> /* the header file for the printf () function */ main ( ) { /* the main function body till line 13 */ char c1; /* declaration of the character variable c1 */ char c2; /* declaration of the character variable c2 */ c1 = ‘A’; /* assigning c1 with the character constant A */ c2 = ‘a’; /* assigning c2 with the character constant a */ printf ( “ The character c1 is : %c n ”, c1); printf ( “ The character c1 is : %c ” , c1); printf ( “ while the character c2 is : %c n”, c2); return 0; } C Programming 87 Higher Technological Institute
  • 88. Examples /* Example1 : Printing out characters */ # include <stdio.h> /* the header file for the printf () function */ main ( ) { /* the main function body till line 13 */ char c1; /* declaration of the character variable c1 */ printf ( “ The character c1 is : variablen */”, c1); %c c2 char c2; /* declaration of the character c1 = ‘A’; /* assigning c1 with the character constant A */ c2 = ‘a’; /* assigning c2 with the character constant a */ printf ( “ The character c1 is : %c n ”, c1); printf ( “ The character c1 is : %c ” , c1); printf ( “ while the character c2 is : %c n”, c2); return 0; } C Programming 88 Higher Technological Institute
  • 89. Examples /* Example1 : Printing out characters */ # include <stdio.h> /* the header file for the printf () function */ main ( ) { /* the main function body till line 13 */ char c1; /* declaration of the character variable c1 */ printf ( “ The character c1 is : variablen */”, c1); %c c2 char c2; /* declaration of the character c1 = ‘A’; /* assigning c1 with the character constant A */ c2 = ‘a’; /* assigning c2 with the character constant a */ printf ( “ The character c1 is : %c n ”, c1); printf ( “ The character c1 is : %c ” , c1); printf ( “ while the character c2 is : %c n”, c2); return 0; } C Programming 89 Higher Technological Institute
  • 90. Examples /* Example1 : Printing out characters */ # include <stdio.h> /* the header file for the printf () function */ main ( ) { /* the main function body till line 13 */ char c1; /* declaration of the character variable c1 */ printf ( “ The character c1 is : variablen */”, c1); %c c2 char c2; /* declaration of the character c1 = ‘A’; /* assigning c1 with the character constant A */ c2 = ‘a’; /* assigning c2 with the character constant a */ printf ( “ The character c1 is : %c n ”, c1); printf ( “ The character c1 is : %c ” , c1); printf ( “ while the character c2 is : %c n”, c2); return 0; } C Programming 90 Higher Technological Institute
  • 91. Examples /* Example1 : Printing out characters */ # include <stdio.h> /* the header file for the printf () function */ main ( ) { /* the main function body till line 13 */ char c1; /* declaration of the character variable c1 */ printf ( “ The character c1 is : variablen */”, c1); %c c2 char c2; /* declaration of the character c1 = ‘A’; /* assigning c1 with the character constant A */ c2 = ‘a’; /* assigning c2 with the character constant a */ printf ( “ The character c1 is : %c n ”, c1); printf ( “ The character c1 is : %c ” , c1); printf ( “ while the character c2 is : %c n”, c2); return 0; } New Line Specifier for Character Data Type C Programming 91 Higher Technological Institute
  • 92. Examples /* Example1 : Printing out characters */ # include <stdio.h> /* the header file for the printf () function */ main ( ) { /* the main function body till line 13 */ char c1; /* declaration of the character variable c1 */ char c2; /* declaration of the character variable c2 */ c1 = ‘A’; /* assigning c1 with the character constant A */ c2 = ‘a’; /* assigning c2 with the character constant a */ printf ( “ The character c1 is : %c n ”, c1); printf ( “ The character c1 is : %c ” , c1); printf ( “ while the character c2 is : %c n”, c2); return 0; } C Programming 92 Higher Technological Institute
  • 93. You Have To Know about Data Types Type Name Value Specifier char c1 ‘&’ %c int n1 100 %d float f1 32/10 %f double d1 5e3 %e,%E C Programming 93 Higher Technological Institute
  • 94. Examples /* The arithmetic operations on integers */ # include<stdio.h> /* the header file for the printf () function */ main ( ) { /* the main function body till line 15 */ int m = 3; int n=2; printf ( "The summation of %d and %d is : %d.n", m, n, m+n); printf ( "The difference between %d and %d is : %d.n", m, n, m-n); printf ( "The multiplication of %d by %d is : %d.n", m, n, m*n); printf ( "The division of %d by %d is : %d.n", m, n, m/n); printf ( "The remainder of division of %d by %d is : %d.n", m, n, m%n); return 0 ; } C Programming 94 Higher Technological Institute
  • 95. Operators Precedence Operator Sign ( ) / * + - C Programming Name Brackets Division Multiplication Addition Subtraction 95 Higher Technological Institute
  • 96. Logic Operators Operator Sign || && != C Programming Name OR AND NOT 96 Higher Technological Institute
  • 97. Examples /* Example3 : Integer vs. floating point divisions */ # include <stdio.h> /* the header file for the printf () function */ main ( ) { int n1,n2,n3; float m1,m2,m3; n1 = 32/10; m1= 32/10; n2 = 32.0/10; m2= 32.0/10; n3 = 32/10.0; m3 = 32/10.0; return 0; } C Programming 97 Higher Technological Institute
  • 98. Examples /* Example3 : Integer vs. floating point divisions */ # include <stdio.h> /* the header file for the printf () function */ main ( ) { int n1,n2,n3; float m1,m2,m3; n1 = 32/10; m1= 32/10; n2 = 32.0/10; m2= 32.0/10; n3 = 32/10.0; m3 = 32/10.0; printf ( “ The integer division of 32/10 is : %d n”, n1); printf ( “The floating point division of 32/10 is : %f n”, m1); printf ( “The integer division of 32.0/10 is : %d n”, n2); printf ( “The floating point division of 32.0/10 is : %f n”, m2); printf ( “The integer division of 32/10.0 is : %d n”, n3); printf ( “The floating point division of 32/10.0 is : %f n”, m3); return 0; } C Programming 98 Higher Technological Institute
  • 99. Double Data Type o Here are two examples: [mantissa] e [exponent] [mantissa] E [exponent] Example 5000 -300 0.0025 5e3. -3e2 2.5e-3. Specifier %e or %E With printf ( ) C Programming 99 Higher Technological Institute
  • 100. Precedence Example # include<stdio.h> main ( ) { int A,B,C,D,E, X, Y, Z; A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ; X= E*D – B/C + A ; Y=(E*D)–(B/C)+ A ; Z= A*(D–B)/(C+ E); printf ( “ X= %d n Y= %d n Z=%d n”, X,Y,Z); return 0 ; } C Programming 100 Higher Technological Institute
  • 101. Precedence Example # include<stdio.h> main ( ) { int A,B,C,D,E, X, Y, Z; A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ; X= E*D – B/C + A ; Y=(E*D)–(B/C)+ A ; Z= A*(D–B)/(C+ E); printf ( “ X= %d n Y= %d n Z=%d n”, X,Y,Z); return 0 ; } C Programming 101 Higher Technological Institute
  • 102. Precedence Example # include<stdio.h> X( B/C + A ; main = ) E * D – { int A,B,C,D,E, X, Y, Z; A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ; X= E*D – B/C + A ; Y=(E*D)–(B/C)+ A ; Z= A*(D–B)/(C+ E); printf ( “ X= %d n Y= %d n Z=%d n”, X,Y,Z); return 0 ; } C Programming 102 Higher Technological Institute
  • 103. Precedence Example X= E*D – X= E*D – C Programming B/C B/C + A ; + A ; 103 Higher Technological Institute
  • 104. Precedence Example X= E*D – C Programming B/C + A ; 104 Higher Technological Institute
  • 105. Precedence Example X= E*D – B/C + A ; A = 20 B=6 C=3 D = 10 E=2 C Programming ( ) * / +- 105 Higher Technological Institute
  • 106. Precedence Example X= E*D – 2 * 10 – A = 20 B/C + A ; 6 / 3 + 20 B=6 C=3 D = 10 E=2 C Programming 106 ( ) * / +- Higher Technological Institute
  • 107. Precedence Example X= E*D – 2 * 10 – A = 20 B=6 C=3 D = 10 E=2 C Programming 20 B/C + A ; 6 / 3 + 20 – 2 107 + 20 ( ) * / +- Higher Technological Institute
  • 108. Precedence Example X= E*D – 2 * 10 – A = 20 B=6 C=3 D = 10 E=2 C Programming 20 B/C + A ; 6 / 3 + 20 – 2 38 108 + 20 ( ) * / +- Higher Technological Institute
  • 109. Precedence Example # include<stdio.h> main ( ) { int A,B,C,D,E, X, Y, Z; A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ; X= E*D – B/C + A ; Y=(E*D)–(B/C)+ A ; Z= A*(D–B)/(C+ E); printf ( “ X= %d n Y= %d n Z=%d n”, X,Y,Z); return 0 ; } C Programming 109 Higher Technological Institute
  • 110. Precedence Example # include<stdio.h> main ( ) { int A,B,C,D,E, X, Y, Z; A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ; X= E*D – B/C + A ; Y=(E*D)–(B/C)+ A ; Z= A*(D–B)/(C+ E); printf ( “ X= %d n Y= %d n Z=%d n”, X,Y,Z); return 0 ; } C Programming 110 Higher Technological Institute
  • 111. Precedence Example # include<stdio.h> main = )( E * D ) – ( B / C ) + A ; Y ( { int A,B,C,D,E, X, Y, Z; A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ; X= E*D – B/C + A ; Y=(E*D)–(B/C)+ A ; Z= A*(D–B)/(C+ E); printf ( “ X= %d n Y= %d n Z=%d n”, X,Y,Z); return 0 ; } C Programming 111 Higher Technological Institute
  • 112. Precedence Example Y=(E*D)–(B/C)+ A ; . . . C Programming 112 Higher Technological Institute
  • 113. Precedence Example Y=(E*D)–(B/C)+ A ; . . . 38 C Programming 113 Higher Technological Institute
  • 114. Precedence Example # include<stdio.h> main ( ) { int A,B,C,D,E, X, Y, Z; A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ; X= E*D – B/C + A ; Y=(E*D)–(B/C)+ A ; Z= A*(D–B)/(C+ E); printf ( “ X= %d n Y= %d n Z=%d n”, X,Y,Z); return 0 ; } C Programming 114 Higher Technological Institute
  • 115. Precedence Example # include<stdio.h> Z( = A * ( D – B ) / ( C + E ) ; main ) { int A,B,C,D,E, X, Y, Z; A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ; X= E*D – B/C + A ; Y=(E*D)–(B/C)+ A ; Z= A*(D–B)/(C+ E); printf ( “ X= %d n Y= %d n Z=%d n”, X,Y,Z); return 0 ; } C Programming 115 Higher Technological Institute
  • 116. Precedence Example Z= A*(D–B)/(C+ E); A = 20 B=6 C=3 D = 10 E=2 C Programming ( ) * / +- 116 Higher Technological Institute
  • 117. Precedence Example Z= A*(D–B)/(C+ E); A = 20 B=6 C=3 D = 10 E=2 C Programming ( ) * / +- 117 Higher Technological Institute
  • 118. Precedence Example Z= A*(D–B)/(C+ E); A = 20 B=6 C=3 D = 10 E=2 C Programming 20 * 4 / 118 5 ( ) * / +- Higher Technological Institute
  • 119. Precedence Example Z= A*(D–B)/(C+ E); A = 20 B=6 C=3 D = 10 E=2 C Programming 20 * 4 / 16 119 5 ( ) * / +- Higher Technological Institute
  • 120. Precedence Example # include<stdio.h> main ( ) { int A,B,C,D,E, X, Y, Z; A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ; X= E*D – B/C + A ; Y=(E*D)–(B/C)+ A ; Z= A*(D–B)/(C+ E); printf ( “ X= %d n Y= %d n Z=%d n”, X,Y,Z); return 0 ; } C Programming 120 Higher Technological Institute
  • 121. Precedence Example # include<stdio.h> main ( ) { int A,B,C,D,E, X, Y, Z; A = 20 ; B = 6 ; C = 3 ; D = 10 ; E = 2 ; X= E*D – B/C + A ; Y=(E*D)–(B/C)+ A ; Z= A*(D–B)/(C+ E); printf ( “ X= %d n Y= %d n Z=%d n”, X,Y,Z); return 0 ; } C Programming 121 Higher Technological Institute
  • 122. Home Work ..!! Exercises 3 Page 44 C Programming 122 Higher Technological Institute
  • 123. Course Contents 1 Introduction 2 Program Development 3 The Essential of C Programs 4 Manipulating Data with Operators 5 Reading from and Writing to Standard I/O 6 Decision 7 Iteration 8 Arrays 9 C Functions C Programming 123 Higher Technological Institute
  • 124. Course Contents 4 Manipulating Data with Operators C Programming 124 Higher Technological Institute
  • 125. Course Contents 4 Manipulating Data with Operators  Errors Types  Arithmetic Assignment Operators  Unary Minus Operator  The Cast Operator C Programming 125 Higher Technological Institute
  • 127. Error Types Syntax Example C Programming They are errors that you have made in the form Or syntax of the language o Spelling a keyword incorrectly o Errors that are detected during running of a program are shown in a dialog box that appears and the error is highlighted in the program. 127 Higher Technological Institute
  • 128. Error Types Execution Example C Programming If the program has no syntax errors. The computer can execute it. During execution, execution errors may be detected. o If a number is divided by zero causes an execution error. Whenever an execution error is detected, the computer displays a dialog box with an error message and stops executing the program. 128 Higher Technological Institute
  • 129. Error Types Logic C Programming If the output of the program does not agree with what is expected, this is logic error. The computer cannot detect such an error because it does not know the logic of the program should be. So, it is your responsibility to detect logic errors in the program. 129 Higher Technological Institute
  • 131. Arithmetic Assignment Operators o Here the statement causes the value of the right-handoperand to be assigned to the memory location of the left-hand-operand. left-hand-operand = right-hand-operand a=5 ; o Statement writes the value of the right-hand-operand (5) into the memory location of the integer variable a (which is the left-hand-operand in this case). b=a=5 ; C Programming 131 Higher Technological Institute
  • 132. Assignment and Arithmetic Operators Operator += -= *= /= %= C Programming Description Addition Assignment Operator Subtraction Assignment Operator Multiplication Assignment Operator Division Assignment Operator Remainder Assignment Operator 132 Higher Technological Institute
  • 133. Equivalence of Statements Statement x+ = y; x- = y; x* = y; x/ = y; x% = y; z=z*x+y; C Programming Equivalence x = x + y; x = x - y; x = x * y; x = x / y; x = x % y; ? 133 z *= x + y ; Higher Technological Institute
  • 134. Equivalence of Statements Statement x+ = y; x- = y; x* = y; x/ = y; x% = y; z=z*x+y; C Programming Equivalence x = x + y; x = x - y; x = x * y; x = x / y; x = x % y; ≠ 134 z *= x + y ; Higher Technological Institute
  • 135. Example # include <stdio.h> main ( ) { int x, y, z; x =1; y =3; printf (“Given x = %d, y = %d, and z = %d,n”, x, y, z); x= x + y; printf ( “ x= x +y assigns %d to x;n”, x); x = 1; x+= y ; printf ( “ x+= y assigns %d to x;n”, x); x = 1; z = z* x + y; printf ( “z = z*x+y assigns %d to z;n”, z); z = 10; z = z* (x + y); printf ( “z = z*( x+ y) assigns %d to z;n”, z); z = 10; z *= x + y; printf ( “z *= x+ y assigns %d to z;n”, z); return 0; } C Programming 135 z= 10; Higher Technological Institute
  • 136. Unary Minus Operator o Given an integer, you can get its negation by changing the sign of the integer by using, -, the minus operator, which is called the unary minus operator. x = 1.234 ; -x equals -1.234 o Differ between the unary minus operator and the subtraction operator. z = x- -y; OR z = x- (-y); o The first – symbol is used as the subtraction operator while the second –symbol is the unary minus operator. C Programming 136 Higher Technological Institute
  • 137. X++ ++X Increment X-- --X Decrement Post- Pre- Incrementing or Decrementing by ONE C Programming 137 Higher Technological Institute
  • 138. Example # include <stdio.h> main ( ) { int x=5; printf (“ X++ %d ,n”, x++); printf (“++X %d ,n”, ++x); printf (“ X-- %d ,n”, x--); printf (“ --X %d ,n”, --x); return 0; } C Programming 138 Higher Technological Institute
  • 139. Relational Operators Operator Description == != > < >= <= Equal to Not equal to Greater than Less than Greater than or equal to Less than or equal to C Programming 139 Higher Technological Institute
  • 140. Relational Operators x*y < z+3 (x * y) < ( z + 3 ) oAll relational expressions produce a result of either 0 or 1. oGiven x = 3 and y = 5, for instance, the relational expression x < y gives a result of 1 C Programming 140 Higher Technological Institute
  • 141. Example # include <stdio.h> main ( ) { int x, y; double z; x = 7; y = 25; z = 24.46; printf (“ Given x = %d, y = %d and z = %2f, n”, x, y, z); printf ( “ x > = y produces: %d n”, x > =y); printf ( “x = = y produces: %d n”, x = =y); printf ( “x < y produces: %d n”, x < y); printf ( “x > y produces: %d n”, x > y); printf ( “x != y - 18 produces: %d n”, x ! =y -18); printf ( “x + y ! = z produces: %d n”, x +y ! = z); return 0; } C Programming 141 Higher Technological Institute
  • 142. Relational Operators 1/2+1/2==1 1 Or 0 This is algebraically True and is supposed to return 1. o However, the expression returns 0 which means that the equal to relationship does not hold. o This is because the truncation of the integer division – that is , 1 /2 – produces 0 not 0.5. C Programming 142 Higher Technological Institute
  • 143. The Cast Operator o You can convert one data type to a different one by prefixing and cast operator to the operand. o The general form of the cast operator is (data-type) x o Here data-type specifies the data type you want to convert to x is a variable ( or, expression) that contains the value of the current data type. o You have to include the parentheses (and) to make up a cast operator. C Programming 143 Higher Technological Institute
  • 144. Example # include <stdio.h> main ( ) { int x, y; x = 7; y = 5; printf ( “ Given x = %d, y = %d, n”, x, y); printf ( “ x / y produces: %d n”, x /y); printf ( “(float) x / y produces: %f n”, (float) x / y); return 0 ; } C Programming 144 Higher Technological Institute
  • 145. Course Contents 1 Introduction 2 Program Development 3 The Essential of C Programs 4 Manipulating Data with Operators 5 Reading from and Writing to Standard I/O 6 Decision 7 Iteration 8 Arrays 9 C Functions C Programming 145 Higher Technological Institute
  • 146. Course Contents 5 Reading from and Writing to Standard I/O C Programming 146 Higher Technological Institute
  • 147. scanf ( ) function o The scanf ( ) function is a very important function in C. o It is used to read data from the standard input device; the keyboard. o The syntax for the scanf ( ) function is : scanf(“ % % ”, & & ); o Example : to input two variables integer x and float y scanf(“ C Programming %d %f ”, 147 &x &y ); Higher Technological Institute
  • 148. scanf ( ) function o The scanf ( ) function is a very important function in C. o It is used to read data from the standard input device; the keyboard. o One thing you need to be aware of; is that the scanf ( ) function doesn’t actually start reading the input until the Enter key is pressed. o Data entered from the keyboard is placed in an input buffer. o When the Enter key is pressed, the scanf () function looks for its input in the buffer. C Programming 148 Higher Technological Institute
  • 149. printf( ) function o The printf ( ) function is a very important function in C. o It is used to print out messages on the screen. o The syntax for the printf ( ) function is printf(“ % % ”, ); o Example : to print two variables integer x and float y printf(“ %d C Programming %f ”, 149 x y ); Higher Technological Institute
  • 150. printf( ) function o The printf ( ) function is a very important function in C. o It is used to print out messages on the screen. o The number of expressions is determined by the number of the format specifiers inside the first argument. o The format specifiers and the expressions are matched in order from left to right, and you should use exactly the same number of expressions as the number of format specifiers within the format string. C Programming 150 Higher Technological Institute
  • 151. Adding the Minimum Field Width o A integer is added between the percent sign (%) and the letter in a format specifier to specify the minimum field width and ensures that the output reaches the minimum width. o For example in %10f printf(“ %10f ”, x ); 10 is a minimum field width specifier that ensures that the output is at least 10 character spacers wide. C Programming 151 Higher Technological Institute
  • 152. Example # include <stdio.h> main ( ) { int x=12; int y=12345; printf (“%d n”, x); printf (“%d n”, y); printf (“%5d n”, x); printf (“%05d n”, x); printf (“%2d n”, y); return 0; } C Programming 152 Higher Technological Institute
  • 153. Example # include <stdio.h> main ( ) { int x=12; int y=12345; printf (“%d n”, x); printf (“%d n”, y); printf (“%5d n”, x); printf (“%05d n”, x); printf (“%2d n”, y); return 0; } C Programming 153 12 12345 12 00012 12345 Higher Technological Institute
  • 154. Aligning Output o By default, all output is placed on the Right edge of the field, as long as the field width is longer than the width of the output. o You can change this and force output to be leftjustified. o To do so, you need to prefix the minimum field specifier with the minus sign (-). %-12d o Specifies the minimum field width as 12, and justifies the output from the left edge of the field. C Programming 154 Higher Technological Institute
  • 155. Example # include <stdio.h> main ( ) { int x,y,z,m,n; m=1234; printf (“%8d %-8d printf (“%8d %-8d printf (“%8d %-8d printf (“%8d %-8d printf (“%8d %-8d return 0; } C Programming x=1; y=12; z=123; n=12345; n”, x, x ); n”, y, y ); n”, z, z ); n”, m, m ); n”, n, n ); 155 Higher Technological Institute
  • 156. Example # include <stdio.h> main ( ) { int x,y,z,m,n; m=1234; printf (“%8d %-8d printf (“%8d %-8d printf (“%8d %-8d printf (“%8d %-8d printf (“%8d %-8d return 0; } C Programming x=1; y=12; z=123; n=12345; n”, x, x ); n”, y, y ); n”, z, z ); n”, m, m ); n”, n, n ); 1 12 123 1234 12345 156 1 12 123 1234 12345 Higher Technological Institute
  • 157. The Precision Specifier o You can put a period (.) and an integer right after the minimum field width specifier. o The combination of the period and the integer make up a precision specifier. o The precision specifier is another important specifier you can use to determine the number of decimal places for floating-point numbers, or to specify the maximum field width for integers or strings. %10.3f o The minimum field width length is specified as 10 character long, and the number of decimal places is set to 3. C Programming 157 Higher Technological Institute
  • 158. The Precision Specifier o You can put a period (.) and an integer right after the minimum field width specifier. o The combination of the period and the integer make up a precision specifier. o The precision specifier is another important specifier you can use to determine the number of decimal places for floating-point numbers, or to specify the maximum field width for integers or strings. o Remember, the default number of decimal places is 6. C Programming 158 Higher Technological Institute
  • 159. Example # include <stdio.h> main ( ) { int x=123; float y=123.456789; printf (“Default integer format n printf (“With precision specifier n printf (“Default float format n printf (“With precision specifier n printf (“With precision specifier n return 0; } C Programming 159 %d n”, x ); %2.8d n”, x ); %f n”, y ); %10.2f n”, y ); %-10.2f n”, y ); Higher Technological Institute
  • 160. Example # include <stdio.h> main ( ) { int x=123; float y=123.456789; printf (“Default integer format n printf (“With precision specifier n printf (“Default float format n printf (“With precision specifier n printf (“With precision specifier n return 0; } C Programming 160 %d n”, x ); %2.8d n”, x ); %f n”, y ); %10.2f n”, y ); %-10.2f n”, y ); Higher Technological Institute
  • 161. Course Contents 1 Introduction 2 Program Development 3 The Essential of C Programs 4 Manipulating Data with Operators 5 Reading from and Writing to Standard I/O 6 Decision 7 Iteration 8 Arrays 9 C Functions C Programming 161 Higher Technological Institute
  • 163. Course Contents Decision 6  The if statement  Flowchart of if statement  The if else statement and flowchart  Nested if statements and their flowcharts  The switch statement  The break and continue statement  The goto statements C Programming 163 Higher Technological Institute
  • 164. The if statement o The if statement is the most popular conditional branching statement. o It can be used to evaluate the conditions as well as to make the decision whether the block of code controlled by the statement is going to be executed. o Here if condition is logical TRUE, the statements inside the braces are executed. o If condition is logical FALSE, then the statements are skipped. C Programming 164 Higher Technological Institute
  • 165. The if statement Flowchart IF Statement Syntax ….. if ( Test Expression) { statement1; statement2; … } …. C Programming 165 Higher Technological Institute
  • 166. The if statement Flowchart IF Statement Syntax … if ( Test Expression) statement; … Example if ( x>5) printf( “ X > 5 ” ); … C Programming 166 Higher Technological Institute
  • 167. Example # include <stdio.h> main ( ) { int i; printf (“Integers that can be divided by 3 n ” ); printf (“Enter a positive number : n ”, x ); scanf(“%d”, &i ); if ( i %3 == 0 ) printf (“The number %d is divisible by 3 n ”, i ); return 0; } C Programming 167 Higher Technological Institute
  • 168. Output Integers that can be divided by 3 Enter a positive number : 12 The number 12 is divisible by 3 Integers that can be divided by 3 Enter a positive number : 7 C Programming 168 Higher Technological Institute
  • 169. The if else statement if else Statement Syntax … if ( Test Expression) { statement1; statement2; … } else { statement_A statement_B … } … C Programming 169 Higher Technological Institute
  • 170. The if else statement if else Statement Syntax … if ( Test Expression) statement1; else statementA; … Example if ( X > 5) printf( “ X > 5 ” ); else printf( “ X < 5 ” ); C Programming 170 Higher Technological Institute
  • 171. if else Example Draw a flow chart to read two numbers and compare between both if they are equal or not. C Programming 171 Higher Technological Institute
  • 172. if else Example # include <stdio.h> main ( ) { int x,y; printf (“Please Enter X and Y n ” ); scanf(“%d %d”, &x , &y ); if ( x == y ) printf (“ X = Y n ” ); else printf (“ X != Y n ” ); return 0; } C Programming 172 Higher Technological Institute
  • 173. Nested if Flowchart Nest IF Statement Syntax if ( Test Expression1) { if ( Test Expression2) { statement1; statement2; } else { statementA; statementB; } }else { statement X ; } C Programming 173 Higher Technological Institute
  • 174. Nested if Flowchart Draw a flow chart to determine the minimum value of three input numbers. C Programming 174 Higher Technological Institute
  • 175. Nested if Example # include <stdio.h> main ( ) { int A,B,C; printf (“Please Enter A,B and C n ” ); scanf(“%d %d %d”, &A , &B , &C ); if ( A<B ) { if ( A<C ) printf (“ A The is the Smallest n ” ); else printf (“ C The is the Smallest n ” ); } else if ( B<C ) printf (“ B The is the Smallest n ” ); else printf (“ C The is the Smallest n ” ); return 0; } C Programming 175 Higher Technological Institute
  • 176. The switch statement o The nested if statements will become very complex if there are many decisions that need to be made. o The switch statement, can be used to make unlimited decisions or choices based on the value of a conditional expression and specified cases. o The conditional expression is evaluated first, if the return value of the conditional expression is equal to the constant expression expression 1, the statement statement 1 is executed. o the value of the conditional expression is not equal to any values of the constant expressions labeled by the case keyword, the statement (statement-default) following by the default keyword is executed. C Programming 176 Higher Technological Institute
  • 177. The Switch Statement Syntax switch ( Conditional Expression ) { case expression1; statment1; case expression2; statment2; … default; statement-default; } C Programming 177 Higher Technological Institute
  • 178. The Switch Statement Syntax switch ( Conditional Expression ) { case expression1; statment1; case expression2; statment2; … default; statement-default; } o You have to use the case keyword to label each case. o The default keyword is recommended to be used for the default case. o Note that no constant expressions are identical in the switch statement. C Programming 178 Higher Technological Institute
  • 179. The break statement o An important feature of the switch statement is that the computer continues to execute the statements following the selected case until the end of the switch statement. o You can add a break statement at the end of the statement list following every case label, if you want to exit the switch construct after the statements within a selected case are executed C Programming 179 Higher Technological Institute
  • 180. The Break Statement Syntax switch ( Conditional Expression ) { case expression1; statment1; break ; case expression2; statment2; break; … default; statement-default; } C Programming 180 Higher Technological Institute
  • 181. Example Write Program that compute the grade for a student, the grade is based on Total Score T ( from 0 to 100) The Grading Scale Is Illustrated In The Following Table: Total Score 85 <= T <= 100 80 <= T < 85 75 <= T < 80 70 <= T < 75 65 <= T < 70 55 <= T < 65 50 <= T < 55 T < 50 C Programming Letter Grade A B+ B C+ C D+ D F 181 Higher Technological Institute
  • 182. Example # include <stdio.h> main ( ) { int score; printf (" Enter Scorre: n"); scanf ("%d", &score); switch (score / 10 ) { case 10: printf ("your grade is A n"); break; case 9: printf ("your grade is A n"); break; case 8: printf ("your grade is B n"); break; case 7: printf ("your grade is C n"); break; C Programming case 6: printf ("your grade is D n"); break; case 5: printf ("your grade is D n"); break; case 4: printf ("your grade is F n"); break; case 3: printf ("your grade is F n"); break; case 2: printf ("your grade is F n"); break; case 1: printf ("your grade is F n"); break; 182 case 0: printf ("your grade is F n"); break; default: printf ( "illegal input n"); break; } printf ( "GOODBYEn" ); return 0; } Higher Technological Institute
  • 183. The Continue statement o There are times when you want to stay in a loop but skip over some statements within the loop. o To do this, the continue statement causes execution to jump to the top of the loop immediately. o The statement can be used within a while, do…while or for statement to terminate the current iteration of the loop and begin the next. C Programming 183 Higher Technological Institute
  • 184. The goto statement o Do not use the goto statement unless it’s absolutely necessary because its usage may make the C program unreliable and hard to debug. o The Syntax of goto Statement: … lablename : statment1; statment2; .. goto lablename ; … C Programming 184 Higher Technological Institute
  • 185. goto Example # include <stdio.h> main ( ) { int x; guess_lbl: printf (“Guess integer number in the range 1 to 10 n”); scanf (“%d”, &x ); if ( x == 7) goto rght_lbl; printf (“NO, wrong guess, try again n”); goto guess_lbl; rght_lbl: printf (“WELL DONE your guess = %d is rightn” , x); return 0; } C Programming Higher Technological Institute 185
  • 186. goto Example # include <stdio.h> main ( ) { int x; guess_lbl: printf (“Guess integer number in the range 1 to 10 n”); scanf (“%d”, &x ); if ( x == 7) goto rght_lbl; printf (“NO, wrong guess, try again n”); goto guess_lbl; rght_lbl: printf (“WELL DONE your guess = %d is rightn” , x); return 0; } C Programming 186 Higher Technological Institute
  • 187. Course Contents 1 Introduction 2 Program Development 3 The Essential of C Programs 4 Manipulating Data with Operators 5 Reading from and Writing to Standard I/O 6 Decision 7 Iteration 8 Arrays 9 C Functions C Programming 187 Higher Technological Institute
  • 189. Course Contents 7 Iteration  The for statement  The Null Statement  The while statement  The infinite while Loop  The do-while Statement  The Nested Loop  Examples C Programming 189 Higher Technological Institute
  • 190. Iteration o Iteration (looping) is used in programming to perform the same set of statements over and over until certain specified conditions are met. Repeat the same statement(s) until certain condition C Programming 190 Higher Technological Institute
  • 191. Iteration Example Draw a flow chart to generate a table of squares and cubes of integers from 1 to 9. C Programming 191 Higher Technological Institute
  • 192. Iteration Example Draw a flow chart to generate a table of squares and cubes of integers from 1 to 9. From 1 to 9 Step 1 C Programming 192 Higher Technological Institute
  • 193. The for statement for Statement Syntax One Statement for ( initialization ; Test Expression ; Step) statement; … for Statement Syntax Multiple Statements for ( initialization ; Test Expression ; Step) { statement1; statement2; … } C Programming 193 Higher Technological Institute
  • 194. The for statement for ( i=1 ; i <= 10 ; i++ ) printf (“ Hello n ”); printf (“ Goodbye n ”); for ( i=1 ; i <= 10 ; i++ ) { printf (“ Hello n ”); printf (“ Goodbye n ”); } C Programming 194 Will Print Hello 10 Times Then Print Goodbye Will Print Hello Goodbye 10 Times Higher Technological Institute
  • 195. Example # include <stdio.h> main ( ) { int i=1; int j,k; for ( i=1 ; i <= 9 ; i++ ) { j=i*i; k=j*i; printf (“ i = %d ”, i ) ; printf (“ j = %d ”, j ) ; printf (“ k = %d n”, k ) ; } return 0; } C Programming 195 Higher Technological Institute
  • 196. Null statement for ( i=1 ; i <= 10 ; i++ ) ; printf (“ Hello n ”); printf (“ Goodbye n ”); C Programming 196 Higher Technological Institute
  • 197. The while statement while Statement Syntax Initialization while ( Test Expression ) { … Increment Or Decrement ; } C Programming 197 Higher Technological Institute
  • 198. The while statement int w = 1; while ( w <= 10 ) { printf (“ Hello n ”); w = w++; } printf (“ Goodbye n ”); C Programming 198 Will Print Hello 10 Times Then Print Goodbye Higher Technological Institute
  • 199. While Loop Example # include <stdio.h> main ( ) { int i=1; int j,k; while (i<=10) { j=i*i; k=j*i; printf (“ i = %d ”, i ) ; printf (“ j = %d ”, j ) ; printf (“ k = %d n”, k ) ; i=i+1; } return 0; } C Programming 199 Higher Technological Institute
  • 200. The Infinite while loop while(1) { Statement1; Statement2; } Always returns 1, the statements inside the statement block will be executed over and over- that is, the while loop will continue forever. You can set certain conditions inside the while loop to break the infinite loop as soon as the conditions are met. C Programming 200 Higher Technological Institute
  • 201. The do-while statement do-while Statement Syntax Initialization do { … Increment Or Decrement ; ; } while ( Test Expression ) C Programming 201 Higher Technological Institute
  • 202. The do-while statement do-while Statement Syntax Initialization do { … Increment Or Decrement ; ; } while ( Test Expression ) Only after do-while loop Don’t forget to write C Programming ; 202 Higher Technological Institute
  • 203. The do-while statement int w = 1; do { printf (“ Hello n ”); w = w++; } while ( w <= 10 ) ; Will Print Hello 10 Times Then Print Goodbye printf (“ Goodbye n ”); C Programming 203 Higher Technological Institute
  • 204. do-while Loop Example # include <stdio.h> main ( ) { int i=1; int j,k; do { j=i*i; k=j*i; printf (“ i = %d ”, i ) ; printf (“ j = %d ”, j ) ; printf (“ k = %d n”, k ) ; i=i+1; } while (i<=10); return 0; } C Programming 204 Higher Technological Institute
  • 205. Examples Example 1 Write a program to calculate the summation and Average of 20 values read from a data statement. C Programming 205 Higher Technological Institute
  • 206. Examples # include <stdio.h> main ( ) { int i,x,sum; float av; sum=0; for ( i=1 ; i<=20 ; i++ ) { printf(“Please enter new no n”); scanf(“%d”,&x); sum=sum+x; } av=sum/20; printf(“Sum=%d n Av=%fn”, sum , av); return 0; } C Programming 206 Higher Technological Institute
  • 207. Examples Example 2 Write a program to calculate the summation and Average of N numbers read from a data statement. C Programming 207 Higher Technological Institute
  • 208. Examples # include <stdio.h> main ( ) { int i,n,x,sum; float av; sum=0; printf(“How many Numbers ? n”); scanf(“%d”,&n); for ( i=1 ; i<=n ; i++ ) { printf(“Please enter new no n”); scanf(“%d”,&x); sum=sum+x; } av=sum/n; printf(“Sum=%d n Av=%fn”, sum , av); return 0; } C Programming 208 Higher Technological Institute
  • 209. Flowcharts Examples Example 3 Nested Loop Write a program and Draw its flowchart to compute all possible products of X and Y X and Y varying from 1 through 9 (100 products) 1x0=0 1x1=0 1x2=0 1x3=0 . . 1x9=0 C Programming 2x0=0 2x1=1 2x2=2 2x3=3 . . 2x9=9 209 … … … … 9x0=0 9x1=9 9x2=18 9x3=27 … 9x9=81 Higher Technological Institute
  • 210. Flowcharts Examples Example 3 Nested Loop # include <stdio.h> int main ( ) { int x,y; for ( x=1 ; x <= 9 ; x++) for ( y=1 ; y <= 9 ; y++) printf(“%d x %d = %dn”,x,y,x*y); return 0; } C Programming 210 Higher Technological Institute
  • 211. Flowcharts Examples Example 4 Draw a flowchart and write a Program that Calculate and Display The Factorial of n 𝒏! = 𝒏 × 𝒏 − 𝟏 × 𝒏 − 𝟐 … × 𝟑 × 𝟐 × 𝟏 C Programming 211 Higher Technological Institute
  • 212. Flowcharts Examples # include <stdio.h> int main ( ) { int n,fact; fact=1; printf(“Enter your N n”); scanf(“%d”,&n); for( int i=1;i<=n;i++) fact=fact*i; printf(“ N! =%d n”,fact); return 0; } C Programming 212 Higher Technological Institute
  • 213. Home Work..!! Rewrite The previous examples using: a) while Loop b) do-while Loop C Programming 213 Higher Technological Institute
  • 214. Course Contents 1 Introduction 2 Program Development 3 The Essential of C Programs 4 Manipulating Data with Operators 5 Reading from and Writing to Standard I/O 6 Decision 7 Iteration 8 Arrays 9 C Functions C Programming 214 Higher Technological Institute
  • 216. Examples Example 2 Write a program to calculate the summation and Average of N numbers read from a data statement. C Programming 216 Higher Technological Institute
  • 217. Examples # include <stdio.h> main ( ) { int i,n,x,sum; float av; sum=0; printf(“How many Numbers ? n”); scanf(“%d”,&n); for ( i=1 ; i<=n ; i++ ) { printf(“Please enter new no n”); scanf(“%d”,&x); sum=sum+x; } av=sum/n; printf(“Sum=%d n Av=%fn”, sum , av); return 0; } C Programming 217 Higher Technological Institute
  • 218. Arrays  An array is a collection of variables that are of the same data type.  Each item in an array is called an element.  All elements in an array are referenced by the name of the array and are stored in a set of consecutive memory slots.  The array takes two forms :  One dimensional array (1-D) ; where the data is arranged horizontally or vertically.  Multi-dimensional array; where the data is arranged in two dimensional (2-D), three dimensional (3-D),…etc. C Programming 218 Higher Technological Institute
  • 219. Arrays  The following is the syntax form to declare an array: data-type Array-Name [Array-Size];  The data-type is the type specifier that indicates what data type the declared array will be.  Array-Name is the name of the declared array. When choosing a name for an array, you should follow the same rules of naming a variable.  Array-Size defines how many elements the array can contain. Note that the brackets are required in declaring an array. C Programming 219 Higher Technological Institute
  • 220. Arrays Example int array_int [8];  Where int specifies the data type of the array whose name is arrat_int.  The size of the array is 8, which means that the array can store 8 elements C Programming 220 Higher Technological Institute
  • 221. Indexing Arrays  You can access each of the elements in the array separately.  The following declaration declares an array called day of integer numbers: int day [7];  You can access the elements in the array of day one after another: day[0], day[1], …, day[6].  The important thing to remember is that all arrays in C are indexed starting at 0.  The first element in the array of day is day[0]. C Programming 221 Higher Technological Institute
  • 222. Initializing Arrays  You can initialize each element in an array by two way.  For instance if we have the array : int number [5];  Initialize the first element in the array: like this: number[0] = 24;  Here the value 24 is assigned to the first element of the array number, number[0]. C Programming 222 Higher Technological Institute
  • 223. Initializing Arrays  The second way to initialize an array is to initialize all elements in the array together.  For instance, the following statement initializes all elements of the array, number: int number [5]= {100, 4, 24, 34,5, 16};  Here the integers inside the braces are assigned to the corresponding elements of the array number.  That is, 100 is given to the first element (number[0]), 4 to the second element (number[1]), and so on. C Programming 223 Higher Technological Institute
  • 224. Initializing Arrays  The second way to initialize an array is to initialize all elements in the array together.  For instance, the following statement initializes all elements of the array, number: int number [5]= {100, 4, 24};  Here the integers inside the braces are assigned to the corresponding elements of the array number.  That is, 100 is given to the first element (number[0]), 4 to the second element (number[1]), and so on. C Programming 224 Higher Technological Institute
  • 225. Initializing Arrays # include <stdio.h> main ( ) { int i; int A[10]; for ( i=0 ; i<10 ; i++ ) { A[i]=i+1; printf(“A[%d]=%d n”,i,A[i]); } return 0; } C Programming 225 Higher Technological Institute
  • 226. Initializing Arrays # include <stdio.h> main ( ) { int i; int A[10]; for ( i=0 ; i<10 ; i++ ) { A[i]=i+1; printf(“A[%d]=%d n”,i,A[i]); } return 0; } C Programming 226 Higher Technological Institute
  • 227. Example Example Write a program to calculate the Summation and Average of 10 numbers read from a data statement.( Using Array ) C Programming 227 Higher Technological Institute
  • 228. Example # include <stdio.h> main ( ) { int i; int A[10]; int sum=0; float av; for ( i=0 ; i<10 ; i++ ) { printf(“Enter the value of Element [%d] n”,i); scanf(“%d”,A[i]); sum=sum+A[i] } av=sum/10; printf(“Sum=%d n Av=%fn”, sum , av); return 0; } C Programming 228 Higher Technological Institute
  • 229. Two Dimensional Arrays Rows int A[3][4] ; Columns C Programming 229 Higher Technological Institute
  • 230. Two Dimensional Arrays Assigning Static Value int A[3][4] = { { 1 , 2 , 3 , 4 }, { 5 , 6 , 7 , 8 }, { 9 , 8 , 7 , 6 }} ; OR A[3][4] = { { 1 , 2 , 3 , 4 }, { 5 , 6 , 7 , 8 }, { 9 , 8 , 7 , 6 }} ; C Programming 230 Higher Technological Institute
  • 231. Two Dimensional Arrays Example 1 Write a program to calculate and display the Summation of two Matrices A and B. A= 𝟏 𝟒 𝟕 C Programming 𝟐 𝟓 𝟖 𝟑 𝟔 𝟗 ,B= 231 𝟗 𝟔 𝟑 𝟖 𝟓 𝟐 𝟕 𝟒 𝟏 Higher Technological Institute
  • 232. Example # include <stdio.h> main ( ) { int A[3][3] = { {1,2,3}, {4,5,6}, { 7 , 8, 9 } } ; int B[3][3] = { {9,8,7}, {6,5,4}, { 3 , 2, 1 } } ; return 0; } C Programming 232 Higher Technological Institute
  • 233. Example # include <stdio.h> main ( ) { int A[3][3] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8, 9 } } ; int B[3][3] = { { 9 , 8 , 7 } , { 6 , 5 , 4 } , { 3 , 2, 1 } } ; return 0; C Programming } 233 Higher Technological Institute
  • 234. Example # include <stdio.h> main ( ) { int A[3][3] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8, 9 } } ; int B[3][3] = { { 9 , 8 , 7 } , { 6 , 5 , 4 } , { 3 , 2, 1 } } ; int S[3][3] ; int i,j; for ( i=0 ; i<3 ; i++ ) /* Rows*/ for ( j=0 ; j<3 ; j++ ) /* Columns*/ { S[i][j]=A[i][j]+B[i][j]; printf(“S[%d][%d] = %d n ”, i , j , S [ i ][ j ] ) ; } return 0; } C Programming 234 Higher Technological Institute
  • 235. Example S3,3 = A3,3 + B3,3 = 𝟏 𝟒 𝟕 𝟐 𝟓 𝟖 𝟑 𝟗 𝟔 + 𝟔 𝟗 𝟑 𝟖 𝟓 𝟐 𝟕 𝟒 = 𝟏 𝟏𝟎 𝟏𝟎 𝟏𝟎 𝟏𝟎 𝟏𝟎 𝟏𝟎 𝟏𝟎 𝟏𝟎 𝟏𝟎 at i = 0 for ( i=0 ; i<3 ; i++ ) /* Rows*/ j=0 for ( j=0 ; j<3 ; j++ ) /* Columns*/ { S[i][j]=A[i][j]+B[i][j]; printf(“S[%d][%d] = %d n ”, i , j , S [ i ][ j ] ) ; } return 0; } C Programming 235 Higher Technological Institute
  • 236. Example S3,3 = A3,3 + B3,3 = 𝟏 𝟒 𝟕 𝟐 𝟓 𝟖 𝟑 𝟗 𝟔 + 𝟔 𝟗 𝟑 𝟖 𝟓 𝟐 𝟕 𝟒 = 𝟏 𝟏𝟎 𝟏𝟎 𝟏𝟎 𝟏𝟎 𝟏𝟎 𝟏𝟎 𝟏𝟎 𝟏𝟎 𝟏𝟎 at i = 0 for ( i=0 ; i<3 ; i++ ) /* Rows*/ j=1 for ( j=0 ; j<3 ; j++ ) /* Columns*/ { S[i][j]=A[i][j]+B[i][j]; printf(“S[%d][%d] = %d n ”, i , j , S [ i ][ j ] ) ; } return 0; } C Programming 236 Higher Technological Institute
  • 237. Example S3,3 = A3,3 + B3,3 = 𝟏 𝟒 𝟕 𝟐 𝟓 𝟖 𝟑 𝟗 𝟔 + 𝟔 𝟗 𝟑 𝟖 𝟓 𝟐 𝟕 𝟒 = 𝟏 𝟏𝟎 𝟏𝟎 𝟏𝟎 𝟏𝟎 𝟏𝟎 𝟏𝟎 𝟏𝟎 𝟏𝟎 𝟏𝟎 at i = 1 for ( i=0 ; i<3 ; i++ ) /* Rows*/ j=1 for ( j=0 ; j<3 ; j++ ) /* Columns*/ { S[i][j]=A[i][j]+B[i][j]; printf(“S[%d][%d] = %d n ”, i , j , S [ i ][ j ] ) ; } return 0; } C Programming 237 Higher Technological Institute
  • 238. Example S3,3 = A3,3 + B3,3 = 𝟏 𝟒 𝟕 𝟐 𝟓 𝟖 𝟑 𝟗 𝟔 + 𝟔 𝟗 𝟑 𝟖 𝟓 𝟐 𝟕 𝟒 = 𝟏 𝟏𝟎 𝟏𝟎 𝟏𝟎 𝟏𝟎 𝟏𝟎 𝟏𝟎 𝟏𝟎 𝟏𝟎 𝟏𝟎 at i = 2 for ( i=0 ; i<3 ; i++ ) /* Rows*/ j=0 for ( j=0 ; j<3 ; j++ ) /* Columns*/ { S[i][j]=A[i][j]+B[i][j]; printf(“S[%d][%d] = %d n ”, i , j , S [ i ][ j ] ) ; } return 0; } C Programming 238 Higher Technological Institute
  • 239. Example S3,3 = A3,3 + B3,3 = 𝟏 𝟒 𝟕 𝟐 𝟓 𝟖 𝟑 𝟗 𝟔 + 𝟔 𝟗 𝟑 𝟖 𝟓 𝟐 𝟕 𝟒 = 𝟏 𝟏𝟎 𝟏𝟎 𝟏𝟎 𝟏𝟎 𝟏𝟎 𝟏𝟎 𝟏𝟎 𝟏𝟎 𝟏𝟎 at i = 2 for ( i=0 ; i<3 ; i++ ) /* Rows*/ j=2 for ( j=0 ; j<3 ; j++ ) /* Columns*/ { S[i][j]=A[i][j]+B[i][j]; printf(“S[%d][%d] = %d n ”, i , j , S [ i ][ j ] ) ; } return 0; } C Programming 239 Higher Technological Institute
  • 240. Example # include <stdio.h> main ( ) { int A[3][3] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8, 9 } } ; int B[3][3] = { { 9 , 8 , 7 } , { 6 , 5 , 4 } , { 3 , 2, 1 } } ; int S[3][3] ; int i,j; for ( i=0 ; i<3 ; i++ ) /* Rows*/ for ( j=0 ; j<3 ; j++ ) /* Columns*/ { S[i][j]=A[i][j]+B[i][j]; printf(“S[%d][%d] = %d n ”, i , j , S [ i ][ j ] ) ; } return 0; } C Programming 240 Higher Technological Institute
  • 241. Two Dimensional Arrays Example 2 Write a program to calculate and display the Summation of any two 3x3 Matrices. C Programming 241 Higher Technological Institute
  • 242. Example 2 # include <stdio.h> main ( ) { int A[3][3] , B[3][3] , S[3][3]; int i,j ; . . . . for ( i=0 ; i<3 ; i++ ) /* Rows*/ for ( j=0 ; j<3 ; j++ ) /* Columns*/ { S[i][j]=A[i][j]+B[i][j]; printf(“S[%d][%d] = %d n ”, i , j , S [ i ][ j ] ) ; } return 0; } C Programming 242 Higher Technological Institute
  • 243. Example 2 # include <stdio.h> main ( ) { int A[3][3] , B[3][3] , S[3][3]; int i,j ; for ( i=0 ; i<3 ; i++ ) /* Rows of Metrics A*/ for ( j=0 ; j<3 ; j++ ) /* Columns of Metrics A */ { printf(“Please Enter A[%d][%d] = n ”, i , j ) ; scanf(“%d”,A[i][j]); } for ( i=0 ; i<3 ; i++ ) /* Rows*/ for ( j=0 ; j<3 ; j++ ) /* Columns*/ { S[i][j]=A[i][j]+B[i][j]; printf(“S[%d][%d] = %d n ”, i , j , S [ i ][ j ] ) ; } return 0; } C Programming 243 Higher Technological Institute
  • 244. Example 2 # include <stdio.h> main ( ) { int A[3][3] , B[3][3] , S[3][3]; int i,j ; for ( i=0 ; i<3 ; i++ ) /* Rows of Metrics A*/ for ( j=0 ; j<3 ; j++ ) /* Columns of Metrics A */ { printf(“Please Enter A[%d][%d] = n ”, i , j ) ; scanf(“%d”,A[i][j]); for ( i=0 ; i<3 ; i++ ) /* Rows of Metrics B*/ for ( j=0 ; j<3 ; j++ ) /* Columns of Metrics B */ { printf(“Please Enter B[%d][%d] = n ”, i , j ) ; scanf(“%d”, B[i][j]); for ( i=0 ; i<3 ; i++ ) /* Rows*/ for ( j=0 ; j<3 ; j++ ) /* Columns*/ { S[i][j]=A[i][j]+B[i][j]; printf(“S[%d][%d] = %d n ”, i , j , S [ i ][ j ] ) ; } return 0; } C Programming 244 } } Higher Technological Institute
  • 245. Two Dimensional Arrays Example 3 Write a program to calculate and display the Summation of any two 3x3 Matrices. Then from Summation Matrix Find : a) The Largest and Smallest element. b) The average c) Matrix Transpose C Programming 245 Higher Technological Institute
  • 246. Example 3 # include <stdio.h> main ( ) { int A[3][3] , B[3][3] , S[3][3]; int i,j ; for ( i=0 ; i<3 ; i++ ) /* Rows of Metrics A*/ for ( j=0 ; j<3 ; j++ ) /* Columns of Metrics A */ { printf(“Please Enter A[%d][%d] = n ”, i , j ) ; scanf(“%d”,A[i][j]); for ( i=0 ; i<3 ; i++ ) /* Rows of Metrics B*/ for ( j=0 ; j<3 ; j++ ) /* Columns of Metrics B */ { printf(“Please Enter B[%d][%d] = n ”, i , j ) ; scanf(“%d”, B[i][j]); for ( i=0 ; i<3 ; i++ ) /* Rows*/ for ( j=0 ; j<3 ; j++ ) /* Columns*/ S[i][j]=A[i][j]+B[i][j]; return 0; } C Programming 246 } } Higher Technological Institute
  • 247. Example 3 # include <stdio.h> main ( ) { int A[3][3] , B[3][3] , S[3][3]; { { int i,j ; for ( i=0 ; i<3 ; i++ ) /* Rows of Metrics A*/ for ( j=0 ; j<3 ; j++ ) /* Columns of Metrics A */ printf(“Please Enter A[%d][%d] = n ”, i , j ) ; scanf(“%d”,A[i][j]); for ( i=0 ; i<3 ; i++ ) /* Rows of Metrics B*/ for ( j=0 ; j<3 ; j++ ) /* Columns of Metrics B */ printf(“Please Enter B[%d][%d] = n ”, i , j ) ; scanf(“%d”, B[i][j]); for ( i=0 ; i<3 ; i++ ) /* Rows*/ for ( j=0 ; j<3 ; j++ ) /* Columns*/ } } S[i][j]=A[i][j]+B[i][j]; int L=S[0][0], M=S[0][0]; for ( i=0 ; i<3 ; i++ ) /* Rows*/ for ( j=0 ; j<3 ; j++ ) /* Columns*/ if (L<S[i][j]) L=S[i][j]; return 0; } C Programming 247 Higher Technological Institute
  • 248. Example 3 (a) # include <stdio.h> main ( ) { int A[3][3] , B[3][3] , S[3][3]; int i,j ; for ( i=0 ; i<3 ; i++ ) for ( j=0 ; j<3 ; j++ ) { printf(“Please Enter A[%d][%d] = n ”, i , j ) ; scanf(“%d”,A[i][j]); } for ( i=0 ; i<3 ; i++ ) for ( j=0 ; j<3 ; j++ ) { printf(“Please Enter B[%d][%d] = n ”, i , j ) ; scanf(“%d”, B[i][j]); } for ( i=0 ; i<3 ; i++ ) for ( j=0 ; j<3 ; j++ ) S[i][j]=A[i][j]+B[i][j]; int L=S[0][0], M=S[0][0]; for ( i=0 ; i<3 ; i++ ) for ( j=0 ; j<3 ; j++ ) if (L<S[i][j]) L=S[i][j]; C Programming 248 for ( i=0 ; i<3 ; i++ ) for ( j=0 ; j<3 ; j++ ) if (M>S[i][j]) M=S[i][j]; printf(“Largest Element = %d n ”, L ) ; printf(“Smallest Element = %d n ”, M ) ; return 0; } Higher Technological Institute
  • 249. Example 3 (a) (b) # include <stdio.h> main ( ) { int A[3][3] , B[3][3] , S[3][3]; int i,j ; for ( i=0 ; i<3 ; i++ ) for ( j=0 ; j<3 ; j++ ) { printf(“Please Enter A[%d][%d] = n ”, i , j ) ; scanf(“%d”,A[i][j]); } for ( i=0 ; i<3 ; i++ ) for ( j=0 ; j<3 ; j++ ) { printf(“Please Enter B[%d][%d] = n ”, i , j ) ; scanf(“%d”, B[i][j]); } for ( i=0 ; i<3 ; i++ ) for ( j=0 ; j<3 ; j++ ) S[i][j]=A[i][j]+B[i][j]; int L=S[0][0], M=S[0][0]; for ( i=0 ; i<3 ; i++ ) for ( j=0 ; j<3 ; j++ ) if (L<S[i][j]) L=S[i][j]; C Programming 249 for ( i=0 ; i<3 ; i++ ) for ( j=0 ; j<3 ; j++ ) if (M>S[i][j]) M=S[i][j]; printf(“Largest Element = %d n ”, L ) ; printf(“Smallest Element = %d n ”, M ) ; int sum = 0 ; for ( i=0 ; i<3 ; i++ ) for ( j=0 ; j<3 ; j++ ) sum=sum + S[i][j]; float average =sum/9 ; printf(“Average = %d n ”, average ) ; return 0; } Higher Technological Institute
  • 250. Example 3 (a) (b) (c) # include <stdio.h> main ( ) { int A[3][3] , B[3][3] , S[3][3]; int i,j ; for ( i=0 ; i<3 ; i++ ) for ( j=0 ; j<3 ; j++ ) { printf(“Please Enter A[%d][%d] = n ”, i , j ) ; scanf(“%d”,A[i][j]); } for ( i=0 ; i<3 ; i++ ) for ( j=0 ; j<3 ; j++ ) { printf(“Please Enter B[%d][%d] = n ”, i , j ) ; scanf(“%d”, B[i][j]); } for ( i=0 ; i<3 ; i++ ) for ( j=0 ; j<3 ; j++ ) S[i][j]=A[i][j]+B[i][j]; int L=S[0][0], M=S[0][0]; for ( i=0 ; i<3 ; i++ ) for ( j=0 ; j<3 ; j++ ) if (L<S[i][j]) L=S[i][j]; C Programming 250 for ( i=0 ; i<3 ; i++ ) for ( j=0 ; j<3 ; j++ ) if (M>S[i][j]) M=S[i][j]; printf(“Largest Element = %d n ”, L ) ; printf(“Smallest Element = %d n ”, M ) ; int sum = 0 ; for ( i=0 ; i<3 ; i++ ) for ( j=0 ; j<3 ; j++ ) sum=sum + S[i][j]; float average =sum/9 ; printf(“Average = %d n ”, average ) ; int ST[3][3]; for ( i=0 ; i<3 ; i++ ) for ( j=0 ; j<3 ; j++ ) ST[i][j]=S[j][i]; return 0; } Higher Technological Institute
  • 251. Example 3 (a) (b) (c) # include <stdio.h> main ( ) { int A[3][3] , B[3][3] , S[3][3]; int i,j ; for ( i=0 ; i<3 ; i++ ) for ( j=0 ; j<3 ; j++ ) { printf(“Please Enter A[%d][%d] = n ”, i , j ) ; scanf(“%d”,A[i][j]); } for ( i=0 ; i<3 ; i++ ) for ( j=0 ; j<3 ; j++ ) { printf(“Please Enter B[%d][%d] = n ”, i , j ) ; scanf(“%d”, B[i][j]); } for ( i=0 ; i<3 ; i++ ) for ( j=0 ; j<3 ; j++ ) S[i][j]=A[i][j]+B[i][j]; int L=S[0][0], M=S[0][0]; for ( i=0 ; i<3 ; i++ ) for ( j=0 ; j<3 ; j++ ) if (L<S[i][j]) L=S[i][j]; C Programming 251 for ( i=0 ; i<3 ; i++ ) for ( j=0 ; j<3 ; j++ ) if (M>S[i][j]) M=S[i][j]; printf(“Largest Element = %d n ”, L ) ; printf(“Smallest Element = %d n ”, M ) ; int sum = 0 ; for ( i=0 ; i<3 ; i++ ) for ( j=0 ; j<3 ; j++ ) sum=sum + S[i][j]; float average =sum/9 ; printf(“Average = %d n ”, average ) ; int ST[3][3]; for ( i=0 ; i<3 ; i++ ) for ( j=0 ; j<3 ; j++ ) Do Not Forget to ST[i][j]=S[j][i]; add the display statements for ST return 0; } Higher Technological Institute
  • 252. Example 3 (a) (b) (c) # include <stdio.h> main ( ) { int A[3][3] , B[3][3] , S[3][3]; int i,j ; for ( i=0 ; i<3 ; i++ ) for ( j=0 ; j<3 ; j++ ) { printf(“Please Enter A[%d][%d] = n ”, i , j ) ; scanf(“%d”,A[i][j]); } for ( i=0 ; i<3 ; i++ ) for ( j=0 ; j<3 ; j++ ) { printf(“Please Enter B[%d][%d] = n ”, i , j ) ; scanf(“%d”, B[i][j]); } for ( i=0 ; i<3 ; i++ ) for ( j=0 ; j<3 ; j++ ) S[i][j]=A[i][j]+B[i][j]; int L=S[0][0], M=S[0][0]; for ( i=0 ; i<3 ; i++ ) for ( j=0 ; j<3 ; j++ ) if (L<S[i][j]) L=S[i][j]; C Programming 252 for ( i=0 ; i<3 ; i++ ) for ( j=0 ; j<3 ; j++ ) if (M>S[i][j]) M=S[i][j]; printf(“Largest Element = %d n ”, L ) ; printf(“Smallest Element = %d n ”, M ) ; int sum = 0 ; for ( i=0 ; i<3 ; i++ ) for ( j=0 ; j<3 ; j++ ) sum=sum + S[i][j]; float average =sum/9 ; printf(“Average = %d n ”, average ) ; int ST[3][3]; for ( i=0 ; i<3 ; i++ ) for ( j=0 ; j<3 ; j++ ) { printf (“n”); ST[i][j]=S[j][i]; printf (“%d”, ST[i][j]); } return 0; } Higher Technological Institute
  • 253. Matrix Multiplication Example 4 Write a program to calculate and display the Multiplication of any two 3x3 Matrices. C Programming 253 Higher Technological Institute
  • 254. Two Dimensional Arrays Remember = New Matrix Dimensions C Programming 254 Higher Technological Institute
  • 255. Example 4 # include <stdio.h> main ( ) { int A[4][3]={ 2 , 3 , 4 , 1, 2 ,1, 2 , 1 ,0, 1 , 0 , 2 }; int B[3][4]={ 10 , 0 , 20 , 10 , 0 , 20 , 0 , 20, 20 , 10 , 10 , 0 }; int M[4][4]; int ra = 4 , ca = 3 , rb = 3, cb = 4 ; int i , j , k ; for (i=0;i<ra;i++) for (j=0;j<cb;j++) { M[i][j]=0; for(k=0;k<ca;k++) M[i][j]= M[i][j]+ A[i][k]*B[k][j]; } C Programming 255 printf("n The M is n"); for(i=0;i<ra;i++) { printf("n"); for (j=0;j<cb;j++) printf("%4d",M[i][j]); } return 0 ; } Higher Technological Institute
  • 256. Home Work ..!! Write a program to calculate the determent of 3x3 Matrix. C Programming 256 Higher Technological Institute
  • 257. Course Contents 1 Introduction 2 Program Development 3 The Essential of C Programs 4 Manipulating Data with Operators 5 Reading from and Writing to Standard I/O 6 Decision 7 Iteration 8 Arrays 9 C Functions C Programming 257 Higher Technological Institute
  • 258. Course Contents 9 C Functions C Programming 258 Higher Technological Institute
  • 259. Functions Why Function o To make large programs Manageable, Programmers modularize them into subprograms. o These subprograms are called Functions C Programming 259 Higher Technological Institute
  • 260. Functions C Standard Functions OR Built in Function C Programming 260 Higher Technological Institute
  • 261. C Standard Functions  The C language has a standard library which is a collection of predefined functions and other program elements which are accessed through header files.  One of the most important header file <math.h> contains useful mathematical functions. Note Every mathematical function return double data type C Programming 261 Higher Technological Institute
  • 262. Some Mathematical in <math.h> Function Description cos(x) exp(x) log(x) log10(x) pow(x,p) sin(x) sqrt(x) tan(x) Cosine of x in Radian Exponential of x (base e) Natural Logarithm of x (base e) Common Logarithm of x (base e) x to the power p = xP Sine of x in Radian Square root of x = 𝒙 Tangent of x in Radian C Programming 262 Higher Technological Institute
  • 263. Example 1 Write a program to calculate the following: a) cos (x) b) e x c) x 3/2 d) 𝒙 Test your program with x = 8, 16, 32 C Programming 263 Higher Technological Institute
  • 264. Example 1 # include <stdio.h> # include <math.h> main ( ) { int x=8; double a,b,c,d; do { a = cos(x*180/(22/7)); b=exp(x); c=pow(x, 3/2); d=sqrt(x); printf(“At x = %d n A = %f n B= %f n C= %f n D=%f n ”,x,a,b,c,d); x=x*2; } while ( x<=32 ); return 0; } C Programming 264 Higher Technological Institute
  • 265. Example 1 # include <stdio.h> # include <math.h> main ( ) { int x=8; double a,b,c,d; do { a = cos(x*180/(22/7)); b=exp(x); c=pow(x, 3/2); d=sqrt(x); printf(“At x = %d n A = %f n B= %f n C= %f n D=%f n ”,x,a,b,c,d); x=x*2; } while ( x<=32 ); return 0; } C Programming 265 Higher Technological Institute
  • 266. Example 1 # include <stdio.h> # include <math.h> main ( ) { int x=8; double math b Function 1 Function 2 … … exponential Function … … ; b=exp(x); main x 8 b return 0; } C Programming 266 Higher Technological Institute
  • 267. Example 1 # include <stdio.h> # include <math.h> main ( ) { int x=8; double math b ; 8 Function 1 Function 2 … … exponential Function … … b=exp(x); main x 8 b return 0; } C Programming 267 Higher Technological Institute
  • 268. Example 1 # include <stdio.h> # include <math.h> main ( ) { int x=8; double math b ; 8 Function 1 Function 2 … … exponential Function … … b=exp(x); main x 8 b 8886110.520508 return 0; } C Programming 268 Higher Technological Institute
  • 269. Some Header Files in C Library Header Description File <ctype.h> Defines function to test characters <float.h> Defines constant to float Defines the integer limits on your local <limits.h> computer <math.h> <stdio.h> <stdlib.h> <string.h> <time.h> C Programming Defines mathematical function Defines functions for standard input and output Defines utility functions Defines functions for processing string Defines time and date functions 269 Higher Technological Institute
  • 270. User Defined Function You Have to Know o Function Type o Function Name o Arguments to the function Type Name Arguments void Func_1 with returnable n1 without C Programming 270 Higher Technological Institute
  • 271. Build Your Function Func_Type Func_name( type1 Arg1, type2 Arg2 ) { statment1 ; statment2 ; . . . Function’s Body return variable; } C Programming 271 Higher Technological Institute
  • 272. Where I can put my Function ?? # include <stdio.h> func_type Func_1( Argument1, Argument2 ) { statment1 ; statment2 ; … … return ; } main ( ) { … Func1 Call ; … return 0; } Function’s Body Main Function C Programming 272 Higher Technological Institute
  • 273. Where I can put my Function ?? OR Function Declaration # include <stdio.h> func_type main ( ) { … Func1 Call ; … return 0; } func_type { statment1 ; statment2 ; … … return ; } C Programming Func_1( typeArgument1, typeArgument2 ) ; Main Function Func_1( Argument1, Argument2 ) Function’s Body 273 Higher Technological Institute
  • 274. Where I can put my Function ?? Function Declaration # include <stdio.h> func_type main ( ) { … Func1 Call ; … return 0; } func_type { statment1 ; statment2 ; … … return ; } C Programming Func_1( typeArgument1, typeArgument2 ) ; Main Function Func_1( Argument1, Argument2 ) Please Do not forget ; after Declaration Statement Function’s Body 274 Higher Technological Institute
  • 275. Simple Example # include <stdio.h> int sum ( int x, int y) { z = x + y; return z ; } main ( ) { int a, b, c; a = 3; b = 5; c = sum( a , b ); printf("C=%d n", c); return 0; } C Programming 275 Higher Technological Institute
  • 276. Simple Example # include <stdio.h> int sum ( int x, int y) { z = x + y; return z ; } main ( ) { int a, b, c; a = 3; b = 5; c = sum( a , b ); printf("C=%d n", c); return 0; } C Programming 276 Higher Technological Institute
  • 277. Simple Example # include <stdio.h> int sum ( int x, int y) { z = x + y; return z ; } main ( ) { int a, b, c; a = 3; b = 5; c = sum( a , b ); printf("C=%d n", c); return 0; } C Programming 277 Higher Technological Institute
  • 278. Simple Example # include <stdio.h> int sum ( int x, int y) { z = x + y; return z ; } • Why we use x, y,z in Function’s Body main ( ) While use a,b,c in main Function …? { • Effect of use the same variable name. int a, b, c; a = 3; b = 5; c = sum( a , b ); printf("C=%d n", c); return 0; } C Programming 278 Higher Technological Institute
  • 279. Simple Example by Method 2 # include <stdio.h> int sum ( int, int ) ; main ( ) { int a, b, c; a = 3; b = 5; c = sum( a , b ); printf("C=%d n", c); return 0; } int sum ( int x, int y) { z = x + y; return z ; } C Programming 279 Higher Technological Institute
  • 280. Simple Example by Method 2 # include <stdio.h> int sum ( int, int ) ; main ( ) Again Do not { forget ; after Declaration Statement int a, b, c; a = 3; b = 5; c = sum( a , b ); printf("C=%d n", c); return 0; } int sum ( int x, int y) { z = x + y; return z ; } C Programming 280 Higher Technological Institute
  • 281. Why Functions Example Write a program to calculate the factorial of any integer number. Then use your program to calculate the value of Y. 𝒏! − 𝒌! 𝒀= 𝒏− 𝒌 ! C Programming 281 Higher Technological Institute
  • 282. Example # include <stdio.h> main ( ) { int n ; printf(“Please Enter The Value of n n"); scanf(“%d”,&n); int factorial =1; for ( int i=n ; i>=1 ; i-- ) factorial = factorial * i ; printf(“Factorial =%d n", factorial); return 0; } C Programming 282 Higher Technological Institute
  • 283. Example # include <stdio.h> int fact ( int ) ; main ( ) { int n, k, y; printf(“Please Enter The Value of n and k n"); scanf(“%d %d”,&n,&k); y = ( fact(n) * fact(k) ) / fact( n – k ) ; printf(“Y=%d n", y); return 0; } int fact ( int fc ) { int factorial =1; for ( int i=fc ; i>=1 ; i-- ) factorial = factorial * i ; return factorial ; } C Programming 283 Higher Technological Institute
  • 284. LOGO Higher Technological Institute 10th of Ramadan City 6th of October Branch Electrical and Computer Engineering Department Eng. Ibrahim Elewah HTI Student Book Main Reference “C For Dummies” by Dan Gookin 2nd Edition 28