CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
help me urgent

 



KK
New User

Oct 11, 2013, 5:29 AM

Post #1 of 8 (809 views)
help me urgent Can't Post

i have written a program for fibonaci series. I would like to the stop the fibonaci series if it come greather than 10:.

if i give input as 10. the fibonaci values should stop at 10.

Please find my code below & help me asap.

$i = 0;
$j = 1;
print "enter the fin series";
$n = <>;
print "fin series input given are $n";
for ($c=0; $c<=$n; $c++)
{
if ( $c <=0 )
{
print "fibonaci series are $i\n";
}
else
{
$k = $i + $j;
$i = $j;
$j = $k;
print "fibonaci series are $k\n";
}
}


BillKSmith
Veteran

Oct 11, 2013, 9:04 AM

Post #2 of 8 (798 views)
Re: [keerthi2013] help me urgent [In reply to] Can't Post

Two issues need clarification.

Your text implies that you wish to print the series until a single value exceeds 10. Your code attempts to print 10 terms. Which do you intend?

There is no universal agreement on what is the first term of this series. It is important to state the exact definition of the series that you wish to implement.


Your code is not bad if you intend to print ten terms. It prints eleven, the first of which is wrong! If you intend to print ten terms starting with '2', all you must do is remove the the statement which prints $i. Of course, this allows you to simplify the logic.


I have several comments on your perl style. Because this is clearly a homework assignment, I cannot show you the improved code.

  • Always use strict and warnings.


  • do not use C-style for loops. Use:

    Code
    for my $c (0..$n){



  • Always remove the newline from numbers entered by user.

  • Good Luck,
    Bill


    KK
    New User

    Oct 14, 2013, 9:49 PM

    Post #3 of 8 (763 views)
    Re: [BillKSmith] help me urgent [In reply to] Can't Post

    Hi Bill,

    Thanks for your quick help & reply.

    Yes i would like to print the series until a single value digit below 10.

    could you please let me know which line i need to modify as per your below comment in my code.

    ==> Always remove the newline from numbers entered by user

    I will make sure warning are used & will not use the C style loops in the code.


    BillKSmith
    Veteran

    Oct 15, 2013, 4:01 AM

    Post #4 of 8 (754 views)
    Re: [KK] help me urgent [In reply to] Can't Post

    change

    Code
    $n = <>;  
    print "fin series input given are $n";

    To

    Code
    $n = <>; 
    chomp $n;
    print "fin series input given are $n\n";


    I doubt that any one or two line change will fix this program to meet the correct requirement.

    Not only is it important to stop printing in the right place, you must start in the right place. This requires the definition that I asked for before.
    Good Luck,
    Bill


    KK
    New User

    Oct 15, 2013, 5:26 AM

    Post #5 of 8 (748 views)
    Re: [BillKSmith] help me urgent [In reply to] Can't Post

    please find my output as below. The requirement is if i give input as 10. The fibonaci series nmber should be displayed less than 10.


    enter the fin series10
    fin series input given are 10
    fibonaci series are 2
    fibonaci series are 4
    fibonaci series are 7
    fibonaci series are 11
    fibonaci series are 16
    fibonaci series are 22
    fibonaci series are 29
    fibonaci series are 37
    fibonaci series are 46
    fibonaci series are 56


    BillKSmith
    Veteran

    Oct 15, 2013, 6:20 AM

    Post #6 of 8 (743 views)
    Re: [KK] help me urgent [In reply to] Can't Post

    You are printing ten numbers. That is what for loops do. You want to loop until you meet a condition. Use a while or until loop
    Good Luck,
    Bill


    KK
    New User

    Oct 15, 2013, 7:28 AM

    Post #7 of 8 (739 views)
    Re: [BillKSmith] help me urgent [In reply to] Can't Post

    thanks for your quick help bill. You're guidance helped me to tweak the code as per logic.

    Please find my code below & sorry for asking basic questions. I knew it was very basic. Just started practicing programming language. Thank you once again.



    use warnings;
    use strict;
    my $i = 0;
    my $j = 1;
    my $k = 0;
    print "enter the fin series";
    my $n = <>;
    chomp $n;
    print "fin series input given are $n\n";
    while($n > $j)
    {
    $k = $i + $j;
    $i = $j;
    $j = $k;
    if ($n > $j)
    {
    print "fibonaci series are $j\n";
    }
    }


    BillKSmith
    Veteran

    Oct 15, 2013, 10:04 AM

    Post #8 of 8 (731 views)
    Re: [KK] help me urgent [In reply to] Can't Post

    Good job using only basic Perl. You should consider renaming variables with longer, more meaningful names.

    Are you sure that you want your series to start (1,2,3,...).
    I would expect (1,1,2,3,...). Again, this is the question of definition that you never answered.

    Check spelling of all words that you print.
    Good Luck,
    Bill

     
     


    Search for (options) Powered by Gossamer Forum v.1.2.0

    Web Applications & Managed Hosting Powered by Gossamer Threads
    Visit our Mailing List Archives