Some practice problems for Quiz 2
what is the output ?
$sum=0;
$a=1;
while($a<10)
{
$b=2;
while( $b<7)
{
$sum=$sum+$a=$b;
print "A is $a B is $b
Sum is $sum\n";
$b=$b+2;
}
$a=$a+4;
}
print "A is $a B is $b Sum is $sum\n";
-----------------------------------------------------
what is the output ?
for( $a=10; $a>=1;$a--)
{
for($b=1;$b<=10;$b++)
{
if ( $b < $a )
{
print ' ';
}
else
{
print '*';
}
print '\n';
}
}
------------------------------------------------------
what is the output?
use strict;
my @data;
my $i;
$data[0]=3;
$data[1]=0;
$data[2]=4;
$data[3]=1;
$data[4]=2;
for($i = 0 ; $i < =$#data ; $i++)
{
print " $data[$i]
$data[$data[$i]] \n";
#no, this isn't a misprint
}
-----------------------------------------------------
Fibonacci Numbers!!!!
Fibonacci numbers are defined as follows:
- the first Fibonacci number is 1. That is, fib(1)=1
- the second Fibonacci number is 1. That is, fib(2)=1
- for any n greater than 1,
fib(n)=fib(n-1)+fib(n-2).
So fib(3)= fib(2)+fib(1) = 1 + 1 = 2
fib(4)=fib(3)+fib(2) = 2 + 1 = 3
fib(5)=fib(4)+fib(3) = 3 + 2 = 5
Easy program: use an array to find and print out the first 20 Fibonacci
numbers
Harder Program : without using an array, print out the first 20
Fibonacci numbers
----------------------------------------------------------
Simple practice problem:
- declare an array called @numbers
- fill up the first 20 slots of @numbers with the sequence
0,5,10,15,20,........
- print out this array in reverse
----------------------------------------------------------------------
Do you understand the perl last and next statements ?
----------------------------------------------------------------------
Write a complete perl loop that prompts (asks!!) the user to enter an
even number. If the number entered is not even,
a warning message is displayed and the user must again enter a number.
Once an even number is entered, the program
should respond "Thank You".