• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Using "for" to print out odd numbers

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
}
}
}
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!
 
Ranch Hand
Posts: 52
1
MySQL Database Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic