Results 1 to 6 of 6
Thread: How to say if (odd)?
- 06-09-2008, 02:47 AM #1
Member
- Join Date
- Jun 2008
- Posts
- 20
- Rep Power
- 0
-
cross-posted in the Sun Java forums.
- 06-09-2008, 06:21 AM #3
not clear actually....
What's the purpose of adding up all the value from those elements if your question is to have a logical value that represents odd or not?
You may design a method that returns a logical value true/false with a parameter int value.....
Here is an example,
Then, use that for testing those elements if the value is an odd or not....Java Code:public static final boolean isOdd(int value){ return (value%2)>0; }
If satisfied, do the operation....
You may use simplified operator "+="Last edited by sukatoa; 06-09-2008 at 06:31 AM.
freedom exists in the world of ideas
- 06-09-2008, 12:42 PM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Hope you are looking something like this.
Java Code:public static void main(String[] args) { // TODO code application logic here int[] number = new int[8]; number[0] = 1; int sum = 0; // Filled the array with some numbers for(int i = 1; i < number.length; i++) { number[i] = number[i - 1] * 2; } // Process for(int j = 0; j < number.length; j++) { if(j%2 != 0) { sum += number[j]; } } System.out.println(sum); }
- 06-09-2008, 02:55 PM #5
Member
- Join Date
- Jun 2008
- Posts
- 20
- Rep Power
- 0
^ Yes, very simple. Thank you for the insight.
- 06-09-2008, 02:59 PM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Did you solve this question. If so please mark it as solved.


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks