Results 1 to 4 of 4

Thread: Problem with Odd/Even number

  1. #1
    Join Date
    Jul 2009
    Posts
    127

    Problem with Odd/Even number

    Hi
    I am new to java and i want to learn how to code in java. i have started creating some small programs. I am trying to code a program that takes a integer and returns odd and even values upto that number. I am posting my code please check it out. Thanks in advance.
    Code:
    public class OddEvenNum
    {
       public static void main(String[] args)
            {
            int i;
            char choice;
            Scanner scn = new Scanner(System.in);
            do
                    {
                    System.out.println("Welcome to odd and even number checker please enter a number");
                    i = scn.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 = scn.next() .charAt(0);
                    }while (choice == 'y' || choice == 'Y');
            }
    }

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: Problem with Odd/Even number

    Hi
    I think including a loop will be better for your program and will display the results more clearly.
    Code:
    for ( int j = 0; j < i; j++ ){
        //check j for even or odd and act accordingly
    }
    If any problem do post back. Feel free to ask queries.

  3. #3
    Join Date
    Jul 2009
    Posts
    127

    Re: Problem with Odd/Even number

    Hi
    Thanks for your reply
    You advice and your part of the program is taken me to one step ahead but the problem is that it will now print the numbers which are amount of times of the integer. ex it will print 3,3 times
    Code:
    public class Test
    {
       public static void main(String[] args)
    	{
    	int i;
    	
    	char choice;
    	scnanner scn = new scnanner(System.in);
    		do
    		{
            	System.out.println("Welcome to odd and even number checker please enter a number");
            	i = scn.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 = scn.next() .charAt(0);
    		}while (choice == 'y' || choice == 'Y');
    	}
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    Re: Problem with Odd/Even number

    Hi
    I think you can try this one. It is simple
    Code:
      public static void main(String[] args) {
            int cu = 0;      
            do {
               cu++;
                if (cu % 2 == 0) {
                    System.out.println(cu + " is even");
                }
                else {
                    System.out.println(cu + " is odd");              
                }
            }
            while(cu != 10);
            
        }
    }

Similar Threads

  1. problem creating Skype to go number.
    By Caryas in forum Technology & Internet
    Replies: 3
    Last Post: 20-06-2011, 08:17 PM
  2. Aero Effects not working after number of attempts to fix the problem
    By arjun rantu in forum Monitor & Video Cards
    Replies: 5
    Last Post: 24-12-2010, 10:27 PM
  3. Problem with C# program which calculates the prime number
    By $Bird$ in forum Software Development
    Replies: 4
    Last Post: 29-11-2010, 12:54 PM
  4. Problem in random number subtraction
    By Linoo in forum Software Development
    Replies: 4
    Last Post: 05-03-2010, 07:37 PM
  5. Problem in random number
    By cyber-noob in forum Software Development
    Replies: 3
    Last Post: 08-12-2009, 01:30 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,486,726.44451 seconds with 16 queries