displaying odds only

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • casscass
    New Member
    • Feb 2007
    • 1

    displaying odds only

    hi, im having trouble with a java program. i need to know how to display only the odd numbers using a simple while loop.

    the user has to enter a number greater than or equal to 1

    for example, i prompt the user to enter a number.

    if the number the user enters is '6' , i want to display 1, 3, and 5 (all the odds before i get to the value the user got in)

    if the number the user enters is '13', i want to display 1, 3, 5, 7, 9, 11, and 13.

    can anyone help me with this?
  • JagPeram
    New Member
    • Jan 2007
    • 13

    #2
    try this friend,

    public class oddNumberDispla y
    {
    public static void main(String[] args)
    {
    int num = 10; /* enter the number here */
    for(int i=1;i<=num;i++)
    {
    if((i%2)!=0)
    {
    System.out.prin tln(i);
    }
    }
    }
    }

    Comment

    • rengaraj
      New Member
      • Jan 2007
      • 168

      #3
      did u got the answer ???

      Comment

      Working...