6 Replies - 583 Views - Last Post: 19 October 2012 - 05:03 AM Rate Topic: -----

#1 j_007  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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

and add the odd numbers

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  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • 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
Was This Post Helpful? 0
  • +
  • -

#3 lameraz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 62
  • 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


Was This Post Helpful? 0
  • +
  • -

#4 karabasf  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 202
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#5 j_007  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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:?!
Was This Post Helpful? 0
  • +
  • -

#6 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • 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

Was This Post Helpful? 0
  • +
  • -

#7 karabasf  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 202
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1