• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Another one from "Art and Science of Java"

 
Greenhorn
Posts: 15
Mac Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 22802
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's n * (n + 1) / 2 actually.
 
Campbell Ritchie
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I couldn't remember the correct spelling of n at the time
 
Darrin Altman
Greenhorn
Posts: 15
Mac Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Mac Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try adding one variable to your program

nextNum which is (1,3,5,7,9,.... )
 
Kristjan Toots
Ranch Hand
Posts: 59
Eclipse IDE Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Mac Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


I missed[forgot] this in my algorithm
 
Campbell Ritchie
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Venny Tank,
Your post was moved to a new topic.
 
Talk sense to a fool and he calls you foolish. -Euripides A foolish tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic