You are on page 1of 21

Basic Data Types

(almost all P.Languages)

• int ---- integer – Whole number


• float ---- floating point – number with
decimal point
• double ---- for bigger or larger
numbers with decimal point
• char - character – single letter, symbol,
or number enclosed commonly by
single quotes
Arithmetic Operators

Symbols Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus(Remainder)
Hierarchy of Arithmetic Operator

MDAS RULE
FROM LEFT TO RIGHT
Assignment Operator

= (single equal sign)


---- use to assign value
to a variable
Relational Operators

Symbols Meaning
> Greater than
>= Greater than or Equal to
< Less than
<= Less than or Equal to
== Equal to
!= Not Equal to
Logical Operator

Symbols Meaning
&& and
|| or
! not
Logical Operator

Logical AND(&&) Logical OR(||)


A B Z = A&&B A B Z = A||B

0 0 0 0 0 0

0 1 0 0 1 1

1 0 0 1 0 1

1 1 1 1 1 1

Logical NOT(!)
A Z = !A

0 1

1 0
Programming Exercises

PROBLEMS
Problem no.1

• Create a flowchart that calculates the


sum of two input number and display
the result.
• Algorithm
Input Process Output
Enter two Compute the sum Display the sum
numbers (n1,n2) (sum = n1+n2) (sum)
start

int n1
int n2
int sum

Enter 1st no: _


Enter 2nd no: _

sum = n1 + n2

THE SUM is sum

stop
Problem no.2
(single sided)
• Draw a flowchart that determines if the
input number is positive. Consider zero
as positive.
• Algorithm
Enter a number (n)
if(n>=0)
Display that the number is POSITIVE
start

int n

Enter a number
n: _

yes
The number is
if(n>=0)
POSITIVE

no

stop
Problem no.3
(double sided)
• Draw a flowchart that determines if the
input number is positive or negative.
Consider zero as positive.
• Algorithm
Enter a number (n)
if(n>=0)
Display The number is POSITIVE
else
Display The number is NEGATIVE
start

int n

Enter a number
n: _

no yes
The number is The number is
if(n>=0)
NEGATIVE POSITIVE

stop
Problem no.4
(multiple alternative)
• Draw a flowchart that determines if the
input number is positive or negative or
neutral if the input number is zero.
• Algorithm
Enter a number (n)
if(n>0)
Display The number is POSITIVE
else if(n<0)
Display The number is NEGATIVE
else
Display The number is neutral
start

int n

Enter a number n:
_

yes The
if(n>0) number is
POSITIVE
no
The no yes The
number is if(n<0) number is
NEUTRAL NEGATIVE

stop
Problem no.5
(case control, similar with
multiple alternative)

• Draw a flowchart that displays an


equivalent color once an input letter
match its first letter. Consider the criteria
• Criteria
Letters Colors
B or b Blue
R or r Red
Y or y Yellow
Other letters Unknown
start

char l

Enter a letter l_

Case l
is
Case l is other letter

Case l is B or b Case l is R or r Case l is Y or y

Unknown
BLUE RED YELLOW
Color

stop
Problem no.6
(case control, similar with multiple alternative)

• Draw a flowchart that generates/displays the


given sequence of numbers.
Sequence nos.
1
2
3
4
5
start

int n = 1

true

n <=5 Display n n++

stop
start

int n = 1
a
true
n == 6

false
stop
Display n

n++

You might also like