Results 1 to 7 of 7
Thread: Adding odd numbers
 02122012, 08:50 PM #1Member
 Join Date
 Feb 2012
 Posts
 6
 Rep Power
 0
Adding odd numbers
This is probably something very basic, but I can't for the life of me think of how to get this to work.
What I need to do is prompt the user for a positive integer(n), then add n odd numbers together (eg: n=4; add 1+3+5+7 = 16)
I know I need to loop through 4 times: for(int i = 0; i < n; i++)
I can add up sequential numbers, just can't seem to get it right when I only need odd numbers.
I'm stuck on this problem, can someone help me with this?
Thanks!
 02122012, 09:08 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,390
 Blog Entries
 7
 Rep Power
 25
Re: Adding odd numbers
If you have to loop n times, the largest number in your loop is 2*n1 (check this); you only want to do odd numbers so you have to start at one (1) with an increment of 2; so:
Java Code:for (int i= 1; i <= 2*n1; i+= 2) // do your dirty deed here ...
kind regards,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 02122012, 09:16 PM #3Member
 Join Date
 Feb 2012
 Posts
 6
 Rep Power
 0
Re: Adding odd numbers
yeah, I understand that this is just squaring the N number, but here's the question I'm trying to answer:
Write a program that reads in a positive integer N and then calculates and displays
the sum of the first N odd integers. For example, if N is 4, your program should
display the value 16, which is 1 + 3 + 5 + 7.
I want to break it down into the it's smallest parts so I can get a better understanding of why and how it's doing what it's doing.
 02122012, 09:19 PM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,390
 Blog Entries
 7
 Rep Power
 25
 02122012, 09:44 PM #5Member
 Join Date
 Feb 2012
 Posts
 6
 Rep Power
 0
Re: Adding odd numbers
Thanks for the help! I was thinking about it completely wrong.
Java Code:import acm.program.*; public class AddOddInts extends ConsoleProgram { public void run() { int n = readInt("Enter a positive integer: "); int total = 0; for (int i = 1; i <= 2 * n  1; i++) { if (i % 2 != 0) { total += i; //println(i + " Odd " + total); } } println(total); } }
 02122012, 10:24 PM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,390
 Blog Entries
 7
 Rep Power
 25
Re: Adding odd numbers
Almost perfect; if i is an odd number, then i+2 also is an odd number. You can change your loop to this:
Java Code:for (i= 1; i <= 2*n1; i+= 2) total+= i;
kind regards,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 02122012, 11:25 PM #7Member
 Join Date
 Feb 2012
 Posts
 6
 Rep Power
 0
Similar Threads

adding numbers in a string?
By brycepower1 in forum New To JavaReplies: 14Last Post: 08192011, 05:39 PM 
adding numbers
By droidus in forum New To JavaReplies: 7Last Post: 03092011, 05:26 AM 
adding numbers in an array together
By pds8475 in forum New To JavaReplies: 3Last Post: 01222011, 08:23 PM 
Adding numbers in an array?
By hawaiifiver in forum New To JavaReplies: 9Last Post: 01222009, 04:50 AM 
Adding numbers in array
By Shaolin in forum New To JavaReplies: 1Last Post: 11152007, 07:30 PM
Bookmarks