473,395 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

I just cant find what's wrong with this code

5
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:
Expand|Select|Wrap|Line Numbers
  1. #!usr/bin/perl
  2. #scrap.pl
  3. use warnings;
  4. use strict;
  5.  
  6. my $test=27;
  7. my @arr1;
  8.  
  9. for(my $i=0;$test<=1;$i++)
  10. {
  11.     print "entered loop !!! \n";
  12.     print $test,"\n";
  13.     $arr1[$i]=$test%2;
  14.     $test/=2;
  15.     print $test,"\n";
  16. }
  17. print @arr1;
  18.  
Thanks for the help.
Mar 14 '12 #1
3 1538
sabie
5
I think I've got it !!! a very small logical mistake!!
Mar 14 '12 #2
sabie
5
I've actually trying to convert decimal to binary and I did it now !!!

the code:
Expand|Select|Wrap|Line Numbers
  1. #!usr/bin/perl
  2. #scrap.pl
  3. use warnings;
  4. use strict;
  5.  
  6. my $test;
  7. THERE: print "Enter a decimal number less than 256: ";
  8. my $deca=<STDIN>;
  9.  
  10. if($deca>=256)
  11. {
  12.     print "number entered is greater than or equal to 256. please enter again!\n";
  13.     goto THERE;
  14. }
  15.  
  16. print "\n\n";
  17. my @arr1;
  18.  
  19. for(my $i=0;$deca>1;$i++)
  20. {
  21.     $arr1[$i]=$deca%2;
  22.     $deca/=2;
  23.     if($deca==1)
  24.     {
  25.         $i++;
  26.         $arr1[$i]=$deca;
  27.         last;
  28.     }
  29.  
  30. }
  31.  
  32. print "The binary equalent is: ";
  33. for(my $j=$#arr1;$j>=0;$j--)
  34. {
  35.     print $arr1[$j];
  36. }
  37. print "\n\n\n";
  38.  
Mar 14 '12 #3
numberwhun
3,509 Expert Mod 2GB
Glad you solved it. :)

Regards,

Jeff
Mar 15 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Nikolai Onken | last post by:
Hey, I got a rather more mathematical Problem but have no idea how to solve it: I have following values and want them to return other values as stated below: (Best seen with fixed pitch font)...
5
by: Derek Richards | last post by:
The following syntax is what I found under the Help in Excel's VBA Editor (you may refer to it for the details of each parameter). i specified to open in Write mode (Read Only is false), it...
11
by: Dart | last post by:
This code is okay: class CSingle { public: static CSingle membs; int MEM_NUM; CSingle(int n) { }
10
by: Sune | last post by:
Hi, previously I used Eclipse CDT for compiling my files just to get started with C and leave C++ behind. Now it's time to get a little more serious so I've moved my files to a new workplace and...
2
by: GaryDean | last post by:
I have inherrited an undocumented asp.net 1.1 application that contains a subproject with about 85 .vb modules that look like they are generated from some kind of data layering tool such as...
1
by: shapper | last post by:
Hi, I just created a predicate class following some articles there is an error. Could you please, tell me, what might be going on? 1 Public Class FindRowByName 2 3 Private...
1
by: rave3085 | last post by:
Actually this is a part of the code that keeps me wondering why? Dim rsExp As New ADODB.Recordset Dim rsDB As New ADODB.Connection Set rsExp = New ADODB.Recordset ...
1
by: emre esirik(hacettepe computer science and enginee | last post by:
void div ( int pol1 , int pol1p , int pol2 , int pol2p) { int r=5,z,i,g,j,k; for (i=0 ; i<6 ; i++) *(po5 + i)=pol1; for(i=5 ; i >=0 ; i--) { for(z=r ; z>=0 ; z--) {
4
by: hirsh.dan | last post by:
i have a functions that writes information to a file. inside that function i have a line in which i call another function. if this line is executed, nothing is written to the file, but if i remark...
1
by: Michelle Chase | last post by:
Option Compare Database 'this one is to use to concatenate the fields Function combine_descriptions() 'turn off warning dialog DoCmd.SetWarnings (False) Dim rst As Recordset Dim...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.