This program counts the sum of odd numbers between two given numbers.
SumOdds.java
import java.util.Scanner; public class SumOdds { public void run() { Scanner reader = new Scanner(System.in); int lowLimit, upLimit, even, sum=0; System.out.println("Enter lower and upper limit. The sum of odd numbers will be counted between the limits."); lowLimit=reader.nextInt(); upLimit=reader.nextInt(); reader.close(); //swap places without an extra variable (example: low=3 & up=1) if(lowLimit>upLimit) { lowLimit=lowLimit+upLimit;//3->3+1=4 upLimit=lowLimit-upLimit;//1->4-1=3 lowLimit=lowLimit-upLimit;//4->4-3=1 } while(lowLimit<=upLimit) { even=lowLimit%2; if(even!=0) { //check if not even sum=sum+lowLimit; } lowLimit++; } System.out.println("The sum of the odd numbers is "+sum+"."); } public static void main(String[] args) { SumOdds program = new SumOdds(); program.run(); } }