Results 1 to 2 of 2
Thread: Storing an integer in an array
- 01-05-2011, 03:25 AM #1
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Storing an integer in an array
I found a way to do it without an array. I am doing something about vampire numbers.
http://en.wikipedia.org/wiki/Vampire_number
my pseudo code is this
and my real code:Java Code:1. loop i from 10-99 2. convert i to a sum by adding (i % 10) + (i / 10) and store in a variable names isum 3. loop j from 10-99 4. convert j same as step 2, store it in jsum 5. create ijsum which is isum + jsum 6. store product of i and j 7. calculate the numbers in product out 8. if(psum = ijsum) print out the numbers since its a vampire number
This is my most recent attempt, I made seperate methods for summing a number up.Java Code:import static net.mindview.util.Print.*; import java.util.*; public class Vampire { static int fourDigitSum(int x) { int tmp = 0, tmp1 = 0, sum = 0; /*if(x % 1000 >= 10) { return 0; }*/ tmp1 = x / 1000; //5436 / 1000 = 5 sum += tmp1; tmp = x % 1000;//5436 % 1000 = 436 tmp1 = tmp / 100; // 436 / 100 = 4 sum += tmp1; //4 + rest tmp = x % 100;//5436 % 100 = 36 tmp1 = tmp / 10; // 36 / 10 = 3 sum += tmp1; // 3 + rest tmp = x % 10; // 5436 % 10 = 6 sum += tmp; // 6 = 6 return sum; } static int twoDigitSum(int x) { int sum = 0; sum = (x % 10) + (x / 10); return sum; } public static void main(String[] args) { int product = 0; for(int i = 10; i <= 99; i++) { for(int j = 10; j <= 99; j++) { //System.out.println(fourDigitSum(i * j)); if(fourDigitSum(i * j) == (twoDigitSum(i) + twoDigitSum(j)) &&(i % 10) == ((i * j) / 1000) || (i % 10) == (((i * j) % 1000) / 100) ||(i % 10) == (((i * j) % 100) / 10) || (i % 10) == ((i * j) % 10) &&(i / 10) == ((i * j) / 1000) || (i % 10) == (((i * j) % 1000) / 100) ||(i / 10) == (((i * j) % 100) / 10) || (i % 10) == ((i * j) % 10) &&(j % 10) == ((i * j) / 1000) || (i % 10) == (((i * j) % 1000) / 100) ||(j % 10) == (((i * j) % 100) / 10) || (i % 10) == ((i * j) % 10) &&(j / 10) == ((i * j) / 1000) || (i % 10) == (((i * j) % 1000) / 100) ||(j / 10) == (((i * j) % 100) / 10) || (i % 10) == ((i * j) % 10)) { //product = i * j; System.out.println(i + " * " + j + " = " + (i * j)); } } } } }
I added in the if clause so it is more specific, but it let to more inputs.
What Im trying to code with the somewhat confusing if clause is to check and make sure all the numbers in i and j are contained in i * j
It doesn't quite work like that though and Im really not sure why.
this is an example of how I think the if cluase should work
from the output I received it doesn't work anything like this.Java Code:i = 21, j = 60; product = i * j;(2160) 1. calculate the sums of i, j and product isum = 3, jsum = 6 psum = 9 compare them isum + jsum == psum is true. 2. i % 10 = 1, check if 1 is contained in (i * j) 3. i / 10 = 2, check if 2 is contained in (i * j) 4. j % 10 = 0, check if 0 is contained in (i * j) 5. j / 10 = 6, check if 6 is contained in (i * j) if all criteria returns true, print i * j = product.
Last edited by sunde887; 01-05-2011 at 05:01 AM.
- 01-05-2011, 05:16 AM #2
I just placed some print statements in you code in the if/else condition.
Like following,
Java Code:. . . . .for(int i = 10; i <= 99; i++) { //convert i to a sum isum = (i % 10) + (i / 10); for(int j = 10; j <= 99; j++) { System.out.println("Is "+((j * i) % 1000)+" > 10 ?"); if((j * i) % 1000 > 10) { System.out.println("In IF, before breaking"); break; } else { System.out.println("Inside else"); . . . .
When I ran it, its always getting break as your if condition is always returning true.
The output was like,
and so on...Java Code:Is 100 > 10 ? In IF, before breaking Is 110 > 10 ? In IF, before breaking Is 120 > 10 ? In IF, before breaking Is 130 > 10 ? In IF, before breaking Is 140 > 10 ? In IF, before breaking Is 150 > 10 ? In IF, before breaking Is 160 > 10 ? In IF, before breaking Is 170 > 10 ? In IF, before breaking Is 180 > 10 ? In IF, before breaking Is 190 > 10 ? In IF, before breaking Is 200 > 10 ? In IF, before breaking Is 210 > 10 ? In IF, before breaking Is 220 > 10 ? In IF, before breaking Is 230 > 10 ? . . .
Which means it never enters the else condition [and your final if statement] until it terminates.
Hope you know now where to make the change.
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
Similar Threads
-
Storing a record set into an array
By DJCali in forum New To JavaReplies: 2Last Post: 10-25-2009, 02:47 PM -
Storing in an Array
By Bascotie in forum New To JavaReplies: 10Last Post: 10-15-2009, 05:12 AM -
storing a string in an array
By tiyani in forum New To JavaReplies: 3Last Post: 08-12-2009, 07:25 PM -
storing strings into an array
By anthonym2121 in forum New To JavaReplies: 2Last Post: 04-04-2009, 07:32 AM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks