# need to calculate d sum of all odd numbers between 1 and 100

Page 1 of 1

## 4 Replies - 16415 Views - Last Post: 20 February 2008 - 10:49 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=43818&amp;s=8f1ea13ee0606f51c828444d7f281c4d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jayraj

Reputation: 0
• Posts: 11
• Joined: 16-February 07

# need to calculate d sum of all odd numbers between 1 and 100

Posted 20 February 2008 - 05:26 AM

its not showing an error but its givin a d list of all odd numbers between 1 nd 100
wat i need sis d sum of all odd numbers

```using System;

class oddNumbers
{
public static void Main ()
{

for (int i = 0; i < 100; i++)
{

if (i%2 != 0)
{
Console.WriteLine("odd num"+i);

}
int sum=0;
sum=sum+i;
Console.WriteLine(""+sum);
}

}
}

```
[font=Arial][size=2]

This post has been edited by jayraj: 20 February 2008 - 06:09 AM

Is This A Good Question/Topic? 0

## Replies To: need to calculate d sum of all odd numbers between 1 and 100

### #2 PsychoCoder

Reputation: 1619
• Posts: 19,853
• Joined: 26-July 07

## Re: need to calculate d sum of all odd numbers between 1 and 100

Posted 20 February 2008 - 05:39 AM

You really need to do more than just post your code and hope we can decipher your title to decide what you need help with. It's good that you posted your code, is this code causing an error? If so what exactly is the error. Otherwise, what is this code doing versus what you need it to do.

### #3 SpiderSpartan

Reputation: 14
• Posts: 120
• Joined: 06-February 08

## Re: need to calculate d sum of all odd numbers between 1 and 100

Posted 20 February 2008 - 07:12 AM

Move "int sum=0;" and "Console.WriteLine(""+sum)" to outside of the for loop. Each time you pass through the loop it is reinitializing sum to 0 and writing the value of sum.
Also this code is giving you all the odd numbers between 0 and 99. Not that it makes a difference as long as the upper bounds(100 in this case) and lower bounds(0) are even.

### #4 jayraj

Reputation: 0
• Posts: 11
• Joined: 16-February 07

## Re: need to calculate d sum of all odd numbers between 1 and 100

Posted 20 February 2008 - 07:47 AM

SpiderSpartan, on 20 Feb, 2008 - 07:12 AM, said:

Move "int sum=0;" and "Console.WriteLine(""+sum)" to outside of the for loop. Each time you pass through the loop it is reinitializing sum to 0 and writing the value of sum.
Also this code is giving you all the odd numbers between 0 and 99. Not that it makes a difference as long as the upper bounds(100 in this case) and lower bounds(0) are even.

hey buddy i tried it but its not workin its showing an output 200
i want to get an answer for calculatin summ of all odd numbers

PsychoCoder, on 20 Feb, 2008 - 05:39 AM, said:

You really need to do more than just post your code and hope we can decipher your title to decide what you need help with. It's good that you posted your code, is this code causing an error? If so what exactly is the error. Otherwise, what is this code doing versus what you need it to do.

can u plz help now as i have specified wat i want my program to do

### #5 jayraj

Reputation: 0
• Posts: 11
• Joined: 16-February 07

## Re: need to calculate d sum of all odd numbers between 1 and 100

Posted 20 February 2008 - 10:49 AM

SpiderSpartan, on 20 Feb, 2008 - 07:12 AM, said:

Move "int sum=0;" and "Console.WriteLine(""+sum)" to outside of the for loop. Each time you pass through the loop it is reinitializing sum to 0 and writing the value of sum.
Also this code is giving you all the odd numbers between 0 and 99. Not that it makes a difference as long as the upper bounds(100 in this case) and lower bounds(0) are even.

i got it mate thanks a lot