You are on page 1of 21

C

COOM
MPPU
UTTE
ERR FFUUNNDDAAMMEEN
NTTA
ALLS
S
A
ANND
D PPRRO
OGGRRAAMMMMIINNG
G UUSSI
INNG
G
PPYYTTHHOONN
HIDLAO, GWYNETH
IDJIRAN, JERVIN
GANIH, AMAT
PABILONA, ROLIMAR
JOROLAN, JOHN MICHAEL
V
VAAR
RIIA
ABBL
LEE
I
INNI
ITTI
IAAL
LIIZZA
ATTI
IOON
N
Variable initializations are used if you want to set
an initial value to a variable.
VARIABLE INITIALIZATIOON

SYNTAX identifier = constant_value;

SEMANTICS identifier = indicates the name of the variable.

constant_value: any fixed value, that is, if the data type is an integer, the value could be any
number or if the data type is a character it could be any letters.
For this problem, you can declare a variable for the exchange rate (i.e. exchangeRate) and
assign 41 as its value. The equivalent program code for this is:
exchangeRate = 41
Using variable initialization, you can write this code in one line like this, exchangeRate
= 41
This format already initializes the value of the exchangeRate to 41 upon declaration of the
variable. In your formula or assignment statement, instead of using the actual value (i.e.
41), you can already use the new variable, that is, pesoAmount = dollarAmount *
exchangeRate;
If you have two (2) exchange rates: dollar to peso and euro to peso
rates. Assign a variable for each exchange rate. Let variables
dollarToPeso and euroToPeso represent dollar to peso and euro to peso
rates, respectively. Assuming that 1 USD = Php41 and 1 Euro = Php54. The
initializations for these variables can be written as:
dollarToPeso = 41
euroToPeso = 54
M
MOON
NEEY
Y C COONNVVEERRSSI
IOON N P
PRRO
OGGR
RAAM
M
C
COOD
DEE (( V
VEERRSSIIOON 2 ))
N 2
Let’s modify our Money Conversion program using variable initializations.
Line No. Money Conversion Algorithm Money Conversion Program Code

1 START exchangeRate=41

2 Output message “MONEY CONVERSION” print(“MONEY CONVERSION”)

3 Output message “enter amount in US Dollar $ : “ print(“enter amount in US Dollar $ : “)

4 Input dollarAmount dollarAmount=float(input())

5 Compute pesoAmount=dollarAmount*41 pesoAmount+dollarAmount * exchangeRate

Output Message “equivalent amount in Philippine peso


6 print(“equivalent amount in Philippine Peso (Php):”)
(Php): “

7 Output pesoAmount print(pesoAmount)

8 END
As you can see, the rest of the program is still the same. The difference is that a new variable
is created, an initialization is created, and the new variable is used in the formula. Variable
initializations are very useful when you want to set an initial value to the variable. Take note,
initial. It means that the value can still change inside your program.

P
PRRO
OGGR
RAAM
MMMI
INNG
G E
EXXE
ERRC
CIIS
SEES
S 6
6

Money Conversion (version 3): Review the program code for Money Conversion problem. Modify the
program (and algorithm) so that it will convert Dollar to Euro. Assuming that the following are
the given exchange rates:
1 Euro = Php 57
1 US Dollar = Php 49
The exchange rates must be declared as constants and with the initial value above. Other variables
must be declared and initialized to zero. See the sample output for this problem below:
Sample Program Output:

MONEY CONVERSION
Enter amount in US Dollar: 100
Equivalent amount in Euro : 85

ALGORITHM PROGRAM CODE


Not all data values are in whole numbers. Some data values are not even numbers, like your name,
gender, or course. These are data in a form of characters or texts.
Before choosing a data type, it is important to identify its use in the program. If the program
requires a number, determine if it is an integer or real numbers (i.e. decimal values).
The following are the five standard data types used in Python:
1.Numbers
2.String
3.List
4.Tuple
5.Dictionary
In Python, inputted values using the input() function are in String. Before it can be used as numeric
values, it must be converted into a numeric data type. This process is called type casting.
Type casting is a way to convert a variable from one data type to another data type.
For now, let us just considerdata types for number. For number data types, a variable can either be;
1.int (integers)
2.float (floating point numbers)
3.complex numbers
P
PRRO
OGGR
RAAM
MMMI
INNG
G E
EXXE
ERRC
CIIS
SEES
S 7
7

Write a program that will compute for the area of a circle given its radius. You can
refer to the previous algorithm for the formula. Note: Use the appropriate data type
for the variables in this problem.

PROGRAM CODE
P
PRRO
OGGR
RAAM
MMMI
INNG
G E
EXXE
ERRC
CIIS
SEES
S 8
8

Money Conversion (version 4): Review the Money Conversion (version 3) from Programming Exercises
6. Consider the new sets of exchangerates below:
1 Euro = Php 55
1 US Dollar = Php 41
Modify the program so that a more precise data is computed. The program must only accept whole
numbers as inputs.

PROGRAM CODE
A
ARRI
ITTH
HMME
ETTI
ICC E
EXXP
PRRE
ESSS
SIIO
ONNS
S A
ANND
D M
MOOR
REE A
ASSS
SIIG
GNNM
MEEN
NTT
S
STTA
ATTE
EMME
ENNT
TSS

In most arithmetic expressions, we do not only use a single operator but a combination of multiple
operators. Since we have the modulus operator, we cannot simply use the MDAS (Multiplication,
Division, Addition, and Subtraction) rule. In C++, we follow a certain rule in how to prioritize
the computations in a given expression. We call this the precedence rule.
Exponentation gets done first, followed by multiplication and division (including // and %), and
addition and subtraction comes last.
PRECEDENCE RULE: ( ) , **, * , / , //, % , + , -
In a formula, the expression inside the parenthesis is computed first. Then, the priority of the
restof operations will be basedon the precedence rule, that is, multiplication, division, modulus,
addition, and subtraction as the last priority.
For example,
x = 8 + (9-3) * 4 /2
Using the precedence rule,the computation for the above assignment statement will be in
the following order:
□□Parenthesis : x = 8 + 6 * 4 / 2
□□Division
Multiplication :
:
Addition :
x = 8 + 24 / 2
x = 8 + 12
x = 20
Hence, the value of x for this arithmetic expression is 20.
For our given sample formula in the activity, the numerator and denominator are computed first and
computed separately. In this case, you can use parenthesis to indicated that it should be
prioritized in the computation, that is,
x = (a * b + c ) / (d – e)
For the expression (a * b + c), a and b will be multiplied first and the product will be added to c.
Whatever the computed value is, it will be divided by the expression (d – e).
It is important that you know how to convert these arithmetic expression into Python expression. The
table below contains sample mathematical formula translated to a Python expression.
Since we deal with expressions and assignment statements, it is also important to note that
the Python language has special shorthand coding that simplifies assignment statements. The
table below shows the format of how to write shorthand assignment statements.
In writing your program, you might encounter computations that require the use and reuse of
variables. Assuming you want to count the number of times a user runs the code and you want to
assign this to variable count. You could create an assignment statement like this,
count= count + 1
Using the shorthand format, you can write it as, count += 1
The following are some other examples on the use of shorthand operations:
total = total - discount total -= discount
age = age % 50 age %= 50
total = total + (amount - discount) total += (amount- discount)
Depending on the problem at hand, you might need to change the value of a certain variable which is
dependent on its current value. You can practice and try to apply the shorthand statements in your
previous activities and exercises. When dealing with control statements, this type of statements
will really come in hand (see Flow of Control and Loops).
P
PRRO
OGGR
RAAM
MMMI
INNG
G E
EXXE
ERRC
CIIS
SEES
S 9
9

The area of a trapezoid is computed using the formula A = ½ h (b1 + b2) where h is the height, b1
and b2 are the bases. Write a program (with algorithm) that will compute for the area of a
trapezoid.

ALGORITHM PROGRAM CODE


P
PRRO
OGGR
RAAM
MMMI
INNG
G E
EXXE
ERRC
CIIS
SEES
S 1
100
A Fahrenheit temperature F can be converted to an equivalent Celsius temperature C
according to the following formula

ALGORITHM PROGRAM CODE


P
PRRO
OGGR
RAAM
MMMI
INNG
G E
EXXE
ERRC
CIIS
SEES
S 1
111

In a retail store, they offer an anniversary sale for their specially designed T-shirts (A, B,
and C). For every Php 1000 total purchase, you will get a 5% discount on Shirt A. Assume that
the prices for Shirts A, B, and C are Php50, Php75, and Php100, respectively.
Write a program (with an algorithm) that will accept the number of T-shirts purchased (or
quantity) for each kind and will display the total purchases, discounted amount, and the amount
to be paid (i.e. total purchase – discount).
SampleProgram Output 1 (with 5% discount): Sample ProgramOutput 2 (with 2 x 5% discount):
ALGORITHM PROGRAM CODE
T
THHA
ANNK
K Y
YOOU
U F
FOOR
R L
LIIS
STTE
ENNI
INNG
G !!

You might also like