Welcome to Dream.In.Code
Getting Help is Easy!

Join 86,268 Programmers. There are 1,891 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

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

 
Reply to this topicStart new topic

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

rubyxpearls
post 23 Feb, 2008 - 01: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
Go to the top of the page
+Quote Post


AlilSpaced
post 3 Mar, 2008 - 11:43 AM
Post #2


D.I.C Head

**
Joined: 20 Dec, 2007
Posts: 58

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 online!Profile CardPM
Go to the top of the page
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 5/16/08 10:28AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month