# Help With parameter passing

• 11-16-2004, 10:19 PM
RPBLEA
Help With parameter passing
I have this program, I get the right answer but it does not do the right stuff, I'm supposed to take the SumOfN takes an int n as a parameter and returns the sum ofN, Method does not do this,

Your program should loop, ask for n, if N > 0, Calculate and Print sumofN
if N <= 0, stop
I've been working on this for 3 meesly hours and can't figure it out, pleaseeee help, So frustrated.

import java.util.*;
public class sumOfN {
//Method
public static int sumOfN(int n) {
return n;
}
// main(): application entry point
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
int count = 0;
int sum = 0;
int n = stdin.nextInt();
//Return for zero and negative numbers
if (n <= 0) {
return;
}
//Calculate integer Value
for (count = n; count > 0; count--) {
sum += count;
}
//Print user input in an output form
System.out.println("SumOfN(" + n + ") has an integer value of " + sum);
}
}
• 11-16-2004, 10:34 PM
Philwx
I think you're on the right track since your code in the main method will do what you want it to do.

"SumOfN takes an int n as a parameter and returns the sum ofN"

"Your program should loop, ask for n, if N > 0, Calculate and Print sumofN"

Usually a method will either print something or return it. These instructions are unclear which they really want (or if they want both).

You could do both I suppose. Just stuff your main code in the method you have and that would work. Just don't forget to return the value after you print it.

As it stands you print out the answer, but the sumOfN method just returns the same number it is given. So it kind of halfway works.
• 11-17-2004, 12:04 AM
RPBLEA
The thing is I don't know how to write the code for the method to take the user input, calculate and see if it is 0 and then stop the program or calculate if the answer is 5, get the sum 5 + 4 + 3 + 2 + 1 + 0, could you maybe show me some code that would do this? thanks a bunch.
• 11-17-2004, 02:53 AM
ractoc
First of, there is a very quick and very clean way to calculate the SumOfN.

in your example you used 5, which results iun the SumOfN being 1+2+3+4+5=15. This is the hard way of doing things. the fast way (which only works on even numbers, but I'll give a fix for that later) would be:

n=4; // sumOfN: 1+2+3+4=10
sumOfN=(n/2)*(n+1);

This works because:
1+4 = 5 (n+1)
2+3 = 5 (n+1)
This combination can be made n/2 times.

now, to make this work with odd numbers as well is very straightforward:

n=5; // SumOfN: 1+2+3+4+5=15
sumOfN=(((n-1)/2)*n)+n;

this works because:
1+4 = 5 (n)
2+3 = 5 (n)
this combination can be made (n-1)/2 times (twice in this case).

You can check if an int is odd by doing:
if ((i%2) == 1)
{
}

the % gives you the remainder after dividing by 2, which in case of an odd number would be 1.

Now in you case, this calculation would take place in your sumOfN method, which would take theint as parameter and return the calculated value.
• 11-17-2004, 10:33 AM
RPBLEA
But How?
But still how do I put it into method SumOfN and then Calculate it in SumOfN and then Pull it out of SUmOfN to be printed? that's what I can't figure out, is how to call the method sumOfN, put all the calculations in sumofN to print it out?
• 11-17-2004, 10:37 AM
ractoc
private static int sumofn(int n)
{
int sum = 0;
// do the calculating here;
return sum;
}

System.out.println(sumofn(5));
• 11-17-2004, 11:10 AM
RPBLEA
errors
I still keep getting errors that the main cannot read sumofn, and it's not reading the user input, can anyone find my flaws

import java.util.*;

public class sumOfN {
//Method

private static int sumofn(int n)
{
int count = 0;
int sum = 0;
for (count = n; count > 0; count--) {
sum += count;
if (n <= 0) {
System.out.println("Cannot equal 0");
return sum;
}
}
return sum;

}

public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);