1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
use strict; use warnings; print "please enter two odd numbers \n"; print "enter first odd number:"; my $odd1 = <STDIN>; chomp ($odd1); print "enter second odd number:"; my $odd2 = <STDIN>; chomp ($odd2); exit if (!($odd1 %2) or !($odd2 %2)); my @odds; my $step; if ($odd1 < $odd2) { $step= 2 ; } else { $step =-2; } my $n=0; for (my $i=$odd1; $i != $odd2;$i=$i+$step) { $odds[$n] = $i; $n=$n+1; } $odds[$n]=my $i; print "numbers:@odds\n";
Guest nanoCode (perl): (dl )for (my $i=$odd1; $i != $odd2;$i=$i+$step) {}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#!/usr/bin/perl use warnings; use strict; print "please enter two odd numbers \n"; print "enter first odd number: "; my $odd1 = <STDIN>; chomp ($odd1); print "enter second odd number: "; my $odd2 = <STDIN>; chomp ($odd2); if ($odd1 % 2 == 0 || $odd2 % 2 == 0) { exit; } my @odds; my $step = 2; if ($odd1 >= $odd2) { $step *= -1; } my $i; for ($i = $odd1; $i <= $odd2;$i += $step) { push(@odds, $i); } print "numbers:"; foreach $i (@odds) { print "$i "; } print "\n";
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#!/usr/bin/perl use v5.12; use warnings; say "please enter two odd numbers"; print "enter first odd number: "; chomp (my $odd1 = <STDIN>); print "enter second odd number: "; chomp (my $odd2 = <STDIN>); exit if $odd1 % 2 == 0 or $odd2 % 2 == 0; # exit unless $odd1 % 2 and $odd2 % 2; my $step = $odd1 < $odd2 ? 2 : -2; my (@odds); for (my $i = $odd1; $i <= $odd2; $i += $step) {push(@odds, $i)}; say "numbers: @odds";
1 2 3 4 5 6
... # Eingabe von odd1 und 2 ($odd1, $odd2) = ($odd2, $odd1) if $odd1 > $odd2; for (my $i = $odd1; $i <= $odd2; $i += 2) { ... }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#!/usr/bin/perl use warnings; use strict; print "please enter two odd numbers \n"; print "enter first odd number: "; my $odd1 = <STDIN>; chomp ($odd1); print "enter second odd number: "; my $odd2 = <STDIN>; chomp ($odd2); if ($odd1 % 2 == 0 || $odd2 % 2 == 0) { exit; } my @odds; my $i; my $step = 2; if ($odd1 <= $odd2) { for ($i = $odd1; $i <= $odd2;$i += $step) { push(@odds, $i); } } else { for ($i = $odd1; $i >= $odd2;$i -= $step) { push(@odds, $i); } } print "numbers: "; foreach $i (@odds) { print "$i "; } print "\n";
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#!/usr/bin/perl use v5.12; use warnings; say "please enter two odd numbers"; print "enter first odd number: "; chomp (my $odd1 = <STDIN>); print "enter second odd number: "; chomp (my $odd2 = <STDIN>); exit unless $odd1 % 2 and $odd2 % 2; ($odd1, $odd2) = ($odd2, $odd1) if $odd1 > $odd2; my (@odds); for (my $i = $odd1; $i <= $odd2; $i += 2) {push(@odds, $i)}; say "numbers: @odds";
1 2 3 4 5 6 7 8 9 10
use v6; say "please enter two odd numbers"; my $odd1 = prompt "enter first odd number: "; my $odd2 = prompt "enter second odd number: "; exit unless $odd1 % 2 and $odd2 % 2; my $min = min $odd1, $odd2; say "numbers: ", ($min, $min+2 ... (max $odd1, $odd2));
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include <stdio.h> int main() { int odd1; int odd2; int odds[10000]; int i; int arrp; int step; puts("please enter two odd numbers"); printf("enter first odd number: "); scanf("%d", &odd1); printf("enter second odd number: "); scanf("%d", &odd2); if (odd1 % 2 == 0 || odd2 % 2 == 0) { puts("One of the numbers is even."); return 1; } if (odd1 - odd2 > 9990 || odd2 - odd1 > 9990) { puts("One of the numbers is too big."); return 2; } arrp = 0; step = 2; if (odd1 <= odd2) { for (i = odd1; i <= odd2; i += step) { odds[arrp] = i; arrp++; } } else { for (i = odd2; i <= odd1; i += step) { odds[arrp] = i; arrp++; } } printf("numbers: "); for (i = 0; i < arrp; i++) { printf("%d ", odds[i]); } puts(""); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#!/usr/bin/perl use warnings; use strict; my @fruits = ("Apple", "Banana", "Tangerine"); # Das kann man auch so schreiben: @fruits = qw(Apple Banana Tangerine); my $i; # for-Schleife im C-Stil mit Zahlen: for ($i=1; $i <= 10; $i++) { print "$i\n"; } # Andere for-Schleife mit Zahlen: for $i (1 .. 10) { print "$i\n"; } # for(each)-Schleife durch die Elemente eines Arrays. # "for" und "foreach" sind synonym. foreach $i (@fruits) { print "$i\n"; } # Wenn man noch die jeweilige Elementnummer im Array braucht: # "$#fruits" steht für die Anzahl der Elemente von @fruits minus 1: for ($i=0; $i <= $#fruits; $i++) { print "$fruits[$i]\n"; }