Tech Support Guy banner
Status
Not open for further replies.

Java: Sum of all odd numbers between two numbers?

10K views 7 replies 3 participants last post by  sepala 
#1 ·
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 ·
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 ·
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);

}
}
 
#6 ·
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 ·
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 ·
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 :)
 
Status
Not open for further replies.
You have insufficient privileges to reply here.
Top