Counting even and odd values
Is there another way to count even and odd number than this one
private static int countEvenOrOdd(int[] array, boolean countEvenNumbers){
int count = 0;
int remainderRequired = (countEvenNumbers ? 0 : 1);
for(int i = 0; i < array.length; ++i){
if(array[i] % 2 == remainderRequired){
++count;
return count;
}
> I like hacking ;-) Have a look at this: ...
Jos, you forgot the method for counting both:int countOddsAndEvens(int[] array) {
return (countOdds(array) + countEvens(array));
} ; )Yep, you're right, and for completeness reason I'd like to add the following:int countOthers(int[] array) {
return array.length-countOddsAndEvens(array);
}And for those junit maniacs:boolean testCounters(int[] array) {
return countOddsAndEvens(array)+countOthers(array) == array.length;
}btw, the moment I posted my previous reply I realized that I could've done better:int countOdds(int[] array) {
int c= 0;
for (int i= 0; i < array.length; c+= array[i++]&1);
return c;
}kind regards,
Jos ( <-- never has to maintain others' code ;-)
Similar Messages
-
How to work even and odd number query
Plz explain how below worked even and odd query
2 ) why used subquery after from and which time we can use(what time of out put)
even
select * FROM (SELECT ROW_NUMBER() OVER(ORDER BY stud_id) as row ,grno,stud_id from stud_Enrollment) d
WHERE d.row %2=0
odd
select * FROM (SELECT ROW_NUMBER() OVER(ORDER BY stud_id) as row ,grno,stud_id from stud_Enrollment) d
WHERE d.row %2=1Row_Number function returns the sequential number of a row
(By using partition of a result set, starting at 1 for the first row in each partition)
>> why used subquery after from and which time we can use(what time of out put)
When we need sequntial numbers, we can use row_number function. Put it into derived table and use this derived table after FROM clause (Same way it is used in your query)
CREATE TABLE stud_Enrollment (
grno int,
stud_id int,
stud_name varchar(20)
INSERT INTO stud_Enrollment
VALUES (101, 511, 'Dheeraj'), (112, 521, 'Vaibhav'), (132, 522, 'Lalit'), (124, 564, 'Amogh'), (143, 598, 'Sushrut')
SELECT * FROM stud_Enrollment
-- Result of your table
--grno stud_id stud_name
--101 511 Dheeraj
--112 521 Vaibhav
--132 522 Lalit
--124 564 Amogh
--143 598 Sushrut
-- Now we need to find out the rows which are at even position, ie row should be at position of 0,2,4,6,8 etc..
-- But we don't have sequential number here in your table result
-- So we can create one new column by using row_number function
SELECT
ROW_NUMBER() OVER (ORDER BY stud_id) AS row, --> additiona "row" column
grno,
stud_id,
stud_name
FROM stud_Enrollment
-- We got "row" column in below output
--row grno stud_id stud_name
--1 101 511 Dheeraj
--2 112 521 Vaibhav
--3 132 522 Lalit
--4 124 564 Amogh
--5 143 598 Sushrut
-- Now above table is used after FROM clause. It uses row column in WHERE part
SELECT
FROM (SELECT
ROW_NUMBER() OVER (ORDER BY stud_id) AS row,
grno,
stud_id,
stud_name
FROM stud_Enrollment) d
WHERE d.row % 2 = 0
--row grno stud_id stud_name
--2 112 521 Vaibhav
--4 124 564 Amogh
-Vaibhav Chaudhari -
I have a programming assignment that needs to read a set of integers and then finds and prints the sum of the evens and sum of the odds.
I know how to find out which numbers are odd and even, I just can't figure out how to add up the evens and odds. Say a set of 10 integers is inputted, how can they be added up after it is determined if they are even or odd?Here is my code after the tip from student... It compiles but once ran it doesn't do anything after the integers are put in.
import java.util.*;
public class EvenOddIntegers {
static Scanner console = new Scanner(System.in);
static final int limit = 10;
public static void main(String[] args) {
int number = 0, limit = 0, sumEven = 0, sumOdd = 0;
int odds = 0;
int evens = 0;
System.out.print("Enter ten positive integers: ");
while (limit <= 10)
number = console.nextInt();
if (number % 2 == 0)
sumEven += number;
else if (number % 2 != 0)
sumOdd += number;
System.out.println("Sum of even numbers is " + sumEven + ".");
System.out.println("Sum of odd numbers is " + sumOdd + ".");
} -
Zero bleed at inside page on even and odd master pages
Hello guys! Going directly to my question would be how to apply bleed option specifically on inside pages when working with individual master pages for master and facing pages on the main document. The thing is when I've set no bleed on inside on my Document Setup it has applied on my even and odd master pages (remembering I have individual master pages set on my document). I am not sure if I was clear enough but I keep it open for eventual comments and so on. Thank you.
Ok Peter. Thank you! I am posting some screen shots to help understanding.
-
How to display even and odd number ranges between two given inputs.
Hi Every one,
I am just started my career in sap abap and trying to find the solution for this.
I am trying to display number range using select options, but I am getting an error "Memory low leave the transaction before taking a break".
Example: when I give two number 2 and 10 , it should display set of all even and odd numbers in that range.
Below is the code logic that I am using:
data: a type i,
b type i,
c type i,
d type i,
num type i.
select-options: in for num.
a = inp-low.
b=inp-high.
c = inp-low mod 2.
d = inp-high mod 2.
while a <=b and c = 0.
write: "even numbers:', a.
b = a + 1.
endwhile.
while a <=b and c <> 0.
write: "odd numbers:', b.
b = a + 1.
endwhile.
Any help will be much appreciated.This is your logic...changed:
data: a type i,
b type i,
c type i,
d type i,
num type i.
data: even type i,
odd type i.
select-options: in for num.
a = in-low.
b = in-high.
c = a mod 2.
if c is INITIAL. "It measn a is even
even = a.
odd = a + 1.
else.
odd = a.
even = a + 1.
endif.
* Even
a = even.
while a <= b.
write: / 'even numbers:', a.
* b = a + 1.
a = a + 2. "The next even number
endwhile.
* Odd
a = odd.
while a <= b .
write: / 'odd numbers:', a.
a = a + 2. "The next odd number
endwhile. -
Detecting even and odd numbers
Hi, I'm drawing a series of rectangles using AS3, and need
every alternate one to be lighter... I'm using a switch statement
currently, with a variable counting the number of rectangles and
case 1, case 2, case 3, etc.
Is there a formula or some way of telling whether there is an
even or odd number of rectangles so my switch statement can just be
case even:, case odd: ? The way I have it is fine for 10 or so
rectangles but I'd rather make it more expandable.
Would appreciate any suggestions% is a modulo operator that finds a remainder of a division.
9%2 = 1, 9%4 = 1, but 9%3 = 0.
In the case of odd/even numbers one can look at it as that an
even number that leaves no remainder when divided by 2 and odd
number is a number that leaves remaining 1 when divided by
2. -
Split and add Even and odd numbers
Hi All,
I got a seven digit number any number say for example (6581231). Now i need to add even number and odd number separately.
Output should be
6 + 8 + 2 = 16
5 + 1 + 3 + 1 = 10
Thanks
SAssuming the number is always 7 digits one way to do this would be:
Data: string(7) type c,
num type i.
Nmod type i.
**move the number to a string.
Move num to string.
split into 7 individual chars.
Num1 = string+1
Num2 = string+2.
Num3 = string+3
Etc
divide each by 2 and find remainder
Nmod = num mod 2.
if remainder is 0, number is even
If nmod = 0.
Number is even.
Else.
Number is odd.
Endif. -
On the Catalyst 5000 architecture, all Even VLANs are running on one BUS on the backplane and all the odd VLANs are running on the other BUS on the backplane. Is CSS work in the same way? Hence, is it going to be a problem if I have all Even VLANs or all Odd VLANs?
Thanks,
/davidDavid,
The CSS does not work in this manner so your vlans can be odd or even and have not effect on the css. The only thing to keep in mind is that ever 4 ports, 1-4, 5-8, 9-12, etc.. are on separate EPIFs. So when doing network planning, you may want to consider separating connections.
For example, if you are only using a few ports on a CS150, it would be beneficial to use ports 1,5 and 9 as they are all on different EPIFs.
Regards
Pete Knoops
Cisco Systems -
Even and odd counts..
Whats the easiest way to determine if a button has been pushed and even or an odd number of times?
Here is why I'm asking...
If it has been pushed an even number of times than i want it to display one method, if odd I want
it to display another method....Add a MouseListener to your Button and call the getClickCount() method on the MouseEvent, if the following condition is true then its an even number of clicks :
if (Math.IEEEremainder((double)clickCount, 2.0) == 0.0)Ronny. -
My printer won't do duplex scan, and I need to scan a large amount of 2-sided text. So I'm going to create one document of the odd-numbered pages, and another of the even-numbered pages. I know I can manually combine them into the right order, but I'm hoping there is some automatic way to merge the documents - one of odd-numbered pages, and one of even-numbered pages - into one document in which the pages are in the correct numerical order.
Is there?Hey Seth and others:
Thanks for the positive feedback. I found myself needing to do some collating and reversing again, and discovered that the script didn't show up in Acrobat 9. So I updated the script, and moved the menu item into the "Document" menu.
Unfortunately I can't figure out how to attach the updated script in the new form editor, so I'll inline it. Copy the following script into a file and install it into your Javascripts folder, as before.
// Complements: Planet PDF (http://www.planetpdf.com/)
// Modified by Jeff Baitis for Acrobat 9 compatibility
// Improved Collate function with status bar.
// Add a menu item to reverse all pages in the active document
app.addMenuItem({ cName: "Reverse", cParent: "Document", cExec: "trustedReversePages();", cEnable: "event.rc = (event.target != null);", nPos: 0 });
app.addMenuItem({ cName: "Collate", cParent: "Document", cExec: "trustedCollatePages();", cEnable: "event.rc = (event.target != null);", nPos: 0 });
trustedReversePages = app.trustedFunction(function()
app.beginPriv(); // Explicitly raise privileges
var t = app.thermometer;
t.duration = this.numPages;
t.begin();
for (i = this.numPages - 1; i >= 0; i--)
t.value = (i-this.numPages)*-1;
this.movePage(i);
t.text = 'Moving page ' + (i + 1);
t.end();
app.endPriv();
// Collating pages
Title: Collate Document
Purpose: User is prompted to select document to insert/collate.
Author: Sean Stewart, ARTS PDF, www.artspdf.com
trustedCollatePages = app.trustedFunction(function()
app.beginPriv(); // Explicitly raise privileges
// create an array to use as the rect parameter in the browse for field
var arRect = new Array();
arRect[0] = 0;
arRect[1] = 0;
arRect[2] = 0;
arRect[3] = 0;
// create a non-visible form field to use as a browse for field
var f = this.addField("txtFilename", "text", this.numPages - 1, arRect);
f.delay = true;
f.fileSelect = true;
f.delay = false;
// user prompted to select file to collate the open document with
app.alert("Select the PDF file to merge with")
// open the browse for dialog
f.browseForFileToSubmit();
var evenDocPath = f.value;
var q = this.numPages;
var t = app.thermometer;
t.duration = q;
t.begin();
// insert pages from selected document into open document
for (var i = 0; i < q; i++) {
var j = i*2;
this.insertPages(j, evenDocPath, i);
t.value = i;
t.text = 'Inserting page ' + (i+1);
t.end();
// remove unused field
this.removeField("txtFilename");
app.endPriv();
Have fun
Edited: Found some bugs, fixed the code up a bit -
Split a column based on even and odd rows
Table1
Table2
Col1
Odd
Even
A
A
B
B
C
D
C
E
F
D
G
H
E
I
NULL
F
G
H
I
I am using MS SQL v2005
I want to split a column into two columns : -
one column must have all the odd rows data while the other column must have even rows data
in other words I want the data in Table1 to be displayed as data in Table2.
Col, Odd and Even are column namesIn SQL 2005 wont approach like what I suggested do only a single scan of the table?
A major problem with your solution is that you assume that 1) the values are contiguous 2) they are numeric. Try the below:
CREATE TABLE #t (id INT NOT NULL IDENTITY(1,1), Col1 CHAR(1))
INSERT INTO #t VALUES ('A'),('B'), ('C'),('D'), ('E'),('F'), ('G'), ('H'), ('I')
go
SELECT MAX(CASE WHEN Col1 % 2 > 0 THEN Col1 END),
MAX(CASE WHEN Col1 % 2 = 0 THEN Col1 END)
FROM
SELECT Col1,ROW_NUMBER() OVER (PARTITION BY (Col1 % 2) ORDER BY Col1) AS Rn
FROM #t
)t
GROUP BY Rn
go
; WITH numbering AS (
SELECT Col1, row_number() OVER (ORDER BY Col1) AS rowno
FROM #t
), leading AS (
SELECT Col1 AS odd, LEAD(Col1) OVER(ORDER BY rowno) AS even, rowno
FROM numbering
SELECT odd, even
FROM leading
WHERE rowno % 2 = 1
go
DROP TABLE #t
Erland Sommarskog, SQL Server MVP, [email protected] -
Hi ALL
I have a requirement in my report there are two things location with customer details and meter reading with costing Now my req is that location details should come on odd pages and starting from page 3. For meter details should come on even pages starting from page 4 can you plz help me in this
Regards
Prasooni did not quite get what your report structure is.
Is it something like
-Customer
-- Location details
-- Meter reading
-Customer
-- Location details
-- Meter readingIf so does the customer record fill pages 1 and 2?
Does the location details always fit on one page or could per be more data than for one page?
Does the meter-reding details always fit on one page or could per be more data than for one page? -
Duplicate buttons on even and odd pages
I would to be able to place in Acrobat on 100 facing pages document some buttons for printing and navigating but on different positions for odd and even pages. The Duplicate command does not offer any option for it. There is any available script for it? Thanks.
Only if user written.
You could use the template feature of Acrobat to make 2 templates, one for the odd pages and the the other for the even pages, and then overlay the template on the appropriate pages.
Or split the PDF into odd and even pages, apply the buttons and then recombine the PDFs. -
Random number generator. Even and odd numbers
I have a problem.
I have a random number generator, it goes from 0 to 20, I need to have two indicators, one will show how many numbers are pairs, and how many are not.
I tried to use the decimated 1D array, but since those numbers are random, it doesn´t work for me. I´m new with LabView, and I don´t know if there is some other way to make it work, I need some help, especially with some examples.
Thank you so much.
Solved!
Go to Solution.RavensFan wrote:
Spoiler (Highlight to read)
Create the array. Use quotient remainder to divide by 2. Now you have an array where all the odd numbers are now 1, and all the even numbers are now 0. Sum the array and you have the total number of odd numbers. The number of evens will be the length of the array divided by the # of odds.
Create the array. Use quotient remainder to divide by 2. Now you have an array where all the odd numbers are now 1, and all the even numbers are now 0. Sum the array and you have the total number of odd numbers. The number of evens will be the length of the array divided by the # of odds.
Haha I lost sight of the fact that he was looking for the amount off odds and evens. Good one.
Bill
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all. -
I have to create a po print on both sides.
The odd pages have the po but the odd pages have condition terms, is there a way in xml publisher to do this.
Can I test on page number, or can I set default text for odd pages ?
Thanks for your helpDo you really need bleed at the spine? Unless you are doing spiral binding, or you really have art dying into the binding, you generally don't.
In cases where you do, it is easy to "break" a facing-pages spread into a left and right half. Disable page shuffling for the spread, then just pull it apart. See InDesignSecrets » Blog Archive » Breaking Pages Apart to Bleed Off a Spine
There's a script reference down inthe comments from Harbs, too, that will do the entire document if you need it (I believe Daniel said earlier today that he's had trouble with the script once and had to break the spreads manually, but I've never seen another report of a problem.)
Maybe you are looking for
-
Macbook Pro freezes and comes up with stripey screen and kernel report?
Hi there, I've had a couple of issues with my Macbook Pro Version 10.6.8 which I bought in 2011. I had an issue with it about six months after I first bought it when I accidentally dropped my mac, and the screen occasionally came up with blue and pin
-
End-of-file reached (-39) error
I currently use QT 7.0.2. Frequently when I join multi-part AVI. videos together using "Split and Concat" software and try to play them I get the error message "end-of-file reached (-39)" and nothing happens. What does this message mean and how can I
-
Query on Purchase order header texts
Hi everybody, Can any one please help me on some info regarding hearder texts used in Purchase orders. I have a requirement like, for one of the header texts in PO like "TERMS OF PAYMENT", if the user inputs a text like "Payment terms" in taht partic
-
BI Role upload to Portals got messy
I posted same question on the PCD but got no response so I am reposting this here. we have a Role called Power User and inside there are 10 different folders for each plant and in turn each of these folders have some queries embedded in them. We uplo
-
Hello All, There is Role through which End Users does not have access to the Overview (mountain) screen for the Display Actions InfoType. When we use the eye glasses to view the detail of an Employee's Actions we can see the individual records. Howev