need help with methods

need a method to add 0 to N, main program should pass N to method and the method should return the sum to the main program.

/*
Course Number: CS-151
Course Title: Intro to Programming
Student: Gene Carr
Lab 2
The purpose of this lab is to write a program to use a for loop,
a while loop, write a method and to pass N to the method.
*/

public class labTwoa
{
//public static int counter(int n)
//{

//}
public static void main(String[] args)
{
int x = 0;
int i;

x = 0;
for(i=0;i<101;i++)
{
//x+=i;

x = x + i;
}
System.out.println("The sum from 0 to 100 is: " + x);
System.out.println(i + "
");

x = 0;
i = 0;

while(i<=1000)
{
x = x + i;
i++;
}
System.out.println("The sum from 0 to 1000 is: " + x);
//System.out.println("add("+i+")="+x);
//System.out.println("add: " + i);

//System.out.print(i + "
");


}// end main

public static int counter(int n)
{
{
int sum = (n*(n+1)/2);
return sum;
}// end counter

i = 0;
x = 0;

{
System.out.println("Enter a number");
i = SavitchIn.readInt();
x = counter(i);
}

} // end counter
} // end LabTwoa

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories