You are on page 1of 49

Faculty of Computer Science and Information Technology

BIC 10204 Algorithm and Programming

CHAPTER 4 Part 1
Input and Output

Objectives
• After this lesson, you should be able to:
Understand and identify the usage of input
functions and operations using keyboard and
file
Understand and identify the usage of output
functions and operations (screen and into a
file)
Write C program using input and output
operations.

1
Sub topics in Chapter 4

File I/O
Introduction

Formatting Output
Chap 4 printf() function
Part 1

Other I/O functions


scanf() function
3

Introduction
• Input and output are two important
operations in computer programming.
• Data will be read as input to computer
and will be processed as output.
• Input and output (I/O) can be done either
through interactive or file batch
processing.

2
Input/Output

operation

input output
use use
scanf() function printf() function

Read data (input) Write out data (output)

include <stdio.h> include

printf() function

printf() purpose
Send data to output device regarding to a
given format

Write out data and display as output

format printf(“output_format”,print_list) ;

example
Output
printf(“First example“) ;

printf(“The bread price is RM %lf“,price) ; 6

3
printf() function (continued..)
format printf(“output_format”,print_list) ;

text
may
output_format
consist of placeholder ““

combination

may variable
print_list consist of constant
expression
function_name

printf() function (continued..)

Example 1:

printf(“Universiti Tun Hussein Onn Malaysia”);

Format output –text/string


Output:

Universiti Tun Hussein Onn Malaysia

4
printf() function (continued..)

Example 2:

printf(“%lf”, salary);

variable
Format output- placeholder

Output:

printf() function (continued..)

Example 3:

printf(“Monthly salary is RM %lf”, salary);

Output format -Text variable

placeholder
Output:
Monthly salary is RM
10

10

5
printf() function (continued..)

Example 4:

const float porosity 16.78;


printf(“The porosity of sand = %lf ”, porosity);

Output format -Text


constant
placeholder
Output:
The porosity of sand = 16.78000

11

printf() function (continued..)

Exercise
What are the output produced from the following statements?
1.printf(“Welcome to C programming world!>”);
2.printf(“Now “);
printf(“See “);
printf(“Heart “);
3.printf(“BIC 10204\n”);
printf(“Algorithm and Programming”);
12

6
printf() function (continued..)
escape Character
purpose
To tell the compiler additional formatting
to be performed with the printf() function

Character Explaination
\n New line
\t Tab
\a Beep sound

***Other escape characters are described in pg 56 (text book)


13

printf() function (continued..)

Placeholder
purpose
A symbol beginning with % in a format string that
indicates where to display the output value

format Using percent symbol (%) followed by character


which represent particular data type

Placeholder Output to be print


%d Integer number
%f/%lf Floating number(float/double)
%c Character
14
%s String

7
printf() function (continued..)

Example 5 (Printing character value ) :


char huruf1, huruf2;
huruf1 = ‘B’;
huruf2 = ‘P’;

printf(“%c %c %c”, ‘A’, ‘B’, ‘U’);


printf(“\nI live in %c%c”,huruf1,huruf2);

Output:
ABU
I live in BP

15

printf() function (continued..)


Example 6 (Printing integer value) :

int number1, number2;

printf(“My house number is %d”, 26);


number2=10;
printf(“\n\tNumber %d and %d”,number1,number2);
number2=2;
number1= 15;
printf(“Number %d and %d”,number2,number1);

Output:

My house number is 26
Number -858993460 and 10Number 2 and 15
16

8
printf() function (continued..)
Example 6 (Printing floating point number) :

double sahih1, sahih2;

printf(“The value of pi is %lf”, 3.142);


sahih1= 23.46;
printf(“\nThe value of sahih1 is %lf”,sahih1);
sahih2= 15.78;
printf(“\tThe value of sahih2 is %lf’,sahih2);

Output:
The value of pi is 3.142
The value of sahih1 is 23.46 The value of sahih2 is 15.78
17

printf() function (continued..)

Exercise
1. Given a = 2, b = 4. What are the output produced by the following
statements?
a) printf(“The additionresult of two number >> %d”,a + b);
b) printf(“\nThe addition of %d and %d “, a,b);
2. Write a valid C programming statement to display the following output
(use variables):
(Notes: Given the value of variable x = 2.5 and y = 3.1
The value of x is 2.5cm and y is 3.1cm
The mutiplication result of x and y is 7.75cm
18

9
scanf() function

scanf() function Read data with specified


format

Read input from keyboard

format scanf(“input_format”,&variable_list) ;

example scanf(“%d“,&nom) ;

scanf(“%s %d %lf”,&name,&age,&weight) ;
19

scanf() function (continued)

Consists of example
input_format placeholder %d @ %c @%lf

Called as ampersand
‘&’ symbol
Variable address/location reference

Must consist
variable_list variable

expression constant function

Not allowed!!!
20

10
scanf() function (continued)

Example1:

scanf(“%lf %d %c”,&float_num, &num,&letter);

Placeholder Variable

The above statement is valid!

Important Note!!!!!
Every variables to be used in the program MUST be declared
first!!!

21

scanf() function (continued)

Example 2:
scanf(“ %d ”, 15);

Placeholder Constant value

The above statement is INVALID!

scanf(“ %lf ”, &price + 0.05);

Conversion character Expression

The above statement is INVALID!


22

11
scanf() function (continued)

Example 3(program code):

#include <stdio.h> Any

?
void main() mistakes?
{ int semester;
float CPA;

printf(“Enter the semester : “);

?
scanf(“%d”, &semester);
printf(“\nEnter your CPA : “);
scanf(“%lf”,&CPA);
printf(“\nYour CPA for semester %d is %f”,semester,CPA);
}

? 23

scanf() function (continued)

Output:

Enter the semester : 3


Enter your CPA : 3.45
Your CPA for semester 3 is 3.45

24

12
scanf() function (continued)
Exercise
1).Write the scanf() statement to accept the following type of variables:
a. char jantina;
b. float berat;
c. int tempoh;
2).State whether the following scanf() statements is VALID or INVALID.
Given the following declaration:
int a, b;
char c;
a) scanf(“%d”, &a + b);
b) scanf(“%c”, &c);
c) scanf(“%d”,&a,&b);
25

Other I/O functions

There are other functions can be used in the input and


output operations in C programming such as:

getchar() and putchar() function


getch() and putch() function
gets() and puts() function

26

13
Other I/O functions (continued)

To read any input from input device


getchar() function as character data type

format variable= getchar();

example usage
char letter;
printf(“Enter one letter>>”);
letter= getchar();

27

Other I/O functions (continued)

To display output in a single character


putchar() function value

format putchar(variable);

example usage
printf(“The input letter is ”);
putchar(letter);

28

14
Other I/O functions (continued)

Example 1: getchar() and putchar()


#include <stdio.h>
void main()
{ char letter;
Enter one letter : e
The input letter is e
printf(“Enter one letter : “);
letter = getchar();
printf(“The input letter is\t“);
putchar(letter);
}

29

Other I/O functions (continued)

Example 2: getchar() and putchar()

However, we can also program like this :


#include <stdio.h>
void main()
{ int number; Enter one number : 123
The input number is 49
1
printf(“Enter one number : “);
number = getchar();
printf(“The input number is %d\n“,number);
putchar(number);
}

30

15
Other I/O functions (continued)

To read an input and terminate the


program automatically
getch() function

format variable= getch();

example char letter;


printf(“Enter one letter >>”);
letter= getch();

getch() have same function like getchar(), but different action


31

Other I/O functions (continued)

Example 3: getch()
We can use getch() to hold user screen, and terminate once
any input is read

#include <stdio.h>
void main()
{ int number;

printf(“Enter one number : “);


number = getchar();
printf(“The input number is %d\n“,number);
getch();
}

32

16
Other I/O functions (continued)

To display output , have similar function as


putch() function putchar

format putch (variable);

example printf(“The input letter is ”);


putch(letter);

33

Other I/O functions (continued)

To read string input from input device


gets() function

format gets(variable);

example
char name[20];
printf(“Enter your name >>”);
gets(name);

34

17
Other I/O functions (continued)

Example 4: gets()
If we use scanf() function to read string input, it will only takes
the first data between two words (if any)
#include <stdio.h>
void main() Enter your name: Siti Aminah
{ char name[20]; Your name is Siti

printf(“Enter your name : “);


scanf(“%s”,&name);
printf(“Your name is %s “,name);
getch();
}

35

Other I/O functions (continued)

Example 5: gets()
If we use gets() function to read string input, it will takes all the
data including space between two words (if any)
#include <stdio.h>
void main() Enter your name: Siti Aminah
{ char name[20]; Your name is Siti Aminah

printf(“Enter your name : “);


gets(name);
printf(“Your name is %s\n“,name);
getch();
}
36

18
Other I/O functions (continued)

puts() functions To display string output to output device

format puts (variable);

example
printf(“Your name is ”);
puts(name);

37

Formatting Output

formatting To format the display of an integer


Integer functions value

format %spaced
Where to use? Use the integer formatting in the
output statement

How it works? •Provide spaces according to formatting


•Put the value to display starting from right to left
side
•If there is – sign in the formatting, values will
be printed starting from left to right side

38

19
Formatting Output (continued)
Assume _ is
an empty
Formatting Integer space

example output
printf(“%3d”,123); 123

example output
printf(“%5d”,123); _ _123
example output
printf(“%10d”,123); _ _ _ _ _ _ _123
example output
printf(“%-6d”,123); 123_ _ _
example output
printf(“%-4d%4d”,123,456); 123_ _456
39

Formatting Output (continued)

Example 1: Formatting Integer


#include <stdio.h>

Output?
#include <conio.h>

void main()
{
printf("%d\n",123);
printf("%3d\n",123);
printf("%-3d\n",123);
printf("%5d\n",123);
printf("%-5d\n",123);
printf("%-10d\n",123);
printf("%10d\n",123);
printf("%4d\%-4d",123,456);
getch();
} 40

20
Formatting Output (continued)

Formatting To format the display of the floating


Floating functions number value
number
format %precisionf
Where to use?
Use in the output statement

How it works? •Provide spaces according to formatting


•Put the value to display starting from right to left
side
•If there is – sign in the formatting, values will
be printed starting from left to right side 41

Formatting Output (continued)

format
Formatting Floating number %precisionf

example output
printf(“%10f”,57.78); _57.780000

example
output
printf(“%15f”,57.78); _ _ _ _ _ _57.780000
example
output
printf(“%-10f”,57.78); 57.780000_
example output
printf(“%-15f”,57.78); 57.780000_ _ _ _ _ _

42

21
Formatting Output (continued)

format
Formatting character %precisionc

example char letter = ‘A’; output A


printf(“%c”,letter);
printf(“%2c”,letter);
_A
printf(“%-5c”,letter); A_ _ _ _

43

Formatting Output (continued)

format
Formatting String %precisions

char ayat[] =“UTHM PARIT RAJA”


printf(“%s”,ayat);
example printf(“%4s”,ayat);
printf(“%22s”,ayat);
printf(“%.7s”,ayat);
printf(“%-20.13s”,ayat);
output
UTHM PARIT RAJA
UTHM PARIT RAJA
_ _ _ _ _ _ _UTHM PARIT RAJA
UTHM PA
UTHM PARIT RA_ _ _ _ _ _ _
44

22
Formatting Output (continued)

Exercise
1. What is the output of this statement?
printf(“%8.5s”,”BIC 10204”)

2.State the output of these statements:


float h = 253.7654;
int g=6789;
printf(“%5d %5.1f ”,g,h);
printf(“%10d %10.3f ”,g,h);
printf(“%-7d %-7.5f ”,g,h);

45

READING DATA FROM A FILE


• Advantages
– Overcome lengthy data
– Executing a program many times
– For example:
• Monthly income for an employee is same every month and only her/
his expenses change
• It is cumbersome (etc for Financial department) to repeatedly key in
the same number for each month in order to print pay slips
• It is more convenient to set up a file that has employee income and
expenses
• Your computer program can read that file during execution instead
of receiving the input from the keyboard
• If the financial staffs want to rerun system with different input data,
they can simply edit the input file first and then execute the system

46

23
47

Extra steps for reading data from a


file
Declaring a pointer variable

Calling fopen() to allow


program to access the file

Calling fscanf() to
read data from a file

Closing the file pointed


by the pointer variable

48

24
Extra steps for reading data from a
file (continued)
• Declaring a pointer variable
– To use with fopen() and fscanf() functions
• Calling fopen() to allow program to access the
file
• Calling fscanf() to read data from a file
– fscanf(file_pointer,format_string,argument_list);
– File pointer is a variable whose memory cell contains an address
instead of an int, float or double value.
• The address gives the key to access the file stores on disk.
• It must begin the declaration with the word FILE and have an asterisk (*)
before the variable name

• Closing the file pointed by the pointer variable


(fclose()) 49

WRITING OUTPUT TO A FILE


• Advantages
– Writing your output to a file instead of to the
screen
– You can use a file editor to view the output
– You can use the editor to print the result on a
printer

50

25
51

Extra steps for writing data into a


file
Any acceptable file
name

Linked with a file


pointer before it is used

Open the file before it


is used (fopen())

Close the file after it is


used (fclose())
52

26
Reading data from a file and
Writing data into a file

53

54

27
Faculty of Computer Science and Information Technology

Chapter 4 Part 2
Expression 1
0011 0010 1010 1101 0001 0100 1011

&
Operator 1

O bjectives

1
0011 0010 1010 1101 0001 0100 1011

Students should be able to:


understand concepts and fundamentals
in expression/operator.
write expression in C programming
language

1
Introduction to Expression

1
0011 0010 1010 1101 0001 0100 1011

C
Arithmetic Expression

hapter 4 (Part 2)

Logical Expression
Relational Expression

33

Introduction to Expression

1
0011 0010 1010 1101 0001 0100 1011
Given the following statement :

2x + 3 – z = y

expression

2
Introduction to Expression

Process involves in money

1
0011 0010 1010 1101 0001 0100 1011

withdrawal scenario
balance – drew out money = current balance

Expression in C :

bakiTerkini =wangKeluar – bakiSemasa;

Introduction to Expression
What is Expression?

1
Combination of more than one variable
0011 0010 1010 1101 0001 0100 1011

Expression or constant (operand) which separated


by operator

example Operator
x+3 - z

Operand

Consists of arithmetic
relational
logical

3
Arithmetic Expression

1
0011 0010 1010 1101 0001 0100 1011Known as
Arithmetic Expression Mathematic Expression

using
Arithmetic Operator

Represents by
Represents by
Unary operator Binary operator

Operator Meaning Operator Meaning


- negative * multiply
+ positive / divide

-- decrement + add

++ increment - subtraction
% modulus
7

Arithmetic Expression

Unary Operator

1
0011 0010 1010 1101 0001 0100 1011

Unary Operator Operates for one operand


example Computer memory cell
a = -20; a -20
b = +15; b +15

Increment Decrement
prefix ++c; --c;
postfix c++; c--;
8

4
Arithmetic Expression

Unary Operator

1
Example 1:
0011 0010 1010 1101 0001 0100 1011

int A = 5;
++A;

printf(“%d”, A); output ?


A--;

printf(“%d”,A); output ?
A++;

printf(“%d”,A); output ?
9

Arithmetic Expression

Unary Operator

1
0011 0010 1010 1101 0001 0100 1011

Example 2:

int A ;
A=6;
printf(“%d”,3 + --A); output ?
printf(“%d”, A); output ?
A=6;
printf(“%d”, 3 + A--); output ?
printf(“%d”, A); output ?

10

5
Arithmetic Expression

Unary Operator

1
0011 0010 1010 1101 0001 0100 1011

Example 3:

Given the value of num1 = 8 .Determine the value of


num2 after the execution for each of the following
statements:
num2 = num1++ - 2;
num2 = num1;
num2 = ++num1 – 3;
num2 = num1-- +1;
11

Arithmetic Expression

Binary Operator

1
0011 0010 1010 1101 0001 0100 1011

Binary Operator Located between constants or


variables or both combination

example
A + z

operator
operand

12

6
Arithmetic Expression

Binary Operator

1
0011 0010 1010 1101 0001 0100 1011

Multiplication Use symbol “ * ”

example
A * z

operator
operand
Mathematic Arithmetic Expression

2x + y 2*x+y

13

Arithmetic Expression

Binary Operator

1
0011 0010 1010 1101 0001 0100 1011

Divide Use symbol “/”

example
A / z

operator
operand
Mathematic Arithmetic Expression

2÷y 2/y
14

7
Arithmetic Expression

Binary Operator

1
0011 0010 1010 1101 0001 0100 1011

Modulus Use symbol “%”

example
A % z

operator
operand
Return a balance when 2 numbers is divided

Can only be used with an integer variable


15

Arithmetic Expression

Binary Operator

1
0011 0010 1010 1101 0001 0100 1011

Example:
int A, B;
float C;
A= 2;
B=5;
C= 2.4;
B% A;
Valid! Answer is 1
C % A;
Invalid! C is float

16

8
Arithmetic Expression

Assignment Statement

1
0011 0010 1010 1101 0001 0100 1011
Used to store value/result of process to a variable
Use operator symbol =

Assignment statement

Double assignment Compound assignment


statement statement

17

Arithmetic Expression

Assignment Statement

1
0011 0010 1010 1101 0001 0100 1011

 Format / syntax :
variable = value;
variable = constant; or variable = variable;
variable = expression;

Example : average 44
1.average= ( 6 + 5) * 4;
1500
grossSalary
2.grossSalary = 1500;
nettSalary 1700
nettSalary = grossSalary + 200;
price 50.00
3.price= 50.00;
pay = price; pay 50.00
` 18

9
Arithmetic Expression

Compound Assignment Statement

1
0011 0010 1010 1101 0001 0100 1011

Use more than one operator (=)


Example :

int a = b= c = d = e = 250;
int b =2, number =0, total = 0,average =3;
number = b++ = 10;
int age = workHour = 0;

19

Arithmetic Expression

Compound Assignment Statement

1
0011 0010 1010 1101 0001 0100 1011

Function

To combine two different operator together.


To simplify arithmetic operator
Original function of operator does not affected
Allowed combination are as follow:

operator:

+= , -= , *= , /= , %=
20

10
Arithmetic Expression

Compound Assignment Statement

1
0011 0010 1010 1101 0001 0100 1011
Example :

Operator Expression Meaning


+= total + = 300 total = total+ 300
-= total - = count+ 300 total = total - (count + 300)
*= total *=300 total = total * 300
/= total /= count– 10 total = total / ( count –10)
%= total % = 7 total = total % 7

21

Arithmetic Expression

Arithmetic Operator Precedence Rules

1
0011 0010 1010 1101 0001 0100 1011

Compiler will follows the following precedence to execute the


arithmetic expression based on priority.

Operator Arrangement/Priority
() Left to right
++, -- Right to left
*, /, % Left to right
+, - Left to right

22

11
Arithmetic Operator

Arithmetic Operator Precedence Rules

1
0011 0010 1010 1101 0001 0100 1011

Example:
1. 5 + 2 * 6 – 4 / 2 2. 3 * 4 / 2 + ( 3 –1)

5 + 12 - 4 / 2 3*4/2+ 2

5 + 12 - 2 12 / 2 + 2

17 - 2 6 + 2

15 8 23

Arithmetic Expression

Arithmetic Operator Precedence Rules

1
0011 0010 1010 1101 0001 0100 1011

Example:
3. Prefix unary arithmetic expression

int kira = 5, nilai_pertama = 10;


nilai_kedua = 5 * --kira + nilai_pertama;
printf(“%d %d”, kira, nilai_kedua);

Output:

4 30
24

12
Arithmetic Expression

Arithmetic Operator Precedence Rules

1
0011 0010 1010 1101 0001 0100 1011

Example:
3. Prefix unary arithmetic expression

int kira = 5, nilai pertama = 10;


nilai_kedua = 5 * kira-- + nilai_pertama;
printf(“%d %d”, kira, nilai_kedua);

Output:

4 35
25

Arithmetic Expression

Mathematic Library Function

1
0011 0010 1010 1101 0001 0100 1011

•Header file designed for basic mathematical operations


(math.h)
•Syntax #include <math.h>
•Below are lists of some common math functions :
Fungsi Tujuan
sqrt(x) compute the non-negative square root of x
pow(x,y) computes x raised to the power y
cos(x) compute the cosine of x (measured in radians)
sin(x) computes the sine of x (measured in radians)
tan(x) compute the tangent of x (measured in radians)

26

13
Arithmetic Expression

Mathematic Library Function

1
0011 0010 1010 1101 0001 0100 1011

Example:

#include<stdio.h>
#include <math.h>
Output :
void main() 4
{
int x = 16, y ;
y = sqrt(x);
printf(“%d”,y);

}
27

Arithmetic Expression

Exercise:

1
0011 0010 1010 1101 0001 0100 1011

1. Convert the following mathematic expression to a valid


arithmetic expression :

a) b = 3 + b b) x = (a – b)(a – c2)
a+4

c) d = (3e – d) - ( 4 – 3c3 ) d) r = 2s + 3(s – 9)


x–9 4y s

2. Given a= 3, b = 5, c=1. What is the output of the following


expression?
a. ( 6 * c – 6 / a) - b b. (5 * c) +( a* b / b)

c. ++a d. c + a * c / (3 * c)

28

14
Arithmetic Expression

Exercise:

1
0011 0010 1010 1101 0001 0100 1011

Assume i,j and k are integer variables with i = 5 and j=3. Determine
the value for each of the following statement:
a) k = j++; d) k = ++j;
b) k = i * j--; e) k = i * --j;
c) k = j + i * j++; f) k = 27 / j++ - 16 % i;

29

Relational Expression

1
Relational
0011 0010 1010 1101 0001 0100 1011
use
Relational operator
expression

Combination of more than one statement

Can consists of variable vs variable

produce variable vs constant

constant vs constant

0 (if false) 1(if true)

30

15
Relational Expression

Relational Operator

1
0011 0010 1010 1101 0001 0100 1011

Operator Description

== Equal to

> Greater than


< Less than
>= Greater than or equal

<= Less than or equal


!= Not equal
31

Relational Expression

1
P/s:
Example 1:
0011 0010 1010 1101 0001 0100 1011
a, b and c are variables,
Replaced with the given values

int a=6, b =1, c = -2;

1) a+ b == c 2) a != b

6 + 1== -2 6 != 1
7 == -2

Answer: 0(False) Answer : 1 (True)

32

16
Relational Expression

0011 0010Example 2 :0001 0100 1011

1
1010 1101

int a=6, b =1, c = -2;

3) b<c 4) b + c <= a

1 < -2 1 + -2 <= 6
-1 <= 6

Answer: 0 (False) Answer : 1 (True)

33

Relational Expression
P/s:
Relational operator has less

1
0011 0010 1010 1101 0001 0100 1011
Example 3:
priority than other operators.
Start evaluating from left to
int a=10, b = 3, c = 7; right.
(a+b >= 3*c)==( a != 2*c+b)

(10+3 >= 3*7)==(a != 2*c+b)


(13 >= 21)==(10 != 14+3)
(13 >= 21)==(10 != 17)
0 == 1
0 (false)

34

17
Relational Expression

An example program which uses relational expression

1
0011 0010 1010 1101 0001 0100 1011

#include <stdio.h>
void main()
{ int age;

printf(“\nPlease enter your age >>”);


scanf(“%d”,&age);

if (age > 21) Relational expression

printf(“\nYou are qualified to vote”);

}
35

Logical Expression

Logical expression

1
0011 0010 1010 1101 0001 0100 1011 Logical Operator
use

Combination of one or more expressions

Can consists of
Relational expr. vs logical expr.

produce Relational expr. vs variable

Relational expr. vs constant

0 (if false) 1(if true)

36

18
Logical Expression

Logical Operator

1
0011 0010 1010 1101 0001 0100 1011

Operator Description

&& AND

|| OR

! NOT

Logical operator && dan || is used between 2 or more


relational expression

37

Logical Expression

1
Logical
0011 0010 1010 1101 0001 operator
0100 1011 truth table for AND

AND (&&) Logical Operator Result


False AND False False
Value 0 1
False AND True False
0 0 0
True AND False False
1 0 1
True AND True True

38

19
Logical Expression

1
Logical
0011 0010 1010 1101 0001 0100operator
1011 truth table for OR

OR (||) Logical Operator Result


False OR False False
Value 0 1
False OR True True
0 0 1
True OR False True
1 1 1
True OR True True

39

Logical Expression

1
Logical
0011 0010 1010 1101 0001 0100operator
1011 truth table for NOT

NOT(!)

Value Result Logical Operator Result


!0 1 Not false True

!1 0 Not true False

40

20
Logical Expression

1
Example
0011 0010 1010 1101 0001 0100 1011 1:

Evaluate the following logical expression:

a) (2 < 5 ) && ( 5 < 10) b) (7 % 2 < 2) || ( 2 * 3 == 6)

1 && 1 (1 < 2) || (6 == 6)

1 1 || 1

41

Logical Expression

Example 2:

1
0011 0010 1010 1101 0001 0100 1011

Evaluate the following logical expression:

Given a = 3, b = 4;

c) !((5 * b <= 23 - a )) d) ! ((b +3 != 8) &&( 3 * a < 2))

!((5 * 4 <= 23 – 3)) !(( 7 != 8 ) && ( 9 < 2 ))

!(20 <= 20) !( 1 && 0 )

!(1) ! ( 0)

0 1

42

21
Logical Expression

An example program which using Logical Expression:

1
0011 0010 1010 1101 0001 0100 1011
#include <stdio.h>
void main()
{ int mark;

printf(“\nEnter your mark >>”);


scanf(“%d”,&mark);

if (mark >= 85 && mark <= 100)


printf(“\nGred A”);

else if( mark >=80 && mark <=84)


printf(“\nGred A-”);
}

43

Logical Expression
Exercise:

1
0011 0010 1010 1101 0001 0100 1011
1. Given with i=2, j = 5 and k = 15. Evaluate each of the
following expression:
a) i > j – k g) k == j + i * j
b) i != k h) k <=k /j
c) (i >= 1) && (j == 5) i) (j < i) || (k > j)
d) !( i > j) j) (i >0) && (j <k) || (k <i)
e) i < j < k k) i * k < k / j
f) (i < j) && (j < k) l) i – j > k

2. Complete the following statements with suitable logical


expression.

int angka1,angka2;

if (angka1 is less than or equal to angka2)


printf(“%d is less than or equal to
%d”,angka1,angka2); 44

22

You might also like