Results 1 to 7 of 7
Thread: Adding odd numbers
- 02-12-2012, 07:50 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 5
- 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!
- 02-12-2012, 08:08 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Re: Adding odd numbers
If you have to loop n times, the largest number in your loop is 2*n-1 (check this); you only want to do odd numbers so you have to start at one (1) with an increment of 2; so:
But, if you apply a bit of math, you don't have to loop at all; adding the numbers 1+3+5+ ... +2*n-1 is n*nJava Code:for (int i= 1; i <= 2*n-1; i+= 2) // do your dirty deed here ...
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-12-2012, 08:16 PM #3
Member
- Join Date
- Feb 2012
- Posts
- 5
- 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:
I'm trying to answer this by printing out the loop (ex: i = 0 and total = 1, i = 1 and total = 3, i = 2 and total = 5, i = 3 and total = 7) Then I'll have a separate variable to add total for find the final answer of 16.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.
- 02-12-2012, 08:19 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
- 02-12-2012, 08:44 PM #5
Member
- Join Date
- Feb 2012
- Posts
- 5
- 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); } }
- 02-12-2012, 09:24 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
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:
Variable i only has odd values so you can skip the test in the body of your for loop.Java Code:for (i= 1; i <= 2*n-1; i+= 2) total+= i;
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-12-2012, 10:25 PM #7
Member
- Join Date
- Feb 2012
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
adding numbers in a string?
By brycepower1 in forum New To JavaReplies: 14Last Post: 08-19-2011, 05:39 PM -
adding numbers
By droidus in forum New To JavaReplies: 7Last Post: 03-09-2011, 04:26 AM -
adding numbers in an array together
By pds8475 in forum New To JavaReplies: 3Last Post: 01-22-2011, 07:23 PM -
Adding numbers in an array?
By hawaiifiver in forum New To JavaReplies: 9Last Post: 01-22-2009, 03:50 AM -
Adding numbers in array
By Shaolin in forum New To JavaReplies: 1Last Post: 11-15-2007, 06:30 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks