DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2

Hybrid View

  1. #1
    Join Date
    Jan 2005
    Posts
    1

    help with problem

    Design and implement an application that reads an integer value and prints the //sum of all even integers between 2 and the input value, inclusive. Print an error message //if the input value is less than 2. Prompt accordingly.

    Right now for the following problem i have this:

    import java.util.Scanner;

    public class Multiples
    {
    //-----------------------------------------------------------------
    // Prints multiples of a user-specified number up to a user-
    // specified limit.
    //-----------------------------------------------------------------
    public static void main (String[] args)
    {
    final int PER_LINE = 1;
    int value, limit, mult, count = 0;

    Scanner scan = new Scanner (System.in);

    System.out.print ("Enter a value: ");
    value = scan.nextInt();


    limit = scan.nextInt();
    mult = scan.nextInt();

    System.out.println ();
    System.out.println ("The sum of the even numbers between 2 and " + mult + " (inclusive) are:");

    for (mult = value; mult <= limit; mult+= value)
    {
    System.out.print (mult + "\t");
    count++;
    if (count % PER_LINE == 0)
    System.out.println();
    }


    }
    }

    as of right now all i can do is enter the value, hit return andnothing happens.

    how can i after entering the value, it adds up all the even numbers between 2 and the value entered?

  2. #2
    Join Date
    May 2004
    Location
    Durham, UK
    Posts
    174
    Hi,

    I'm not really sure exactly what all of the variables where for, but based on the initial question here is a solution.

    Code:
    
    package test;
    
    import java.util.Scanner;
    
    public class Main
    {
        //-----------------------------------------------------------------
        // Prints multiples of a user-specified number up to a user-
        // specified limit.
        //-----------------------------------------------------------------
        public static void main(String[] args)
        {
            int limit = 0;
            int sum   = 0;
            
            Scanner scan = new Scanner(System.in);
            
            System.out.print("Enter a Limit: ");
            limit = scan.nextInt();
            
            System.out.println();
            System.out.println("The sum of the even numbers between 2 and " + limit + " (inclusive) are:");
            
            for (int count = 1; count <= limit; count++)
            {
                // Only add if even
                
                if((count & 1)!= 1)
                {
                    sum+=count;
                    System.out.println(count);
                }
            }
            System.out.println("The sum is " + sum);
            
        }
    }
    run it using java test/Main from an command window

    Hope this helps
    Cheers
    Graham

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links