Results 1 to 5 of 5
 10302013, 02:44 PM #1Member
 Join Date
 Oct 2013
 Posts
 13
 Rep Power
 0
Checking whether an integer value has a certain integer in it.
Say we have an array of integers 1 through 10000.
We also have 3 instance variables; sum, sumWith8s, sumWithout8s.
Sum is the sum of all the values 1 through 10000,
sumWith8s is the sum of all the values 1 through 10000, which have 8 in them such as 8, 68, 801, 8888, etc...
sumWithout8s is the sum of all the values that don't have 8s in them such as 1, 12, 242 etc...
Any algorithms for this ?
 10302013, 03:28 PM #2Member
 Join Date
 Mar 2012
 Location
 Sweden
 Posts
 88
 Rep Power
 0
Re: Checking whether an integer value has a certain integer in it.
One way you could do it is convert the int into a string and check if the string contains the number 8 (String.contains("8")). If it does contain 8 then add one to the sum8 variable otherwise add to the other one. Or when the loop is done take the sum of all numbers and subtract the sum of the numbers with 8's in them.
It might not be the fastest or best way but it does the trick. (With this reply I'm expecting you to know how to get the sum of all the integers between 1 and 10000 if you don't post a reply).
PS. I'm not a programming pro.
 10302013, 03:40 PM #3Member
 Join Date
 Oct 2013
 Posts
 13
 Rep Power
 0
Re: Checking whether an integer value has a certain integer in it.
yeah a simple for loop, thanks all i need to know
 10302013, 04:16 PM #4Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 4,631
 Rep Power
 7
Re: Checking whether an integer value has a certain integer in it.
Iterative modular arithmetic mod 10 comes to mind.
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 10302013, 04:25 PM #5
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,004
 Blog Entries
 7
 Rep Power
 23
Re: Checking whether an integer value has a certain integer in it.
Duh, you don't need a for loop for the first subproblem: the sum of the number 1+2+3+ ... +n == n*(n+1)/2; if you subtract the sum of all numbers with an 8 in it from this number you get the sum of all numbers wihout an 8 in it ... with a bit of trickery you don't need any for loops at all.
kind regards,
JosI have the stamina of a seal; I lie on the beach instead of running on it.
Similar Threads

User Inputting Integer, Returning Month Name That Goes With That Integer
By carlyspade in forum New To JavaReplies: 2Last Post: 06222013, 07:49 PM 
Checking if String is added when integer required
By wfsteadman in forum New To JavaReplies: 4Last Post: 01212013, 06:40 AM 
Getting the last integer in a for loop and checking if the new one is equal to that
By ocomobock in forum New To JavaReplies: 2Last Post: 09242012, 11:31 PM 
Integer Comparison, Outputting Largest Integer Not Working
By killingthemonkey in forum New To JavaReplies: 4Last Post: 10162011, 08:59 PM 
convert unsigned integer to signed integer in java?
By diskhub in forum New To JavaReplies: 6Last Post: 05172010, 12:50 AM
Bookmarks