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.

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?

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.

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.

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

Joined: Oct 13, 2005
Posts: 42670

31

posted

0

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).

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.

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.