# Summation of odd numbers

Page 1 of 1

## 6 Replies - 1281 Views - Last Post: 19 October 2012 - 05:03 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=296257&amp;s=e0dc8ed783eda8da92a1e804e8669442&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 j_007

Reputation: -1
• Posts: 25
• Joined: 06-July 12

# Summation of odd numbers

Posted 19 October 2012 - 01:45 AM

Hi there ,

I want to make a simple program
that :

reads numbers in a one line and storing

it in a Long variable ,then i want to

examine the entered number

in a variable ?!

and i try to write some code :

```import java.util.Scanner;

public class Odd
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
int counter=0;
long y=0;

long num=input.nextLong();

if (num%2 !=0)
{
System.out.println(num);
}

```

but it came to nothing

Is This A Good Question/Topic? 0

## Replies To: Summation of odd numbers

### #2 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 12

## Re: Summation of odd numbers

Posted 19 October 2012 - 02:00 AM

Since you are trying to add odd numbers, you might want to initialize a variable called 'sum_odd' to 0 and then use that value in the loop to add the numbers.

regards,
Raghav

### #3 lameraz

Reputation: 0
• Posts: 75
• Joined: 07-May 10

## Re: Summation of odd numbers

Posted 19 October 2012 - 02:04 AM

You can use something like this to add the numbers instead of mod
```int sum = 0;
for(int i =0; i <= n; i++)
sum += 2*i + 1

```

### #4 karabasf

• D.I.C Regular

Reputation: 202
• Posts: 417
• Joined: 29-August 10

## Re: Summation of odd numbers

Posted 19 October 2012 - 02:11 AM

Well, you're only asking the user to enter a number once. I am not sure what the actual requirements are, so I am going to assume that the user enter N inputs and from those inputs the sum of odd numbers is determined.

This can be easily implemented as:
```// Get the amount of times the user wants to enter a number
input = Retrieve input from console
N = Parse(input)

// Some variable declaration
long input;
long sumOddNr = 0;

// Loop N times to request userinput
for i = 1 to N
// Retrieve the value the user inputs
input = Retrieve input from console
currentNumber = Parse(input);

// Determine if the user number is odd. if yes, sum it to the sumOddNr
if(currentNumber %2 != 0)
sumOddNr = sumOddNr + currentNumber;
endif
endfor

```

Note that this is not actual code. It's pseudocode to display the outline of the algorithm.

Have fun

This post has been edited by karabasf: 19 October 2012 - 02:13 AM

### #5 j_007

Reputation: -1
• Posts: 25
• Joined: 06-July 12

## Re: Summation of odd numbers

Posted 19 October 2012 - 04:27 AM

mmmmm...
first of all

thank you for help

but the codes doesn't help me

because i want a simple cod to do this

the program is a homework

and i want to use only a loop state

and if state with the % operator

the question is if i can

enter a numbers without a pressing enter

and save the number in a long variable

to examine every single number to show

that is it odd and add it to

summation state

and print the summation

like this:

12345678910 >>>> this is the number without pressing Enter:)(Variable Type is Long

So the code will Examine the first number if it's Odd

so it will save it in another variable

like smm+=num;>>>>(Which num is the firs number (1))

and so on !!!

and the result will be

the odd numbers is 25:?!

### #6 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 12

## Re: Summation of odd numbers

Posted 19 October 2012 - 04:37 AM

Quote

thank you for help

but the codes doesn't help me

because i want a simple cod to do this

the program is a homework

If you thought that you will be spoon-fed code over here, you thought wrong. People are here to help you and guide you only when you put some genuine effort from your end.

Seriously...I think you need to read This link. Although it is a post for the C and C++ forum, the rules hold good for the Java forum as well.

regards,
Raghav

This post has been edited by raghav.naganathan: 19 October 2012 - 04:37 AM

### #7 karabasf

• D.I.C Regular

Reputation: 202
• Posts: 417
• Joined: 29-August 10

## Re: Summation of odd numbers

Posted 19 October 2012 - 05:03 AM

Considering the question and new requirements of the OP: you can use the following piece of pseudocode (not actual Java code):

```userString = Read input

sum = 0;

for i = 0 to length userString
currentNumber = Parse(char at userInput(i));

//Determine if the number is an odd number
if(currentNumber % 2 != 0)
sum = sum + currentNumber
endif
endfor

```

Storing the input as a long will work, but it involves mathematical operations. Why not working with a string and parsing the characters into integers one by one? This would be a way easier approach.

And as raghav.naganathan said, we cannot and will not give you actual code to work with

Recommended documentation:
http://docs.oracle.c...ang/String.html

This post has been edited by karabasf: 19 October 2012 - 05:04 AM