Well, here's my problem. I have to Count from Num1 to Num2 by input from the user. So when I input let say 1 to 5, it will display 1 3 5. If I do 2 to 6, it would display 3 and 5.

I can't seem to get the evens part. When I input 2 and 10, it inputs at the last number 11, which is exceeding. I also have to sum up all the evens together from num1 to num2. If I input 2 and then 2, it will output a 3, which is suppose to be a 0.

Here is my code. It's Line 25 to 53 i'm working on. The rest is finished right.

import java.util.*;


public class MiscLoops {
	public static void main(String [] args) {
		Scanner console = new Scanner(System.in);

		int num1;
		int num2;
		int number1 = 0;
		int odds = 0;
		int counter = 0;
		int evens = 0;
		int sum = 0;
		int sumOdd = 0;
		int i;
		int squareOdds = 0;
		int count = 1;


		System.out.println("first number must be less than second?");
		num1 = console.nextInt();
		num2 = console.nextInt();

		System.out.println("odd numbers:");
			while(num1 <= num2){
				if (num1 % 2 ==0 )
				{
					evens = num1+1;
					num1 +=2;
					System.out.println(evens);
				}

				else if(num1 % 2 !=0){

				odds = num1;
				num1 +=2;

					System.out.println(odds);
				}
}


		for(num1 = 0; num1 < num2; num1++){
			if(evens % 2 == 0)
			sum =  sum + num1;
			else if(number1 % 2 !=0)
			sum +=number1;


}
		System.out.println("sum of evens: "+ sum);
		System.out.println("squares of 1 to 10");


		for(i = 1; i <=10; i++){
			sum = (i*i);
		System.out.println(i + " " +sum);


	}
		for(num1 = 0; num1 <= num2; num1++){
			if (num1 % 2  != 0)

			squareOdds += num1*num1;

}
System.out.println("Sum of odds squared: " + squareOdds);
		char upper;
		System.out.printf("A to Z:\n");
        for (upper = 'A';  upper <= 'Z';  upper++ ){
System.out.printf("%s",upper);
}


	}
}

Any point in direction appreciated.

One thing jumps out at me:
You get the lower & upper bounds in num1 and num2, but then you keep changing the value of num1 in your loops. This seems very dangerous to me. I suggest you add another variable or two for use as temporary values in your loops and NEVER change the value of num1 or num2. Even if that doesn't fix your immediate problem it will help avoid new ones later.

You stated all number between num1 and num2... why do you increment num1+=2? Its easier to go through all values between num1 and num2 and check which one are odds/even and make calculation depends of number type. It will reduce your code by half. Also increasing by two gives you only odds or evens number which is not all number in sequence. If i misunderstood concept and you really need to check only odds or evens, put a for loop with i+=2 instead of i++ and you will get step by 2 counting.
Also...
Line 30 and 37 should be num1++; (by my previous remark) (if you using your own code, or some other variable that start with num1 value if you are willing to listen @JamesCherrill opinion)
Line 34 should be only "else" because number %2 is either 0 or non 0 there is no alternative.
Also i assume that "evens" and "odds" are counter for even and odd. But in the way you are using it, it seams that you try to keep even/odd values, but you could not do it without array or simple add operation like evens+=num1. Or counting like evens++; depends of need.
Also for loop (line 56-66) is not needed. First in while loop you are checking for odds/even, then in 2 for loop you are checking for odds/even?.
When you check if value is odd or even, calculate sum directly.

Ok now line 44 you stated "If (evens %2==0)" but in line 29 you stated "evens=num1+1"
so according to line 29 evens%2 will NEVER be 0 simple because evens is growing only if you found even number and all even number +1 are odd numbers.
Also evens in your code if FIXED to last even number + 1 value? (line 29). And you are using it in evaluation in line 45?.

while i am on lines 45...

if(evens % 2 == 0)
			sum =  sum + num1;
			else if(number1 % 2 !=0)
			sum +=number1;

this is meaningless... if evens%2==0 increase sum with num1, but if not again increase sum with num1. (i assume that number1 is mistaken for num1 because number1 is initiated to 0 and never used in code. if not that dividing 0 with 2 is also meaningless (number1%2)).

This get me to line 52 when always sum of evens is 0 because of previous loop(l45) and value of evens (l29)... this answers your question :)

Try to keep code simple. If you can use only one loop use it.

Edited 5 Years Ago by monarchmk: n/a

Alright thanks for the replies. I'll be changing it up a bit and editing, and will post later about my progress. Much obliged for helping.

I may not be understanding your requirements correctly... do you mean to say you want to input integers i and j, then count from i to j by... what exactly? Counting odd numbers? Even numbers? Both? Depending on user input?

Your code seems unnecessarily complicated. Rather than trying to find flaws in it, I'm going to suggest you rethink what you're trying to do.

As always, start from pencil and paper.

You're given a starting point and an end point. Let's suppose you want to count by odd numbers, and you know you have an odd starting point. What's the algorithm? It's a simple loop - dead simple, one instruction, or two if you like.

Now suppose you're counting by evens, and you know you have an even starting point. Again, dead simple - same loop.

Okay, suppose you want to count by odds and you have an even starting point. Well, it's the same loop, with an extra decision at the start. Same thing for the contrary case.

Last possibility: you don't mention this one, but what if you want to let the user specify counting by odds or evens? Well, that's another input and another decision.
So at most you have two decisions and a simple loop.

All that extra code you're writing is what's screwing you up. Throw it out and start over.

James is right, never modify your initial user input. We have big fancy machines now with lots of memory, there's no need to save that extra place on the stack.

I have to disagree with monarchmk on the advisability of counting by twos. That's the very definition of counting by odds or evens. Counting by ones and then checking doesn't save you any code at all, and it adds an unnecessary decision in each iteration of the loop. This is horribly wasteful. Any time you can eliminate an if, do it. Never add them if you don't need them.

I have to disagree with monarchmk on the advisability of counting by twos. That's the very definition of counting by odds or evens. Counting by ones and then checking doesn't save you any code at all, and it adds an unnecessary decision in each iteration of the loop. This is horribly wasteful. Any time you can eliminate an if, do it. Never add them if you don't need them.

Also increasing by two gives you only odds or evens number which is not all number in sequence.If i misunderstood concept and you really need to check only odds or evens, put a for loop with i+=2 instead of i++ and you will get step by 2 counting.

i think my quote agrees with your quote. As is in my explanation i advice him to use num++ just to go through every number or if i misunderstood concept he can go with +2...
At the end of code @kay19 displays result of both evens/odds so by that i suppose if displaying of result is not conditional than calculating should not be conditional and should used every number. Problem is with task definition, thats why my answers was dependable from conditions.
And in this case using all numbers in if could reduce code cause he can put here content of two loops, from end of code, that are not needed for separate calculation.

i would release a code but than you will rep me -- :)

Edited 5 Years Ago by monarchmk: n/a

You're right, I will. :)

But I see I failed to read your post correctly. Yes, we agree, you were right the first time. Confusion due, I think, to a poorly-specified problem.

Alright, I finished, everything is working perfectly. Thanks for the replies.

This question has already been answered. Start a new discussion instead.