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.
Code :
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');
}
}
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?
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
Re: Help With Odd/Even number program
You need to include a loop
Code :
for ( int j = 0; j < i; j++ ){
//check j for even or odd and act accordingly
}
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
Code :
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');
}
}
Re: Help With Odd/Even number program
something like this ?
Code :
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);
}
}
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
Re: Help With Odd/Even number program
this one?
Code :
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);
}
}