Results 1 to 17 of 17
Like Tree3Likes
  • 2 Post By Fubarable
  • 1 Post By DarrylBurke

Thread: Help with loops T_T

  1. #1
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    41
    Rep Power
    0

    Default 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:

    Java 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

    Java Code:
    do{
              i /=10;
              i++;
              
             System.out.println("\n Sum of all numbers are: ");
          }while(Num1 !=0);
           System.out.print(i);
    Please help me my head is looping

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

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

    Java 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:

    Java 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);
    }
    }
    Last edited by raffs03; 09-02-2012 at 04:21 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with loops T_T

    Where are you adding values to your sum variables?

    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.

  3. #3
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    41
    Rep Power
    0

    Default 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)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  5. #5
    Yaki is offline Member
    Join Date
    Sep 2012
    Posts
    16
    Rep Power
    0

    Default Re: Help with loops T_T

    Pilipino ka?

  6. #6
    bob 6 is offline Java Rookie
    Join Date
    Aug 2012
    Posts
    20
    Rep Power
    0

    Default 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.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with loops T_T

    Quote Originally Posted by bob 6 View Post
    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.
    joroscof and DarrylBurke like this.

  8. #8
    raffs03 is offline Member
    Join Date
    Sep 2012
    Posts
    41
    Rep Power
    0

    Default 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

  9. #9
    hardcoder is offline Member
    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    Default 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.

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Help with loops T_T

    Quote Originally Posted by hardcoder View Post
    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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #11
    hardcoder is offline Member
    Join Date
    Sep 2012
    Posts
    7
    Rep Power
    0

    Default 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....

  12. #12
    bob 6 is offline Java Rookie
    Join Date
    Aug 2012
    Posts
    20
    Rep Power
    0

    Default Re: Help with loops T_T

    Quote Originally Posted by Fubarable View Post
    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.

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Help with loops T_T

    Quote Originally Posted by bob 6 View Post
    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 View Post
    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 View Post
    the do...while loop has its definite uses and does not deserve to be thrust didactically into the closet.
    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  14. #14
    bob 6 is offline Java Rookie
    Join Date
    Aug 2012
    Posts
    20
    Rep Power
    0

    Default Re: Help with loops T_T

    Quote Originally Posted by DarrylBurke View Post
    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.

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with loops T_T

    Quote Originally Posted by bob 6 View Post
    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.

  16. #16
    bob 6 is offline Java Rookie
    Join Date
    Aug 2012
    Posts
    20
    Rep Power
    0

    Unhappy Re: Help with loops T_T

    Quote Originally Posted by Fubarable View Post
    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.

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,335
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Help with loops!
    By jrelvi23 in forum New To Java
    Replies: 9
    Last Post: 09-04-2012, 01:36 PM
  2. Replies: 20
    Last Post: 03-22-2011, 04:02 AM
  3. need some help with loops!
    By Chewart in forum New To Java
    Replies: 2
    Last Post: 12-03-2009, 11:32 PM
  4. when should we use loops
    By shahemaan in forum New To Java
    Replies: 1
    Last Post: 10-31-2009, 01:38 AM
  5. While Loops, need a bit of help.
    By Keno777 in forum New To Java
    Replies: 7
    Last Post: 10-30-2009, 08:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •