need help please adding odd even numbers

here is my code

import java.io.*;

publicclass addeven

{

publicstaticvoid main(String args[])throws IOException

{

BufferedReader br =new BufferedReader(new InputStreamReader(System.in));

String n1;

int num1;

int r,x;

System.out.print("Enter a number: ");

n1=br.readLine();

num1=Integer.parseInt(n1);

r=num1%2;

if (r==0)

{

System.out.println("Even number!");

}

else

{

System.out.println("Odd number!");

}

}

}

my problem is how can i make a logic to add every even numbers

example the user inputed number 10 and its even then the program will

add every even number under number 10 and it will be 10+8+6+4+2

then print the total how can i do that its not a homework im just selfstudy in java and im new to this language thank's

[1609 byte] By [Jaywhya] at [2007-11-27 6:41:45]
# 1
Google for java tutorial looping or something like that. Look into the for statement and the while statement.
jverda at 2007-7-12 18:11:32 > top of Java-index,Java Essentials,Java Programming...
# 2
i search the net but theres no similar problem like mine i can find
Jaywhya at 2007-7-12 18:11:32 > top of Java-index,Java Essentials,Java Programming...
# 3

> i search the net but theres no similar problem like

> mine i can find

you should create a loop.

1. check for the number you entered first, if it is even then do statement, else redisplay prompt

2. for example 10 is even number, then how u get the numbers that are under 10 and are all even, of course must subtract 2.

3. control the loop so it can not be nagative number when you subtract

that's all u need

Emotionsa at 2007-7-12 18:11:32 > top of Java-index,Java Essentials,Java Programming...
# 4
there's only one "odd even" number and that's 0. As adding 0 to anything results in that anything...
jwentinga at 2007-7-12 18:11:32 > top of Java-index,Java Essentials,Java Programming...
# 5

i think i found the loop for even but im going insane for odd wihs im good in looping

import java.io.*;

public class sumofeven

{

public static void main(String args[])throws IOException

{

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

int x,sum = 0;

System.out.print("Enter a number: ");

x=Integer.parseInt(reader.readLine());

for (int i=0; i<x; i=i+2)

{

sum=sum+i;

System.out.println(sum + "");

}

}

}

>

Jaywhya at 2007-7-12 18:11:32 > top of Java-index,Java Essentials,Java Programming...
# 6

> i think i found the loop for even but im going insane

> for odd wihs im good in looping

>

> import java.io.*;

> public class sumofeven

> {

> public static void main(String args[])throws

> s IOException

> {

> BufferedReader reader = new BufferedReader(new

> ew InputStreamReader(System.in));

> int x,sum = 0;

> System.out.print("Enter a number: ");

> x=Integer.parseInt(reader.readLine());

> for (int i=0; i<x; i=i+2)

> {

> sum=sum+i;

> System.out.println(sum + "");

> }

close, but yours does not check for even or odd. It would be better to do your sum by counting backward until you reach 0

boolean even = false;

System.out.print("Enter a number: ");

num1=Integer.parseInt(br.readLine());

if(num1%2 == 0) even = true;

int orig = num1;

while(num1 > 0)

{

sum += num1;

num1 -= 2;

}

~Tim

SomeoneElsea at 2007-7-12 18:11:32 > top of Java-index,Java Essentials,Java Programming...
# 7

Odds are exactly the same. as a matter of fact, the same method could be used for each one. Just for fun I created a tiny class to do what you're after.

public class FunWithNumbers

{

public static int sumSameTenseValuesBelowN(int n){

int sum = n ;

for(int i=n; i>0; i=i-2){

sum += i ;

}

return sum ;

}

}

As you can see it's identical to what you have, but the way I'm using it, I can give it any number and it will yield the proper results regardless of whether it's odd or even.

PS.

puckstopper31a at 2007-7-12 18:11:32 > top of Java-index,Java Essentials,Java Programming...
# 8

> ...

> public class FunWithNumbers

> {

> public static int sumSameTenseValuesBelowN(int n){

> int sum = n ;

>

Err, int sum = 0;

I presume?

; )

Here's one based on Leonardo de Pisa's solution of the sum of uneven numbers:

public static int sumSameTenseValuesBelowN(int n){

return (int)(((n+1)/2.0)*((n+1)/2.0));

}

prometheuzza at 2007-7-12 18:11:32 > top of Java-index,Java Essentials,Java Programming...
# 9

i'll try the idea's that you gave im almost closed but doesnt have any idea to add all even and odd here is my code so far

import java.io.*;

public class sumofeven

{

public static void main(String args[])throws IOException

{

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

int x,sum = 0;

System.out.print("Enter a number: ");

x=Integer.parseInt(reader.readLine());

for (int i=x; i>=0; i=i-2)

{

sum=i;

System.out.println(sum + "");

}

}

}

Message was edited by: jaywhy

Jaywhy

Jaywhya at 2007-7-12 18:11:32 > top of Java-index,Java Essentials,Java Programming...
# 10
sum=i;All that will do is assign the value of i to sum over and over again until sum has the value of the last i. Have a look at your previous effort.. Also I would have just a single print statement at the end not everytime around the loop.
floundera at 2007-7-12 18:11:32 > top of Java-index,Java Essentials,Java Programming...
# 11

> > sum=i;

>

> All that will do is assign the value of i to sum over

> and over again until sum has the value of the last i.

> Have a look at your previous effort.. Also I would

> have just a single print statement at the end not

> everytime around the loop.

well, as he wants to add a number that's both odd and even he shouldn't add anything...

jwentinga at 2007-7-12 18:11:32 > top of Java-index,Java Essentials,Java Programming...
# 12
any idea how to add them?its only displaying the even and odd numbers but not adding all of them
Jaywhya at 2007-7-12 18:11:32 > top of Java-index,Java Essentials,Java Programming...
# 13
> any idea how to add them?its only displaying the> even and odd numbers but not adding all of themAnd example of how to add them has long been given in reply #7.
prometheuzza at 2007-7-12 18:11:32 > top of Java-index,Java Essentials,Java Programming...
# 14
its giving me incorrect output
Jaywhya at 2007-7-12 18:11:32 > top of Java-index,Java Essentials,Java Programming...
# 15
> its giving me incorrect outputYou'll have a better chance of giving help if you provide details about what output you're getting and what you expected.
jverda at 2007-7-21 22:02:51 > top of Java-index,Java Essentials,Java Programming...
# 16
> its giving me incorrect outputI didn't mean to juts blindly copy-n-paste what was posted in reply #7. I meant that a correct example of how to add numbers to each other is given in that example. You'll need to look more closely.
prometheuzza at 2007-7-21 22:02:51 > top of Java-index,Java Essentials,Java Programming...
# 17
:D done now im just confuse with my own code darn i can say im laughing to my self :D
Jaywhya at 2007-7-21 22:02:51 > top of Java-index,Java Essentials,Java Programming...