diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-01-08 12:38:41 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-01-08 12:38:41 +0000 |
| commit | 0a577a0a6db77addfd533648fa88e517a547689e (patch) | |
| tree | 5f544086d875c91c54462f7c5052241bc4a9c541 | |
| parent | fe9515ca889d150d3d18078dd5c0ca7164800383 (diff) | |
| parent | b781977c30ef8a560ccbb441d7d87811064b10c9 (diff) | |
| download | perlweeklychallenge-club-0a577a0a6db77addfd533648fa88e517a547689e.tar.gz perlweeklychallenge-club-0a577a0a6db77addfd533648fa88e517a547689e.tar.bz2 perlweeklychallenge-club-0a577a0a6db77addfd533648fa88e517a547689e.zip | |
Merge pull request #1123 from oWnOIzRi/week42
add solution week 42 task 1
| -rw-r--r-- | challenge-042/steven-wilson/perl/ch-1.pl | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/challenge-042/steven-wilson/perl/ch-1.pl b/challenge-042/steven-wilson/perl/ch-1.pl new file mode 100644 index 0000000000..31e9ba19d2 --- /dev/null +++ b/challenge-042/steven-wilson/perl/ch-1.pl @@ -0,0 +1,41 @@ +#!/usr/bin/env perl +# Author: Steven Wilson +# Date: 2020-01-08 +# Week: 042 +# Task #1 +# Octal Number System +# Write a script to print decimal number 0 to 50 in Octal Number System. + +# For example: + +# Decimal 0 = Octal 0 +# Decimal 1 = Octal 1 +# Decimal 2 = Octal 2 +# Decimal 3 = Octal 3 +# Decimal 4 = Octal 4 +# Decimal 5 = Octal 5 +# Decimal 6 = Octal 6 +# Decimal 7 = Octal 7 +# Decimal 8 = Octal 10 + +use strict; +use warnings; +use feature qw/ say /; +use Test::More tests => 1; + +ok( decimal_to_octal(1792) == 3400, "test decimal 1792 is octal 3400" ); + +sub decimal_to_octal { + my $dec = shift; + my $result; + while ( $dec > 7 ) { + $result .= $dec % 8; + $dec = int( $dec / 8 ); + } + $result .= $dec; + return scalar reverse $result; +} + +for ( 0 .. 50 ) { + say "Decimal $_ = Octal ", decimal_to_octal($_); +} |
