Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How to add all even numbers between 2 and a user input number which is included in the Addition

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to add all even numbers between 2 and a user input number which is included in the Addition

    import java.util.*;
     
    public class SumOfAllEvens {
     
    	public static void main (String[] args)
    	{
     
    		Scanner s = new Scanner (System.in);
     
    		//for (int i=1; i<4; i++){
    		int usernumber; 
    		int sum=0;
     
    		System.out.print ("Please enter an integer: "); // Prompt
    		usernumber = s.nextInt();
     
    		for (int j=2; j<=usernumber; j=j+2){
     
     
     
    		if (usernumber<2)
    			System.out.println ("Error");
    		else
    			System.out.println ("The sum of all evens from 2 to " + usernumber + " (inclusive) is " + (j+usernumber) + " .");
     
    					//sum=sum+j;
     
    		//}
    		}
    	}
    }

    I'm supposed to use a for loop that runs until it reaches the number input by the user, but I'm not sure how to tell the program to add the user's number along with all of the even numbers in between the user input and 2.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to add all even numbers between 2 and a user input number which is included in the Addition

    On each iteration of the loop, what number do you want to add to the sum?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to add all even numbers between 2 and a user input number which is included in the Addition

    Quote Originally Posted by KevinWorkman View Post
    On each iteration of the loop, what number do you want to add to the sum?
    The sum is supposed to be all of the even numbers between 2 and the number which the user inputs. The user input number is also included in the sum. So basically if the user inputs say, 10, the sum would be 2+4+6+8+10.

    If the user inputs an odd number such as 7, it'd be 2+4+6+7.

    The part i'm most confused about is telling the computer what the number is that the user input and not adding it twice if it's an even number.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to add all even numbers between 2 and a user input number which is included in the Addition

    You might want to look into the modulus operator for determining whether a number is even or odd.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to add all even numbers between 2 and a user input number which is included in the Addition

    You can take a loop from 2 to the inputted number and put increment of 2 and then add the value of the loop suppose x add it to s variable for sum and initialize it as 0 s=s+x;

Similar Threads

  1. Consecutive number addition
    By Tedstriker in forum Java Theory & Questions
    Replies: 11
    Last Post: July 15th, 2013, 12:14 PM
  2. How to add to an array with user input
    By miller4103 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 12th, 2013, 10:27 PM
  3. Replies: 5
    Last Post: July 24th, 2012, 12:37 AM
  4. Replies: 13
    Last Post: December 6th, 2011, 02:17 PM
  5. Vector Addition with small numbers giving NaN
    By Zula in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 28th, 2010, 09:29 PM