codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  sum of odd integers  rsixtyone at 01:45 on Monday, November 07, 2005
 

Hello all,

I'm writing a program write now and got stuck on this one code segmet:

How can you write a code segment that will displays the sum of odd integers from 1 to n?

thanks

  Re: sum of odd integers  crwood at 17:31 on Monday, November 07, 2005
 


public class OddSum
{
public static void main(String[] args)
{
System.out.println(getOddSum(12));
}

private static int getOddSum(int n)
{
int sum = 0;
System.out.print("[");
for(int j = 1; j <= n; j++)
{
if(j % 2 == 1)
{
System.out.print(j);
if(j < n-1)
System.out.print(", ");
sum += j;
}
}
System.out.print("]\n");
return sum;
}
}









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Re: sorting and Linked list
•  Re: need help linked list
•  Re: Help with arrays
•  Re: Reading from a file
•  Re: Why Use Method?
•  Re: Help with a simple program
•  Re: need help with quiz
•  Re: Help with filesystem object & displaying in a table
•  Re: Genetic Algorithm Help


Recent Articles
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net
Creating CSS Buttons


Site Survey
Help us serve you better. Take a five minute survey. Click here!

© Copyright codetoad.com 2001-2005