# need help with this loop

• December 4th, 2011, 07:18 PM
musterdplug12
need help with this loop
Im very new at java and this is the last thing i have to finish before get some sleep. I just cant seem to finish the program. The goal is to make a program where the user inputs a number, then recieves the limit, range and sum of the odd numbers that make up the entered number. I have most of it done, i just cant seem to get the program to print the total sum of the odd numbers

Code :

``` import java.util.Scanner;   class Sumoddjkunitz { public static void main(String[] args) { int user1, div;   //This tells the user what to do System.out.println("This program will tell you the sum, limit, and range of odd numbers between 1 and your chosen number"); System.out.println("Please enter the limit number");   Scanner keyboard = new Scanner(System.in); user1 = keyboard.nextInt();   //spacer below System.out.println(" ");   //limit System.out.print("The Limit of the numbers is "); System.out.print(user1);   //spacer below System.out.println(" ");   //range System.out.print("The range of the numbers is "); System.out.print("1 to "); System.out.print(user1);   //spacer below System.out.println(" ");   //sum of odd numbers and loop System.out.print("The sum of the numbers is ");   sum = 0; for (int j = 1; j <= user1; j++) { if (j % 2 == 1) { sum += i; } }     } }```
• December 4th, 2011, 07:19 PM
musterdplug12
Re: need help with this loop
i never used the div integer, forgot to take it out
• December 4th, 2011, 07:42 PM
Junky
Re: need help with this loop
You calculate the sum but where do you display it?

By the way, you can make your code more efficient if you change j++ to j += 2 and get rid of the if statement.
• December 6th, 2011, 02:49 PM
musterdplug12
Re: need help with this loop
ok so im a day late but ive been busy with work. I now show the sum but its not coming out correctly, whats wrong with my code? Please dont trash me about this, im just learning java.

Code :

``` import java.util.Scanner;   class Sumoddjkunitz { public static void main(String[] args) { int user1, i, sum;   //This tells the user what to do System.out.println("This program will tell you the sum, limit, and range of odd numbers between 1 and your chosen number"); System.out.println("Please enter the limit number");   Scanner keyboard = new Scanner(System.in); user1 = keyboard.nextInt();   //spacer below System.out.println(" ");   //limit System.out.print("The Limit of the numbers is "); System.out.print(user1);   //spacer below System.out.println(" ");   //range System.out.print("The range of the numbers is "); System.out.print("1 to "); System.out.print(user1);   //spacer below System.out.println(" ");   //sum of odd numbers and loop System.out.print("The sum of the numbers is ");   sum = 0; for (int j = 1; j <= user1; j++) { if (j % 2 == 1) { sum += j; System.out.print(sum); } }     } }```
• December 6th, 2011, 02:59 PM
musterdplug12
Re: need help with this loop
nm, figured it out, thanks to the community for existing though!
• December 6th, 2011, 03:58 PM
Junky
Re: need help with this loop
Quote:

Originally Posted by Junky
By the way, you can make your code more efficient if you change j++ to j += 2 and get rid of the if statement.

<sigh>