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.

Java: Sum of all odd numbers between two numbers?

Discussion in 'Software Development' started by javalava, Jan 29, 2012.

Thread Status:
Not open for further replies.
Advertisement
  1. javalava

    javalava Thread Starter

    Joined:
    Jan 25, 2012
    Messages:
    7
    Hi, so far i have managed to make my program so that it prints out the odd numbers between two inputted numbers but i need it to print the sum of them?
    This is what i have:

    public static void main(String[] args) {
    int int1, int2, sum = 0;
    Scanner scan = new Scanner(System.in);
    System.out.println("Please enter two integers: " );
    int1 = scan.nextInt();
    int2 = scan.nextInt();
    for (sum = int1; sum <= int2; sum++){
    if ( sum %2 != 0 ){
    System.out.println("" + sum);
    }
    }
    }
    }
     
  2. Ent

    Ent Trusted Advisor

    Joined:
    Apr 11, 2009
    Messages:
    5,467
    First Name:
    Josiah
    There are two ways to do this. The first, which may be easier, is to create a new variable and add sum to it every time you print out a number.

    A second alternative, which may be more efficient if you aren't already using such a loop, is to look at a bit of mathematics and calculate it. You'd need to look at arithmetic progressions.

    By the way, while sum++ adds one to the value of sum, you can put any statement in that section. sum += 2 adds two to sum each time. Clearly all odd numbers are 2 apart, so your code may be more efficient if you found the highest and lowest odd value, and then counted in twos. It's just a suggestion though, your code works fine at present.
     
  3. sepala

    sepala

    Joined:
    May 20, 2010
    Messages:
    4,038
    Before we go to the sum of the code, lets have a look at the issues in the existing one.

    1. If the first int is odd and GREATER THAN the second and second is even number, then your program will not print the ODD NUMBER. Why? Because of the condition you have passed in the for loop

    2. It is VERY BAD to convert anything to String by using "". There are two methods for this purpose, toString() and String.valueOf(Object);

    I have corrected the code and did some new things for you to learn. Have a look


    Code:
    import java.io.*;
    import java.util.*;//Vector is inside this package..Full import is java.util.Vector;
    
    public class Test
    {
       public static void main(String[] args) {
    int int1, int2, odd = 0,sum=0;
    Scanner scan = new Scanner(System.in);
    System.out.println("Please enter two integers: " );
    int1 = scan.nextInt();
    int2 = scan.nextInt();
    
    Vector<Object>v = new Vector<Object>(); //Vector is a COLLECTION. This is abslolate and replaced by ArrayList. Only difference is this is having less performance.
    v.add(int1);//Adding to vector position 0
    v.add(int2);//Adding to vector position 1
    
    for(int i=0;i<v.size();i++)//Looping through the vector
    {
         odd = Integer.parseInt(v.elementAt(i).toString());//Getting the vector item and converting to string, and then to int
         
         if(odd%2 != 0)
         {
             System.out.println(String.valueOf("ODD NUMBERS ARE: "+odd));//Converting int to string
             
             sum = sum+odd;//Getting the sum
            
             
         }
         
          
    }
    System.out.println("The sum is: "+sum);
    
    }
    }
    
     
  4. sepala

    sepala

    Joined:
    May 20, 2010
    Messages:
    4,038
    If your problems are solved, please mark them as solved
     
  5. javalava

    javalava Thread Starter

    Joined:
    Jan 25, 2012
    Messages:
    7
    it is not solved as of yet as the help given to me is not what i have learnt etc. i am a beginner and dont understand what some of the functions they have said are.
     
  6. sepala

    sepala

    Joined:
    May 20, 2010
    Messages:
    4,038
    I understand.

    What you don't understand? Vector? If yes, use an String Array. But the case is you have to give the size of the array. I am sure you have learnt about arrays :)

    If you didn't understand anything, never stay silence. Ask what you don't understand, then we can clear it out. Now, tell me whether you understood the array work.
     
  7. Ent

    Ent Trusted Advisor

    Joined:
    Apr 11, 2009
    Messages:
    5,467
    First Name:
    Josiah
    Sepala, your code probably introduces a number of new concepts, and it would be much simpler to work on javalava's code directly.

    Code:
    public static void main(String[] args) {
            int int1, int2, sum = 0;
    int oddTotal = 0; /* Records the total of the odd numbers. */ 
            Scanner scan = new Scanner(System.in);
            System.out.println("Please enter two integers: " );
            int1 = scan.nextInt();
            int2 = scan.nextInt();
          for (sum = int1; sum <= int2; sum++){
              if ( sum %2 != 0 ){
                  System.out.println("" + sum);  
    oddTotal += sum; /* Adds "sum" to "oddTotal " */
              }
          }
        }
                  System.out.println("" + oddTotal );  
    }
    As Sepala says, there are various problems with that code. However it should work, and you can work your way through the problems as he mentions them.
     
  8. sepala

    sepala

    Joined:
    May 20, 2010
    Messages:
    4,038
    Yes, if you are not worrying about the issues I showed, the code given by Ent is enough. When you get better experience and knowledge, you will be able to solve the other issues :)
     
  9. Sponsor

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!

Loading...
Thread Status:
Not open for further replies.

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

  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