# Help with loops T_T

• 09-02-2012, 01:55 PM
raffs03
Help with loops T_T
I try to make a program that display numbers,the odd number, Sum of all numbers and sum of all odd numbers.
I can display the even and odd numbers but i can't get the sum off all numbers and sum of the odd numbers:@:

Here's my Program:

Code:

```import java.util.Scanner; public class AmpnessToTheMax3 {     public static void main(String[] args) {       Scanner Khen = new Scanner(System.in);         int Num1,Num2,Num3,i = 0;                 System.out.println("Enter a Number: ");       Num1 = Khen.nextInt();       i = Num1;       do{           System.out.print(" " + i);           i--;                         }while(i >=1 && i <=Num1);                     i =Num1;       System.out.println("\n Odd numbers are: ");       do{           if(i%2 ==1)               System.out.print(" "+i);           i--;       }while(i >=1 && i <=Num1);                           } }```
And this My programs output:
Enter a Number:
10
10 9 8 7 6 5 4 3 2 1
Odd numbers are:
9 7 5 3 1

This is the output i want:
Enter a number:
10
10 9 8 7 6 5 4 3 2 1
Odd numbers are:
9 7 5 3 1

Sum of all numbers are : 55
Sum of all odd numbers are: 25

I try this code to get the sum of all numbers but its looping until the end:@:

Code:

```do{           i /=10;           i++;                   System.out.println("\n Sum of all numbers are: ");       }while(Num1 !=0);       System.out.print(i);```

PS: Use Do while loop only because im not familiar with the for and while loops Thanks! :(sweat):

Thanks for your time but i found an exact solution (on Yahoo answers code by briliant_moves)
import java.util.Scanner;

Code:

```public class AmpnessToTheMax3 { public static void main(String[] args) { Scanner Khen = new Scanner(System.in); int Num1, sumAll = 0, sumOdd = 0, i = 0; System.out.println("Enter a Number: "); Num1 = Khen.nextInt(); i = Num1; do { System.out.print(" " + i); sumAll += i; // add i to sumAll i--; } while (i >= 1); System.out.println("\nSum of all numbers is: "+sumAll); i = Num1; System.out.println("\nEven numbers are: "); do{ if (i%2 == 0) { System.out.print(" "+i); } i--; } while (i >= 1); i = Num1; System.out.println("\nOdd numbers are: "); do{ if (i%2 == 1) { System.out.print(" "+i); sumOdd += i; // add i to sumOdd } i--; } while (i >= 1); System.out.println("\nSum of all odd numbers is: "+sumOdd); } }```
One last request is can someone this to while loop method so i can understand how while loop is working because im not familiar with the while loop method :D (and the output of the even numbers must be 10 8 6 4 2 0)

I found a solution on my problem
Here's the code:

Code:

```import java.util.Scanner; public class Trypack{ public static void main(String[] args) { Scanner jom = new Scanner(System.in); int Num, sum=0, even=0,num2=0,num3=0; System.out.print("Enter a Number: "); Num = jom.nextInt(); num2 = Num; num3=num2; while (num2 >=0) { System.out.print(" "+num2); sum +=num2; num2--; } System.out.println("\nEven numbers are: "); while (num3 >= 0) { if (num3%2 == 0) { System.out.print(" "+num3); even=even+num3; num3-=2; } else { num3--; } } System.out.print("\nSum of all numbers is: "+sum); System.out.println("\nSum of all even numbers is: "+even); } }```
• 09-02-2012, 01:57 PM
Fubarable
Re: Help with loops T_T

Also please edit your original post and wrap your code in [code] [/code] tags so that it retains its formatting and is easier to read.
• 09-02-2012, 02:15 PM
raffs03
Re: Help with loops T_T
I need to add all of the numbers (Example: the user input 10 then the loop result is 10 9 8 7 6 5 4 3 2 1 i need to add them all and that is my problem T_T)
same for the sum of odd numbers(Example: the loop result is 9 7 5 3 1 i need to add them all XD)
• 09-02-2012, 03:11 PM
Fubarable
Re: Help with loops T_T
You need to create two int variables, one to hold the sum of all numbers and one to hold the sum for the odd numbers, create them before your loops, and give them names that make sense like sumNumbers and sumOddNumbers. Then in your loops you need to add the appropriate values to these variables. Then when the loops have completed you can display your sums.
• 09-02-2012, 05:30 PM
Yaki
Re: Help with loops T_T
Pilipino ka?
• 09-03-2012, 03:46 PM
bob 6
Re: Help with loops T_T
a Good Programming practice is to NEVER use "do... while" loops unless neccesary. I strongly suggest you learn the while and expecially the for loop, they should be much easier to use.
• 09-03-2012, 04:49 PM
Fubarable
Re: Help with loops T_T
Quote:

Originally Posted by bob 6
a Good Programming practice is to NEVER use "do... while" loops unless neccesary. I strongly suggest you learn the while and expecially the for loop, they should be much easier to use.

I respectfully disagree with this statement. All loops are nothing more than syntactic sugar for a while loop, including a for loop, but the do...while loop has its definite uses and does not deserve to be thrust didactically into the closet.
• 09-04-2012, 01:48 AM
raffs03
Re: Help with loops T_T
For me learning all the loop methods are fun, im moving now to the for and while loop :D
• 09-04-2012, 11:34 AM
hardcoder
Re: Help with loops T_T
I can suggest you to create the Arrays of odd and even numbers from the given or formed list of numbers. The benifit of array is you need not use the list many times also you will have them collected in some container and can access them. But yes there is a disadvantage of memory consumption, if the list is small please go ahead and create array, loop via arrays and achieve the required functionality.
• 09-04-2012, 02:37 PM
DarrylBurke
Re: Help with loops T_T
Quote:

Originally Posted by hardcoder
I can suggest you to create the Arrays of odd and even numbers from the given or formed list of numbers. The benifit of array is you need not use the list many times also you will have them collected in some container and can access them. But yes there is a disadvantage of memory consumption, if the list is small please go ahead and create array, loop via arrays and achieve the required functionality.

Bad idea. Since the count of odd/even numbers can't be known before traversing the numbers, what size would you define for each array?

db
• 09-05-2012, 08:35 AM
hardcoder
Re: Help with loops T_T
I think user will enter then number which will get stored in variable Num, of course in that series half of the nummbers will be odd and rest will be even. I thought this thing I need not to write.... So the array size can be Num/2 + 1 ..... If size is the only problem with my idea then this is the solution....
• 09-25-2012, 04:09 AM
bob 6
Re: Help with loops T_T
Quote:

Originally Posted by Fubarable
I respectfully disagree with this statement. All loops are nothing more than syntactic sugar for a while loop, including a for loop, but the do...while loop has its definite uses and does not deserve to be thrust didactically into the closet.

I respectfully disagree with your disagreement. The reason the do... while loop is bad is because it is awfully hard to read, since the while and the do are separated. The for loop, for instance, is much easier to read and maintain, because all of the counter properties (original value, end value, and increment value) are all bunched into one line. The do... while loop does have its uses though, so use it only when nessesary.
• 09-25-2012, 04:40 AM
DarrylBurke
Re: Help with loops T_T
Quote:

Originally Posted by bob 6
The reason the do... while loop is bad is because it is awfully hard to read, since the while and the do are separated.

So are the if and the else in an if-else construct. If your loop or any other code block is overly long, that code is crying out to be refactored.

Quote:

Originally Posted by bob 6
The do... while loop does have its uses though, so use it only when nessesary.

So you actually agree with this:
Quote:

Originally Posted by Fubarable
the do...while loop has its definite uses and does not deserve to be thrust didactically into the closet.

db
• 09-26-2012, 03:44 AM
bob 6
Re: Help with loops T_T
Quote:

Originally Posted by DarrylBurke
So are the if and the else in an if-else construct. If your loop or any other code block is overly long, that code is crying out to be refactored.

So you actually agree with this:

db

Well, the way I explained is how I learned it.
• 09-26-2012, 04:29 AM
Fubarable
Re: Help with loops T_T
Quote:

Originally Posted by bob 6
Well, the way I explained is how I learned it.

Please understand that what you learn isn't always right, and again, this useful construct should not be thrust *didactically* into the closet as you are doing. There are places where this loop makes much more sense than other loops, and in these situations, it is used by professional coders and passes code reviews. You don't want to be too rigid in your interpretation and use of what you learn in school since not everything you are taught is going to be correct.
• 10-19-2012, 04:43 AM
bob 6
Re: Help with loops T_T
Quote:

Originally Posted by Fubarable
Please understand that what you learn isn't always right, and again, this useful construct should not be thrust *didactically* into the closet as you are doing. There are places where this loop makes much more sense than other loops, and in these situations, it is used by professional coders and passes code reviews. You don't want to be too rigid in your interpretation and use of what you learn in school since not everything you are taught is going to be correct.

:=(: Now I feel bad. The book I learned it from looked official, so I thought it was true. I see that other people have other opinions. I guess I'll have to shut up now.
• 10-19-2012, 08:22 AM
JosAH
Re: Help with loops T_T
For this particular example, the 'best' solution is to use no loops at all; there are simple closed form formulas for the sum of all numbers <= n and the sum of all odd numbers <= n.

kind regards,

Jo