I just cant find what's wrong with this code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sabie
    New Member
    • Mar 2012
    • 5

    I just cant find what's wrong with this code

    Hi,
    i am a beginner in Perl. All I want to do is a simple operation of saving the reminders of a continuous division of same number until the quotient becomes 1.

    I hope the code is logically correct but nothing is showing in the output.

    here is the code:
    Code:
    #!usr/bin/perl
    #scrap.pl
    use warnings;
    use strict;
    
    my $test=27;
    my @arr1;
    
    for(my $i=0;$test<=1;$i++)
    {
    	print "entered loop !!! \n";
    	print $test,"\n";
    	$arr1[$i]=$test%2;
    	$test/=2;
    	print $test,"\n";
    }
    print @arr1;
    Thanks for the help.
  • sabie
    New Member
    • Mar 2012
    • 5

    #2
    I think I've got it !!! a very small logical mistake!!

    Comment

    • sabie
      New Member
      • Mar 2012
      • 5

      #3
      I've actually trying to convert decimal to binary and I did it now !!!

      the code:
      Code:
      #!usr/bin/perl
      #scrap.pl
      use warnings;
      use strict;
      
      my $test;
      THERE: print "Enter a decimal number less than 256: ";
      my $deca=<STDIN>;
      
      if($deca>=256)
      {
      	print "number entered is greater than or equal to 256. please enter again!\n";
      	goto THERE;
      }
      
      print "\n\n";
      my @arr1;
      
      for(my $i=0;$deca>1;$i++)
      {
      	$arr1[$i]=$deca%2;
      	$deca/=2;
      	if($deca==1)
      	{
      		$i++;
      		$arr1[$i]=$deca;
      		last;
      	}
      	
      }
      
      print "The binary equalent is: ";
      for(my $j=$#arr1;$j>=0;$j--)
      {
      	print $arr1[$j];
      }
      print "\n\n\n";

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        Glad you solved it. :)

        Regards,

        Jeff

        Comment

        Working...