Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Help With Odd/Even number program

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help With Odd/Even number program

    Hey guys i'm new to java and im trying to teach myself java to help with my university course. Im trying to create a program that takes in a integer and returns the odd and even values upto that number. Here is the code i have at the moment and i would appreciate if someone could explain to me where im going thanks in advance.

     
    import java.util.*;
     
    public class OddEvenNum
    {
       public static void main(String[] args)
     
            {
            int i;
     
            char choice;
            Scanner sc = new Scanner(System.in);
     
            do
                    {
                    System.out.println("Welcome to odd and even number checker please enter a number");
                    i = sc.nextInt();
     
     
     
                    if(i%2 == 0; i++)
                            {
                            System.out.println("Even number is :" + i);
                            }
                            else
                           {
                            System.out.println("Odd number is :" + i);
                            }
     
     
                    System.out.println("Would you like to try another number? :");
                    choice = sc.next() .charAt(0);
     
     
     
     
     
                    }while (choice == 'y' || choice == 'Y');
            }
    }


  2. #2
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Help With Odd/Even number program

    hmm looks like your program satisfies what you need.


    so what do you want to know about your program?

  3. #3
    Junior Member
    Join Date
    Nov 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help With Odd/Even number program

    Basically it takes in the integer then only returns the first odd or even number i want it to display all the odd and even numbers for example:
    Int 10
    odd number is 1
    even number is 2
    odd number is 3
    even number is 4
    all the way to 10 etc

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Help With Odd/Even number program

    You need to include a loop

    for ( int j = 0; j < i; j++ ){
        //check j for even or odd and act accordingly
    }

  5. #5
    Junior Member
    Join Date
    Nov 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help With Odd/Even number program

    thats got me one step further but it will now print the number the amount of times of the integer e.g. it prints 5, 5 times

    import java.util.*;
     
    public class Test
    {
       public static void main(String[] args)
     
    	{
    	int i;
     
    	char choice;
    	Scanner sc = new Scanner(System.in);
     
    	do
    		{
            	System.out.println("Welcome to odd and even number checker please enter a number");
            	i = sc.nextInt();
     
    		for(int j = 0; j < i; j++)
    		{
     
    		if(i%2 == 0)
    			{
    			System.out.println("Even number is :" + i);
    			}
    			else
    			{
    			System.out.println("Odd number is :" + i);
    			}
     
    		}
    		System.out.println("Would you like to try another number? :");
    		choice = sc.next() .charAt(0);
     
     
     
     
     
    		}while (choice == 'y' || choice == 'Y');
    	}
    }

  6. #6
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Help With Odd/Even number program

    something like this ?

       public static void main(String[] args) {
     
            int count = 0;
     
            do {
     
                count++;
     
                if (count % 2 == 0) {
     
                    System.out.println(count + " is even");
                }
                else {
     
                    System.out.println(count + " is odd");
     
                }
            }
            while(count != 10);
     
        }
    }

  7. #7
    Junior Member
    Join Date
    Nov 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help With Odd/Even number program

    ye only i want it to display all the odd numbers or even numbers depending on the value i enter

  8. #8
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Help With Odd/Even number program

    this one?
     public static void main(String[] args) throws IOException {
     
            int count = 0;
     
            System.out.print("Enter The Max Value: ");
            int max = Integer.parseInt(br.readLine());
     
            System.out.print("Which Value Do You Want To Display Even Or Odd?");
            String answer = br.readLine();
     
            do {
     
                count++;
     
                if (answer.equalsIgnoreCase("even")) {
     
                    if (count % 2 == 0) {
     
                        System.out.println(count);
                    }
                }
                else  if (answer.equalsIgnoreCase("odd")) {
     
                    if (count % 2 == 1) {
     
                        System.out.println(count);
                    }
                }
            }
            while (count != max);
        }
    }

Similar Threads

  1. Replies: 5
    Last Post: April 22nd, 2013, 07:27 AM
  2. Reverse Number
    By java1 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 28th, 2009, 10:19 AM
  3. check if a number is an integer
    By rsala004 in forum Java Theory & Questions
    Replies: 3
    Last Post: August 15th, 2009, 03:51 PM
  4. [SOLVED] How to string a decimal number in Java?
    By Lizard in forum Loops & Control Statements
    Replies: 6
    Last Post: May 14th, 2009, 03:59 PM
  5. [SOLVED] Random number method implementation
    By big_c in forum Java Theory & Questions
    Replies: 2
    Last Post: April 15th, 2009, 01:10 PM