I need help please, I want to write a program that requests and reads two integers on one line representing a range, the program should output the sun of all even numbers in the range.
if anyone can help please do, thank you.

Recommended Answers

All 16 Replies

- 2 integers in 1 line: There must be separator char, so use split()!
- A simple for loop...
- Condition inside the for loop for even number: mod2=0; &000000001b=0...
- Sun is with capital, I guess you want the sum: init a variable then inside the deepest condition where everything must be already fine add them up: int i=0; i+=j;

OO Overkill just for fun:
- Write itterator by one.
- Write filter wrapper that filters out odd numbers.
:)

Math solution:
- Find the highest and lowest even number
- number of numbers: (a-b)/2+1 (+1 for double closed interval)
- Average: (a+b)/2 with each being even a/2+b/2 works with the same # bits to protect from overflow
- sum=length*average

can you show to me the java codes in its use?

can you show to me the java codes in its use?

Omg! Can you pay me? ... Forget it. You don't want to be a programmer! :Jedi handwave:

Being a programmer means you replace the "I have to write code" thingy with the "I want to write code myself" thingy. Ye, I know, universities make you believe that it means sucking 5 years and paying for it. But no, sorry, that only gives a certificate of a programmer.

hi ibrahem..i think these hint may be useful to you..
if u wish u can get 2 integers from keyboard by using this stmt..here u r getting number 1 and 2 from key board.in this prog,n1,n2 will be taken as string to convert a (n1,n2)string to integer we are using integer.parseint statement.here u can give input as integer format.

class ip1
{
public static void main(String a[]) throws IOException
{
DataInputStream in=new DataInputStream(System.in);
String str;
int s,n1,n2;
//getting number1
System.out.println("enter number1:");
str=in.readLine();
n1=Integer.parseInt(str);
//getting number2
System.out.println("enter number2:");
str=in.readLine();
n2=Integer.parseInt(str);
}}

@sathya_s
1. We want people to learn and not just copy and paste somebody's solution as their own work.
2. You are ignoring naming convention. Better to read this document on basic Oracle/Sun naming conventions
3. Throwing exception from main method YAK :ooh:

not like that it is just an outline to them..even i suffered a lot by not getting a solution from others..if we help like this 2 othersthey can also can improve their knowledge by updating the codings and implementing their own..

Not correct. There are one to many students that cannot start even simple program and are begging on forums for anything they can submit.
If however somebody comes here and say I have problem and here is what I tried, I see no problem in helping

Thank you for your help.

Do not forget to reference it in your report before you submit it...

Do not forget to reference it in your report before you submit it...

I have no idea why I was so amused by this!

/* Try to execute this program if there is any issue please let me know*/

import java.io.*;

class AllEvenNum{
  public static void main(String[] args) {
    try{
      BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Enter number : ");
      int num1 = Integer.parseInt(br1.readLine());
      System.out.println("Enter number : ");
      int num2 = Integer.parseInt(br1.readLine());
      System.out.println("Even Numbers:");
      for (int i=num1;i<=num2 ; i++){
        if(i%2==0 ){
          System.out.print(i+",");
        }
      }
        System.out.println("The Sum of even numers is:"+i);
    }
    catch(Exception e){}
    
  }
}

@Lourdupinto
1.What is purpose of the for loop in your example?
2.How do you now you triggered exception if you didn't do anything in catch statement. Even little like e.printStackTrace(); would be better then empty catch

@Akill10 your reply doesn't make sense.

not like that it is just an outline to them..even i suffered a lot by not getting a solution from others..if we help like this 2 othersthey can also can improve their knowledge by updating the codings and implementing their own..

Well, first by writing a code for someone, you waste his and your time: These programs go right out of the window at the moment they are finished, and he will get the next assignment and obviously fail later on.
Then we give help for his own solution, and more help if he cannot implement it. But not trying to do so and asking another question, I must think he don't even need the solution, but only the thing he'll get in exchange for it. For me that's just so not cool... :S

The purpose of this for loop is to start from the first number and end with the second number.
/* try to execute this program if there is any issues please let me know */

import java.io.*;
class num
{
	public static void main(String[] args)throws IOException 
	{
		BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Enter number : ");
		int num1 = Integer.parseInt(br1.readLine());
		System.out.println("Enter number : ");
		int num2 = Integer.parseInt(br1.readLine());
		System.out.println("Even Numbers are:");
		int sum=0;
		for (int i=num1;i<=num2;i++)
		{
			if(i%2==0 )
			{
				System.out.println(i);
				sum=sum+i;
			}
		}
		System.out.println("The Sum of even numers is:"+sum);
	}
}

@Akill10 your reply doesn't make sense.

Do you really think he will reference sources of information when he hands in his work? It was funny to me because I thought you were being sarcastic :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.