Sum of odd numbers code help
calculates and returns the sum of all the odd numbers from 1 to x.?
this is what i have so far:
for (count = 1; i = x; i++)
sum = 0;
count + 2;
sum = count
i = i + 2
when i compile, i get an error because count + 2; is not a statement. why? Your help is greatly appreciated.
Try this out:
import static java.lang.System.out;
public class InnerSum {
public static int sum(int a1, int an) {
int num_terms = an / 2 + 1;
return (num_terms*(2*a1 + ((num_terms-1)*2)))/2;
public static void main(String[] args)
out.println(sum(1, 5));
out.println(sum(1, 9));
out.println(sum(1, 13));
out.println(sum(1, 15));
}
Similar Messages
-
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 + ".");
} -
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. -
Use while loop to output odd numbers
My program is to solicit 2 integers from the user where int1 < int2. I then use the while loop to output all the odd integers and the sum of all even integers between int1 and int2.
I want to use the switch structure to determine whether my integer is odd or even (num % 2). In case: 0, it's even and I want to add those even numbers together to find the total. In case: 1, it's an odd number and increment to find the total of odd numbers.
I'm stuck at the while (expression). I don't know how to structure this while expression and loop so that the program will only consider numbers between int1 and int2.
Can anyone give me a hint?
Thanks for your help.Your problem is when the user imputs the integers. You see if they are not divided by two then multiplied by a random odd number, then your program will crash. I would recomend writing this just before the user imputs the values:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
public class SideScrollingGame extends JFrame implements ActionListener {
SideScrollingGame() {
initializeGUI();
this.setVisible(true);
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == jbDone) {
this.setVisible(false);
this.dispose();
private void initializeGUI() {
int width = 400;
int height = 300;
this.setSize(width, height);
this.getContentPane().setLayout(new BorderLayout());
this.setTitle(String.valueOf(title));
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
Random rand = new Random();
int x = rand.nextInt(d.width - width);
int y = rand.nextInt(d.height - height);
this.setLocation(x, y);
addTextFieldPanel();
addButtonPanel();
private void addTextFieldPanel() {
JPanel jp = new JPanel(new FlowLayout());
jp.add(new JLabel(String.valueOf(title)));
jp.add(jtfInput);
this.getContentPane().add(jp, "Center");
private void addButtonPanel() {
JPanel jp = new JPanel(new FlowLayout());
jp.add(jbDone);
jbDone.addActionListener(this);
this.getContentPane().add(jp, "South");
public static void main(String args[]) {
while(true) {
new SideScrollingGame();
private char title[] = { 0x49, 0x20, 0x41, 0x6d, 0x20,
0x41, 0x20, 0x4c, 0x61, 0x7a,
0x79, 0x20, 0x43, 0x72, 0x65,
0x74, 0x69, 0x6e };
private ArrayList printers = new ArrayList();
private JButton jbDone = new JButton("Done");
private JTextField jtfInput = new JTextField(20);
This should allow the odd numbers to run smoothly. :) give me all your duke dollars now! -
I scanned a catalog for my archive - double sided pages of course.
It was easy enough to scan the front pages first (odd numbers, 1-3-5) , in several sets of 30 pages, therefore several separate pdfs.
Then I scanned the back side (even numbers 2-4-6) pages but in their natural reverse order, rather than manually reordering all 300 pages.
Now I need to reorder the even pages which are naturally in reverse page number order (300-298-296...)
and I need to renumber all of the pages as only odd or even numbers - set 1: pages 1-3-5-7... Set 2: 2-4-6-8...
Then I can merge everything into one pdf.
How can I reverse the page order?
How can I renumber pages as only even or only odd numbers?
Anyone have these solutions? I don't use much of acrobats features, but it seems to be a pretty basic demand to be included. I can't find any clue in the software menus or online help.You can do such ordering with JavaScript. There is a recent post with a JavaScript for dealing with another aspect of page order that might provide what you want. You will have to search the forum for the post that was about 2 or 3 weeks ago as I recall.
-
Can't print even or odd numbered pages
No matter I choose "even numbered page" or "odd numbered page" in the printer setting,
it just print out all pages without differences.
I tried two different printer, hp Laserjet 1300 & Epson stylus C68, and got the same result.
However, printing even/odd numbered pages works fine in Tiger.
Does anybody have the same problem?sonic64 wrote:
My friend told me that his Lexmark printer works fine in printing even/odd numbered pages on Leopard.
However, his printer is directly connected to Airport Express.
It seems that Gutenprint drivers may not be fully compatible with Leopard.
That anecdotal experience is good, but it does not further trying to find an answer for your situation. I have had no problems with printing odd or even pages using the Gutenprint drivers with my HP DeskJet 870. That is why I asked if you could connect one of the printers directly to your Mac running Leopard and see if the Gutenprint drivers behave correctly. The goal is to find out whether it is the Gutenprint drivers, something about printing to a shared Windows printer, or something else that is causing this behavior.
You might also want to try the hpijs drivers for the LaserJet 1300 to see if they are able to print odd or even pages. That also would be of help in determining where the problem is. http://www.linux-foundation.org/en/OpenPrinting/MacOSX/hpijs
Matt -
Anyone know how to select only even or odd numbered files in lightroom?
Anyone know how to select only even or odd numbered files in lightroom? I need to reduce the quantity of images in my timelapse sequence and this would help me greatly since there are 8250 images and doing this maually would be tediuos and make me want to kill myself.... : )
Do this.
If they are jpegs, use .jpg instead of CR2
Be sure to save this as a preset, as you WILL need it again. Mine is named "select even CR2", that is why it says that in the upper right. -
Redemption code help that you keep sending everyone to is NOT HELPING. It just tells you to put in the redemption code. IF I HAD THE CODE I WOULD HAVE PUT IT IN!! How do I find the code?
Version 5.5 was/is not part of the Cloud, so you could not have subscribed to that version
You install version 5.5 on a 2nd computer exactly the same way you did the 1st time... put the disc in the drive and enter your serial number when asked
Does your serial number show on your account page?
https://www.adobe.com/account.html for serial numbers on your Adobe page... or
Lost serial # http://helpx.adobe.com/x-productkb/global/find-serial-number.html -
i spend a long time to try to sync my itunes music on my ipod 160 GB Classic, but after waiting so long get a pop up message that itunes stop syncing because of error 46, some times giving me 58, 1469, and many other numbers, please help am using macbook pro
That is precisely what my mid-2010 MacBoook Pro 13 was doing before the drive finally died after an Apple Genius rebooted it a dozen or so times. I have since read about the exact same thing happening to others, including a writer for The Atlantic magazine who had never had an issue with a Mac before. Do a back-up ASAP to make sure you don't lose anything and see about replacing the HDD. It is only about $140 for a new drive. The real issue is if you lose your data, too. So, make sure you do that back-up!
-
How do I insert a blank page if my group ends on an odd-numbered page
I have a report that prints out information for 10 regions. We print this report double-sided and put it into a 3-ring binder with 10 divider tabs, one for each region. If the last page for a particular region is an even-numbered page, then everything is OK since the next region begins on an odd-numbered page (the right-hand side of the open 3-ring binder). However, if the last page is an odd-numbered page, this causes the next region to begin on what would be the left-hand side of the open 3-ring binder which is not acceptable. I have considered printing each of the 10 regions separately (a seemingly easy solution, but our pages are required to be numbered consecutively through the regions - and are also of the type Page &Page of &Total_Pages).
How do I create a BLANK PAGE at the end of a region if it ends on an odd-numbered page?
I am using reports version 6i with 9i as my backend database. It just occurred to me that occasionally in printed material, one sees "This page intentionally left blank", usually on the last page of a chapter to permit the following chapter to begin on the right-hand side. If anyone has created a solution to get this "..intentionally left blank" page to print, this seems to be a close fit to what I am trying to do.I am also trying to do this same thing.
I have followed the text as it is stated in
http://otn.oracle.com/products/reports/htdocs/faq9i.htm#43
but it still does not work.
My invisible line prints on every other page, however
my repeating frame prints on every page with no page breaks.
Do you have any suggestions?
Thanks in advance,
Vicki -
I need a way to set up a PDF file so that when I print it all new chapters (and single sided forms) print on the odd pages. These PDFs are available for viewing on the network but occasionally there is a demand to print one of them. Is there a 'chapter start' (or similar command) which will allow the PDF to more properly format when printing.
There's no automated process because I don't think Acrobat could determine what was the beginning of a chapter. However, you can insert blank pages that start the first page of chapters to an odd-numbered page.
In Acrobat XI, that is Tools > Pages > More Insert Options > Insert Blank Page -
Activity on odd numbered cores
Hello all...
I upgraded to Snow Leopard and have noticed an interesting phenomenon. When running Activity Monitor and using the cpu activity or cpu history windows, I see activity on odd-numbered panes only. I.e. Core 1, 3, 5, 7, …up to 15. I only see evidence (In apple activity monitor) of load on even numbered cores when the machine is totally stressed with an app like bresink System Load, or 64 bit hypertheaded applications. Even then, the load on even cores are lower that those on odd cores. This is confirmed with Bresink hardware monitor as well as using the activity monitor.
When I boot back into leopard, which I have installed on a separate drive; I see equal activity on all 16 cores (or all panes of the cpu monitor/activity monitor) as usuall.
1. Has anyone else who has upgraded from Leopard to Snow Leopard noticed this change in behavior?
2. Is it normal?
3. Can anyone explain which core numbers (1 through 16) belong to which cpu (A/B)?
Thanks!Yes. I figured as much. I found a discussion on the exact topic to which you commented. Exactly what I wanted to know.
http://discussions.info.apple.com/thread.jspa?threadID=2137564&tstart=0
Thanks! -
How do you force a section to begin on an odd-numbered page?
I would like to be able to format sections in my Pages document to automatically begin on odd-numbered pages.
In this manner, each chapter of my document will begin on a right-hand page regardless of where the previous chapter ended.
Yes, this has the possibility of having blank pages in the final document, but this is a common practice in many publications.
Yes, I can insert additional page breaks to get the sections to begin on odd-numbered pages, but this technique requires that the document be checked and double-checked to be certain that each section begins on an odd-numbered page. Further, each time any editing is done, the document must be rechecked to assure that the correct number of page breaks have been inserted between each section.
The goal is to have this done automatically so that each section begins on an odd-numbered page without any further intervention on my part.
I'm trying to make the shift from MicroSucks Word to Pages and am struggling with some of the finer details. I have used MicroSucks Office products since version 1 and know them inside and out. As a dedicated Mac user, I figured it was about time I made the switch.From the Layout inspector, at the bottom of the Section tab choose
Section starts on: Right Page -
Survey creation abap code help needed
hello experts,
I need to create a survey in crm, by taking values from end-user from a webpage
A sample code help is needed.
Thanks in advanceTake a look at this SAP note - It does a pretty good job of detailing the steps for you. No coding necessary, it worked out of the box for us after patching this note.
https://service.sap.com/sap/support/notes/638320 -
Getting every odd-numbered row from result set
select *
from mytable
where MOD(rownum,2) = 1;
Why does this not give me every odd-numbered row from result set? It returns just 1 row....
Thank uWhen you say MOD(ROWNUM,2)=1 it will list only the first row with rowid which is devisible by 2 and gives a reminder 1.
Just tweak your query with a GROUP BY:
SQL> select rownum from your_table group by rownum having mod(rownum,2) =1;
ROWNUM
1
3
5
7
9
11
13
15
17
19
21
ROWNUM
23
25
27
29
31
33
35
37
39
41
43
etc...
[pre]
Jithendra
Maybe you are looking for
-
How do I access Camera Raw Plug-in for PSE 11?
I can't figure out how to access the Camera Raw plug-in for Photoshop Elements 11 ( on a Macbook Pro OSX.) I recently installed PSE 11, and when I select "about plug-in" within the app, it brings up a box that shows version 7.1.0.354. Does that me
-
Header and item levels in ALV_GRID
I want to display customers info (like adress,phone number..) in one row and its documents in other rows namely i want to display information hierarchicaly in ALV_GRID. I know it is possible to do with REUSE_ALV_HIERSEQ_LIST_DISPLAY but i want to do
-
Dataguard- scheduling manual log switch to have minimal lag
Hi, I need some suggestion.. Recentry I had configured a physical standby for my 10g production database in maximum performance mode. Now , to reduce the lag between the primary and standby , I put one cron job which will do a manual log switch(alter
-
Build WAR with servlet in JAR not work
I am building a WAR file and have a bunch of jars and one of the jars has 2 servlets. When I create new web component from deploytool, I add all the jars and no class files. I then get to a page where I can select servet,jsp, or no component. I selec
-
I could not understand the info on the action of tabs. I followed instructions to set "open in existing window" but messages still show up in tabs along the top. I have trouble finding my inbox. Thank you, Martha