• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using "for" to print out odd numbers  RSS feed

 
Kasha Blair
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Help! I am extremely new to Java & am having trouble with my code. I am required to use "for" to print out odd numbers between ANY two numbers (whether even or odd) that are input by the user (the numbers are inclusive). There also has to be three columns that print out the numbers, but I'm having a hard time understanding how to create columns. Here is my code so far:

public class Hw3_2
{
public static void main (String[] args)
{
Scanner input = new Scanner (System.in);

int firstnum;
int secondnum;
int count;

System.out.print ("Enter first integer: ");
firstnum = input.nextInt();

System.out.print ("Enter second integer: ");
secondnum = input.nextInt();

// Figure out if the firstnum is odd or even using an if statement
{
if (firstnum % 2 == 0)
{
for (count = firstnum; count <= secondnum ; count += 1 , count += 2); // I'm not sure of the best way to handle an even integer
{
System.out.println ( count += 2);
}
}
else
{
for (count = firstnum; count <= secondnum; count += 2); // If firstnum is an odd number
{
System.out.println (count += 2);
}
}
System.out.println ("List of odd numbers from " + firstnum + " to " + secondnum + "is " + count); // this is where I have issues with creating the columns
}
}
}
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, break the problem down into small chunks. various things you need to do:

print numbers
get input from users
check if a number is odd or even
loop over a range of numbers
print three numbers on a line

These become lego bricks you use to build your program. If any of these are too complicated - like printing three numbers on a line - break it down further. What you need to do for that would be something like

print a number
print a newline
count how many numbers have been printed
reset how many numbers have been printed
figure out if something has happened three times.

again, these become lego bricks you use to build the last lego brick in my first list...

you only try and do one at a time, and make sure it works, before you do the next.
 
Kasha Blair
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:First, break the problem down into small chunks. various things you need to do:

print numbers
get input from users
check if a number is odd or even
loop over a range of numbers
print three numbers on a line

These become lego bricks you use to build your program. If any of these are too complicated - like printing three numbers on a line - break it down further. What you need to do for that would be something like

print a number
print a newline
count how many numbers have been printed
reset how many numbers have been printed
figure out if something has happened three times.

again, these become lego bricks you use to build the last lego brick in my first list...

you only try and do one at a time, and make sure it works, before you do the next.


Thank you very much for your guidance! Unfortunately I ran out of time to turn in my assignment. It's a very frustrating course since the instructor creates assignments that require knowledge of concepts we have not yet learned. I will continue to work on this one though, since I truly want to become great at this some day! Ha ha.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kasha Blair wrote:It's a very frustrating course since the instructor creates assignments that require knowledge of concepts we have not yet learned.

That's what it's like coding in the real world, too. Your boss says "Do this project", and off you go to figure out what to do, and figure out what you need to learn so you can do it!!!
 
Stephen Black
Ranch Hand
Posts: 52
1
Chrome MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The bitwise operator can solve the problem
if ((number & 1)==1)
{
// It's odd
}
In this example the number 3 in the top row is compared to the number 3 using the bitwise operator &
So the rules are that we make a third number from comparing the two numbers and
only if both of the numbers have an on (1) bit will the third number be allowed to have an on (1) bit in that same place
00000011 The number 3 in binary
00000001 The number 1 in binary
00000001 Result is the binary representation of 1 proving that 3 is odd
Therefore if 3 & 1 ==1 it means that 3 is an odd number let's try it again with 4 to prove my point
00000100 The number 4 in binary
00000001 The number 1 in binary
00000000 Result is the binary representation of 0 proving that 4 is even
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephen Black wrote:The bitwise operator can solve the problem
if ((number & 1)==1)
{
// It's odd
}

You can do that, but it's a method that will be less familiar to most people, especially beginners. I'd suggest if (number % 2 == 1), assuming you aren't trying to micro-optimise (which most of the time you shouldn't be).
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:
Stephen Black wrote:The bitwise operator can solve the problem
if ((number & 1)==1)

You can do that, but it's a method that will be less familiar to most people, especially beginners. I'd suggest if (number % 2 == 1)


Not only that, but if you want to generalize "even/odd" (which really means "divisible by 2 / not divisible by 2") to other cases, such as "divisible by 3 / not divisible by 3", then if you're using the % operator as Matthew suggested, it's a trivial change.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!