|
|
![]() |
| Thread Tools | Search this Thread |
#1
| |||
| |||
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
| |||
| |||
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 } |
#3
| |||
| |||
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
| |||
| |||
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); } } |
![]() |
|
Tags: even, java, number, odd, programming language |
Thread Tools | Search this Thread |
|
![]() | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
problem creating Skype to go number. | Caryas | Technology & Internet | 3 | 20-06-2011 08:17 PM |
Aero Effects not working after number of attempts to fix the problem | arjun rantu | Monitor & Video Cards | 5 | 24-12-2010 10:27 PM |
Problem with C# program which calculates the prime number | $Bird$ | Software Development | 4 | 29-11-2010 12:54 PM |
Problem in random number subtraction | Linoo | Software Development | 4 | 05-03-2010 07:37 PM |
Problem in random number | cyber-noob | Software Development | 3 | 08-12-2009 01:30 PM |