There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Software Development
Tag Cloud
access acer blue screen boot bsod computer crash dell drive driver drivers error ethernet excel freeze gaming google hard drive hardware hdd hdmi internet internet explorer internet explorer pop ups keyboard laptop malware memory missing monitor motherboard network problem ram router security software startup trojan ubuntu 11.10 uninstall usb video virus vista windows windows 7 windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Software & Hardware > Software Development >
Java: Sum of all odd numbers between two numbers?

Reply  
Thread Tools
javalava's Avatar
Junior Member with 6 posts.
 
Join Date: Jan 2012
Experience: Beginner
29-Jan-2012, 12:22 PM #1
Java: Sum of all odd numbers between two numbers?
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);
}
}
}
}
Ent's Avatar
Ent Ent is offline Ent is a Trusted Advisor with special permissions.
Computer Specs
Trusted Advisor with 3,270 posts.
 
Join Date: Apr 2009
Location: United Kingdom
Experience: Intermediate
29-Jan-2012, 01:20 PM #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.
__________________
Quod non mortiferum, fortiorem me facit.
I don't read minds. Please help everyone by answering any questions and reporting on the results of any suggestions.
sepala's Avatar
Computer Specs
Member with 3,822 posts.
 
Join Date: May 2010
Location: Under the LION FLAG(Sri Lanka)
Experience: SD Student (JAVA)
29-Jan-2012, 03:05 PM #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);

}
}
__________________
My country Is My Life
Love your country whatever it is..It is your soul..It is your strength..Never blame to it..
Are you an Asian using TSG? Then join Asian TSG Group
sepala's Avatar
Computer Specs
Member with 3,822 posts.
 
Join Date: May 2010
Location: Under the LION FLAG(Sri Lanka)
Experience: SD Student (JAVA)
31-Jan-2012, 02:24 PM #4
If your problems are solved, please mark them as solved
javalava's Avatar
Junior Member with 6 posts.
 
Join Date: Jan 2012
Experience: Beginner
01-Feb-2012, 08:05 AM #5
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.
sepala's Avatar
Computer Specs
Member with 3,822 posts.
 
Join Date: May 2010
Location: Under the LION FLAG(Sri Lanka)
Experience: SD Student (JAVA)
01-Feb-2012, 12:49 PM #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.
__________________
My country Is My Life
Love your country whatever it is..It is your soul..It is your strength..Never blame to it..
Are you an Asian using TSG? Then join Asian TSG Group
Ent's Avatar
Ent Ent is offline Ent is a Trusted Advisor with special permissions.
Computer Specs
Trusted Advisor with 3,270 posts.
 
Join Date: Apr 2009
Location: United Kingdom
Experience: Intermediate
01-Feb-2012, 01:21 PM #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.
__________________
Quod non mortiferum, fortiorem me facit.
I don't read minds. Please help everyone by answering any questions and reporting on the results of any suggestions.
sepala's Avatar
Computer Specs
Member with 3,822 posts.
 
Join Date: May 2010
Location: Under the LION FLAG(Sri Lanka)
Experience: SD Student (JAVA)
02-Feb-2012, 03:18 PM #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
adamstuart07's Avatar
Member with 3 posts.
 
Join Date: Feb 2012
Experience: Intermediate
22-Feb-2012, 07:10 AM #9
public static void main(String[] args) {
int int1, int2, sum = 0,total=0,num=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 ){
total=sum+num;
num=total;
}
System.out.println("" + num);
}
}
}
Reply

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 11:24 PM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.