School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,475 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,739 people online right now. Registration is fast and FREE... Join Now!




stuck on the last portion of figuring out this ruby code...

 

stuck on the last portion of figuring out this ruby code...

rubyxpearls

23 Feb, 2008 - 12:26 PM
Post #1

New D.I.C Head
*

Joined: 23 Feb, 2008
Posts: 1

here's my code so far. I've tried everything to get the last part to work out. Basically, I want my program to print only the numbers up to 3407 where the last digit is equal to the sum of the rest of the digits. so 3407 WOULD print, but not 3406...

CODE


100.upto(3407) do |i|
str=i.to_s
arr=str.split("")
print i
print "\t"
sum=0
sum= arr[arr.length-1]
print sum
print "\t"
sum2=0
sum3=0
sum2=arr.each{|element|
        sum3+=element.to_i
        }
print sum3
print"\t"
sum4=0
sum4=((sum3.to_i)-(arr[arr.length-1].to_i))
puts sum4
puts "\t"



I know that I need to use an if statement, and I've tried " if sum==sum4" but that wasn't working. any suggestions? thanks :D im a beginner so im still taking this all in

User is offlineProfile CardPM
+Quote Post


AlilSpaced

RE: Stuck On The Last Portion Of Figuring Out This Ruby Code...

3 Mar, 2008 - 10:43 AM
Post #2

D.I.C Head
**

Joined: 20 Dec, 2007
Posts: 71


My Contributions
If your issue is that you are getting an exception that looks something like:
QUOTE
projectname.rb:20: syntax error, unexpected $end, expecting kEND

Then it may have something to do with the following line of code.
ruby
arr=str.split("")

I do not think you can split a string without telling it where it needs to be split in any language. Try fixing that, it might just be the reason your code isn't working.

pirate.gif arrrrrgh!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 03:28AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month