# Thread: Storing an integer in an array

1. ## 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
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```
and my real code:
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));
}
}
}
}
}```
This is my most recent attempt, I made seperate methods for summing a number up.
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
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.```
from the output I received it doesn't work anything like this.
Last edited by sunde887; 01-05-2011 at 06:01 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,

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 ?
.
.
.```
and so on...

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.

Goldest

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•