Hey guys! I'm back with another problem :(
I'm supposed to make a for loop program that will print only EVEN numbers from 77 down to 11. I also need to get their sum and average.
By the way here's the code I did:

public class Loop_For2 {
    public static void main (String[] args) { 
        double sum = 0f; 
        int ctr = 1, ctr2 = 0; 
        for (;ctr<=77; ctr++, ctr2--)  {
            System.out.println(ctr); 
            sum += ctr; 
            ctr++; 
        }
        double ave = (double) sum/ctr2;
        System.out.println ("The sum is: " +sum);
        System.out.println ("The average is: " +ave); 
    }
} 

I'm not supposed to use 'if'. Thanks again guys!

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Please use the

tags when posting code.

First, it is a good practice to initialize the loop variable in the loop declaration an not outside of it.
[CODE=java]public static void main (String[] args) 
{ 
   double sum = 0f; 
   int ctr1 = 1; 
   for (int ctr2 = 0 ; ctr<=77; ctr++, ctr2--) 
   {
      System.out.println(ctr); 
      sum += ctr; 
      ctr++; 
   }
   double ave = (double) sum/ctr2;
   System.out.println ("The sum is: " +sum);
   System.out.println ("The average is: " +ave); 
}

I believe that you get confused by all the variables that you put inside the loop declaration. Let's try to simplify things, and then maybe you will see the result.
First, let's declare the variable counter as the variable that counts the number of numbers we have. This variable will replace ctr. ctr2, which is the loop variable, will be replaced by i.

public static void main (String[] args) 
{ 
   double sum = 0f; 
   int counter = 0; //instead of ctr.
   for (int i = 0 ; i<=77; ++i) //i is instead of ctr2.
   {
      System.out.println(i); 
      sum += i; 
      ++counter;
   }
   double ave = (double) sum/counter;
   System.out.println ("The sum is: " + sum);
   System.out.println ("The average is: " + ave); 
}

Now - can you see the problem with the loop declaration?

for (int i = 0 ; i<=77; ++i)

It means that i starts from 0 to 77, and increases by 1. you want it to go from 77 to 11, and only the even ones. - how would you fix that?
Hint: Going from 77 to 11 only the even numbers, is exactly the same as going from 76 to 12, with decrements of 2.

Edited 6 Years Ago by apines: n/a

I fixed my for loop thanks to your help :). but now I'm working on my while and do-while loops. They both have to print only EVEN numbers from 77 down to 11. I also need to get their sum and average. Here's my code for while loop:

public class Loop_While3 { 
public static void main (String[] args) {
    int ctr=1, ctr2=0, sum=0;
    double ave=0.0D;
    while (ctr<=10) {
        System.out.println(ctr);
        sum=sum+ctr; 
        ctr++;
        ctr2++;
        ctr++; 
    }
    ave = (double)sum/ctr2;
    System.out.println ("The sum is: " +sum);
    System.out.println ("The average is: " +ave); 
}
}

and here's my do-while:

public class Loop_DoWhile2 { 
    public static void main (String[] args) {
    int ctr=1, ctr2=0, sum=0; 
    float ave = 0.0f; 
    do {
        System.out.println(ctr); 
        sum = sum+ctr;
        ctr++;
        ctr2++;
        ctr++; 
    }while (ctr<=10); 
    ave = (float)sum/ctr2; 
    System.out.println ("The sum is: " +sum);
    System.out.println ("The average is: " +ave); 
}
}

Thanks again!

If you want only even number while going through, you could do it like...
- Check whether the incoming number is an even number
-- If it is not an even number starting number (snum) is equal to number+1
-- otherwise, starting number (snum) is equal to number
- Iterate through a loop (Remember, do-while will not check whether or not the incoming maximum number if greater than your starting number)
- Loop condition is that the snum is less than your maximum number
-- add the snum to your sum
-- increase the snum by 2 (use +2)
-- increment your number count by 1 (denominator for average computation)
- The loop is done, so compute average

Hmmm.... Can you post the code? I'm quite new to Java so, I need to see it to get it. Thanks though! :)

After looking at your code, it is much simpler than what I said. Here is the while loop. You need some modification for do-while...

public class Loop_While3 { 
  public static void main (String[] args) {
    int ctr=0, sum=0;
    int number=11, maxNum=77;  // this is what you declare for the loop
    // the 3 lines below could be condensed to...
    // int snum = ((number%2)==0)? number : (number+1);
    int snum=0;
    if ((number%2)==0) { snum = number; }
    else { snum = number+1; }
    double ave=0.0D;

    while (snum<=maxNum) {
      System.out.println(snum);
      sum += snum;
      snum += 2;
      ctr++; 
    }
    ave = (double)sum/ctr;
    System.out.println ("The sum is: " +sum);
    System.out.println ("The average is: " +ave); 
  }
}

Edited 6 Years Ago by Taywin: n/a

I appreciate the help, but I'm trying not to use 'if'. Could you teach me how to fix the program without using 'if'? Thanks again!

Here's a sneaky way to do it: print each number n as (n/2)*2. That'll print all of the even numbers in the range, without an if. Okay, it'll print them twice, but nothing in the assignment said you can't print them twice, did it? :)

Let's try and make a while loop act like a for loop shall we? We will transform the loop:

for(int i = 0; i < 100; ++i)
{
  // do something
}

To a while loop. First, we need a variable to act as our counter (i in the for loop)

int counter = 0; // initialized to 0, just like i in the for loop

Now, we have the termination statement i < 100 - we can put it in the expression section of the while loop. So far:

int counter = 0;
while(counter < 100)
{
   // do something
}

When the for loop finishes an iteration, it goes to the incrementation section and increments i, in our case by 1 each iteration. The while loop does not have an incrementation section, so we will need to put the incrementation in the loop's code block at the end. We get:

int counter = 0;
while(counter < 100)
{
   // do something

   ++counter; //increment the counter, just like ++i in the for loop.
}

And we're done! our loop will go from 0 to 100, and will increment itself by one each iteration, just like the for loop.

Now, since you have the solution to the problem using a for loop, try and transform your loop into a while loop :)

This article has been dead for over six months. Start a new discussion instead.