Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# Another one from "Art and Science of Java"

Darrin Altman
Greenhorn
Posts: 15
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.

John Jai
Rancher
Posts: 1776
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?

Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
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--;

Campbell Ritchie
Sheriff
Posts: 50266
79
You won't wear out your welcome by asking interesting questions

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

Rob Spoor
Sheriff
Posts: 20669
65
It's n * (n + 1) / 2 actually.

Campbell Ritchie
Sheriff
Posts: 50266
79
I couldn't remember the correct spelling of n at the time

Darrin Altman
Greenhorn
Posts: 15
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.

Kristjan Toots
Ranch Hand
Posts: 59
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
Sheriff
Posts: 50266
79
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
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.

Raymond Tong
Ranch Hand
Posts: 255
2
Try adding one variable to your program

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

Kristjan Toots
Ranch Hand
Posts: 59
• 1
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
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
Kristjan Toots wrote:
// Instead incrementing getInt by 1, do it by 2

I missed[forgot] this in my algorithm

Campbell Ritchie
Sheriff
Posts: 50266
79
Kristjan Toots wrote:Darrin, please use code tags. It will make reading so much more easier. . . .
I have added some tags.

. . . And use spaces not tabs for indenting

Campbell Ritchie
Sheriff
Posts: 50266
79
Venny Tank,
Your post was moved to a new topic.