| CS 199 | Willamette University | Spring, 2019 |
|
Programming in PHP, Databases with MySQL, and Web Applications |
||
Consider the loop:
for($i = 6; $i <= 12; $i++){
echo $i
}
Next, consdier this loop
for($i = 20; $i <= 10; $i--){
echo $i;
}
What does this for loop print out?
for($i = 1; $i <= 50; $i ++){
$i = $i*2;
echo $i;
}
Using a for loop, print out numbers 1 through 10:
A Table of Fahrenheit and Celsius Temperatures
Given a temperature in Fahrenheit, the equivalent temperature in Celsius is given by the formula:
Celsius = (Fahrenheit - 32) * 5.0 / 9.0
Write a program that shows the Celsius temperatures corresponding to Fahrenheit temperatures 50, 51, 52, ..., 70. For example, possible output might have the form:
Fahrenheit Celsius
50 10.0
51 10.6
52 11.1
53 11.7
54 12.2
55 12.8
56 13.3
57 13.9
... ...
Interest in a Savings Account
A simple savings account earns compound interest over time, based upon an annual interest rate (e.g., 1% or 9%) per year. The basic computations proceed as follows:
monthlyRate = annRate / 1200.0;
interest = monthlyRate * (old balance)
new balance = (old balance) + interest
For example, at an annual rate of 8%, the monthly rate is 0.00666667. With a starting balance of $100, the computations proceed as follows for the first year:
Starting Monthly Ending
Month Balance Interest Balance
0 ------ ---- 100.00
1 100.00 0.67 100.67
2 100.67 0.67 101.34
3 101.34 0.68 102.01
4 102.01 0.68 102.69
5 102.69 0.68 103.38
6 103.38 0.69 104.07
7 104.07 0.69 104.76
8 104.76 0.70 105.46
9 105.46 0.70 106.16
10 106.16 0.71 106.87
11 106.87 0.71 107.58
12 107.58 0.72 108.30
What is printed by the following loop?
$x = 4
while($x <= 10){
echo $x;
$x ++;
}
What does this while loop print out?
$x = 5;
while(x != 0){
echo $x;
$x--;
}
Write a while loop that prints out numbers from 13 to 26.
Write code utilizing the following outline:
Explain in words what this program does.
Write code with the following steps that uses while loop to count down from 10 to 0, printing each item on a new line. After the count down is complete, the code should print "done".
For the following questions, state whether you should use a for loop or a while loop. Then explain why you chose to use that loop.
Task: Loop through all odd numbers from 1 to 100.
Terminology: An integer $i is said to be divisible by the integer $d, if division of $i by $d yields no remainder. (In terms of PHP, $i % $d = 0 (recall % is PHP's symbol for finding a remainer.)
Prime numbers: A number $i is said to be prime, if $i is divisible only by 1 and itself. (That is, $i is not divisible by any integers 2, ..., $i-1.)
Task: Given an integer $i, determine whether or not it is prime.
|
created 24 January 2019 by Saniya Lakka revised 26-31 January 2019 |
|