1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Java - for loop count range problem

Discussion in 'Software Development' started by tjamnz, Jan 8, 2013.

Thread Status:
Not open for further replies.
  1. tjamnz

    tjamnz Thread Starter

    Joined:
    Jun 15, 2004
    Messages:
    775
    Hello... I'm trying to count the total of all even numbers between the given range in Java.

    I'm using a for loop, but am not getting the desired output.

    // Code here

    import java.util.Scanner;
    public class SumEveryOtherNum {

    public static void main (String[] args){

    int num, sum = 0;
    Scanner scan = new Scanner (System.in);
    System.out.println ("Enter a number greater than 1: ");
    num = scan.nextInt();

    for (int count = 2; count <= num; count+=2){
    sum+=count;
    if (num < 1)
    System.out.println ("Number entered not valid. Try again");
    else
    System.out.println("All even numbers added =" + (sum));
    }

    }

    }

    /*

    If i enter the value 10 at the prompt, my output looks like this.
    For some reason i figured "sum+=count" would add each number looped, and just output 30.
    It prints this output instead:

    Enter a number greater than 1:
    10

    output:
    All even numbers added =2
    All even numbers added =6
    All even numbers added =12
    All even numbers added =20
    All even numbers added =30

    Im trying to count the values, and for the result to only print the value of 30 such as:

    All even numbers added = 30


    Thanks for any ideas ! :D

    */
     
  2. tjamnz

    tjamnz Thread Starter

    Joined:
    Jun 15, 2004
    Messages:
    775
    import java.util.Scanner;
    public class AnotherTest {


    public static void main(String[] args) {

    Scanner scan = new Scanner (System.in);

    int sum = 0;
    int upperLimit;
    System.out.println("Enter an integer (Output will add numbers in between 2 and upperLimit): ");
    upperLimit = scan.nextInt();

    for (int current = 2; current <= upperLimit; current += 2)
    sum += current;

    System.out.println("Sum of even numbers between 2 and " + upperLimit + " inclusive is: " + sum);
    // TODO Auto-generated method stub

    }

    }

    // removed the if else statements from prev version
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1084331

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice