Login

Join for Free!
112224 members
table of contents table of contents

Biology Articles » Bioinformatics » Introductory Workbook on Perl for Biology Students » 1. Perl program for calculating the arithmetic averages

1. Perl program for calculating the arithmetic averages
- Introductory Workbook on Perl for Biology Students

The objective of the program is to calculate an average for any two given numbers. Here, to begin with store the desired numbers for which averages are to be determined in scalar variables ($a, $b, $c, etc). Add any two numbers using the scalar variables and divide them by 2. The result of the calculation is stored in another scalar variable ($avg1, $avg2, $avg3, etc). Once the results are obtained and stored, we need to print the result on the screen. For this purpose we have a command called “print”.  The print command also includes some special commands such as “\n”, “\t” for new line and tab respectively. These commands are optional.

 

  1. #calculate averages for the following pairs:
  2. $a=12;
  3. $b=13;
  4. $c=14;
  5. $d=15;
  6. $e=16;
  7. $f=17;
  8. $avg1 = ($a+$b)/2;
  9. $avg2 = ($c+$d)/2;
  10. $avg3 = ($e+$f)/2;
  11. $avg4 = ($b+$c)/2;
  12. print "$avg1\n$avg2\n$avg3\n$avg4\n"

 

 

 

RESULTS:

 

12.5

14.5

16.5

13.5

rating: 3.72 from 111 votes | updated on: 30 Jan 2009 | views: 90534 |

Rate article:







excellent!bad…