programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Junilu Lacar
Sheriffs:
• Rob Spoor
• Liutauras Vilda
• Tim Cooke
Saloon Keepers:
• Tim Moores
• Piet Souris
• Tim Holloway
• Jj Roberts
• Stephan van Hulst
Bartenders:
• Himai Minh
• Carey Brown
• Frits Walraven

# Another one from "Art and Science of Java"

Greenhorn
Posts: 15
• Number of slices to send:
Optional 'thank-you' note:
I sure hope I don't wear out my welcome. I am struggling to get my head around these programming exercises.

"Write a program that reads in a positive integer N and then calculates and displays the sum of the first N integers. For example if N is 4, your answer should be 16, 1+3+5+7."

After hours of trying to get this, I decided to just try and display the digits but I haven't been able to do that just yet. Here is what I have so far which only does a countdown of the odd integers < N. Can someone set me on a better path? Thank you for your patience and help.

Rancher
Posts: 1776
• Number of slices to send:
Optional 'thank-you' note:
Darrin,
welcome to the ranch. I fear you are trying to print the odd numbers. Can you look at the requirement again. Is that adding all first N nos are only odd numbers?

Ranch Hand
Posts: 5575
• Number of slices to send:
Optional 'thank-you' note:
pseudo code:
Algorithm:getTotalSum(num)
input:num, the integer
output:result, the sum of odd number for given count

* get the given count, num.
* int result <----0;
* for count <----1 to num > 0 do
result += count;
num--;

Marshal
Posts: 73317
332
• Number of slices to send:
Optional 'thank-you' note:

What you appear to be doing is calculating the sum of an arithmetical progression. If I remember correctly, it is

(first element + last element) × how many elements ÷ 2

So Σ 1...4 = (1 + 4) × 4 ÷ 2.
That many be quicker than using a loop . . . but maybe the intention of the exercise is for you to learn to use a loop. You would have to change your calculation for odd numbers, remembering that 1 + 2 × (4 - 1) = 7.

Now you see that (1 + 7) × 4 ÷ 2 = 16

Sheriff
Posts: 22271
119
• Number of slices to send:
Optional 'thank-you' note:
It's n * (n + 1) / 2 actually.

Campbell Ritchie
Marshal
Posts: 73317
332
• Number of slices to send:
Optional 'thank-you' note:
I couldn't remember the correct spelling of n at the time

Darrin Altman
Greenhorn
Posts: 15
• Number of slices to send:
Optional 'thank-you' note:
I guess I put the cart before the horse. Before knowing how to calculate the total of a specific number of integers I need to know how can I generate a specific amount of integers? Once I can create them I will worry about adding them. So what I have tried so far is:

int x = readInt("Enter a number: ");

and then that number input will tell the program how many integers to create.

For example, if the user inputs 4, the program will generate four digits, like 1 2 3 4.

I created a for statement:

for (int i = 1; i < x; i+=2);

Which will give me only odd numbers, but doesn't give me the four integers. So I don't believe the for statement is the correct approach. I feel like generating a set number of integers starting at 1 and increasing by two a certain amount of times, must be simple, but for some reason I'm stuck.

Any more help appreciated.

Ranch Hand
Posts: 59
• Number of slices to send:
Optional 'thank-you' note:

Darrin Altman wrote:
For example, if the user inputs 4, the program will generate four digits, like 1 2 3 4.

You could go with a while loop, compare against input and then printing and incrementing inside the loop.
Im almost on the same page with you, I'm struggling around chapter 7.

Campbell Ritchie
Marshal
Posts: 73317
332
• Number of slices to send:
Optional 'thank-you' note:
You are trying to do the body of the loop, incrementing the result, in the counting part of the loop. It is probably best to write for loops in this format, whenever it is possibleThat will give you four iterations if n = 4.

If you use i += 2, you will get two iterations (in the loop you showed for 1 and 3).

Darrin Altman
Greenhorn
Posts: 15
• Number of slices to send:
Optional 'thank-you' note:
Update: I have made some progress in my opinion. I am able to take in an integer and generate a list of integers starting at 1 up to the amount input. So if the user inputs "5", the output will be 1 2 3 4 5 and will add up the total of all of those together. 1+2+3+4+5 = 15. Now I just need to have the numbers generated only be odd, so 1 3 5 7 9 if 5 was input. Here is my program method:

I tried to add to the while statement: while ((getInt < x) && (getInt % 2 != 0)) { but that didn't work. I'm not giving up yet. Again, any guidance will be appreciated.

Ranch Hand
Posts: 258
2
• Number of slices to send:
Optional 'thank-you' note:

nextNum which is (1,3,5,7,9,.... )

Kristjan Toots
Ranch Hand
Posts: 59
• 1
• Number of slices to send:
Optional 'thank-you' note:
Darrin, please use code tags. It will make reading so much more easier.
You could go with for loop where you can do something like this:

Darrin Altman
Greenhorn
Posts: 15
• Number of slices to send:
Optional 'thank-you' note:
This works but I am sure not what the instructor wanted or was expecting, but if the user inputs number 4 as in the exercise question, the program does generate the first 4 odd integers and adds up their combined total. I don't like how I initialized x as -1 to make this work. I really wanted to use a boolean expression to only generate odd numbers but I couldn't get it to work. If anyone wants to re write this in a better manner, that will be helpful.

Here is my latest code:

Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
• Number of slices to send:
Optional 'thank-you' note:

Kristjan Toots wrote:
// Instead incrementing getInt by 1, do it by 2

I missed[forgot] this in my algorithm

Campbell Ritchie
Marshal
Posts: 73317
332
• Number of slices to send:
Optional 'thank-you' note:

Kristjan Toots wrote:Darrin, please use code tags. It will make reading so much more easier. . . .

. . . And use spaces not tabs for indenting

Campbell Ritchie
Marshal
Posts: 73317
332
• Number of slices to send:
Optional 'thank-you' note:
Venny Tank,
Your post was moved to a new topic.

 You showed up just in time for the waffles! And this tiny ad: Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton