Hi , I'm a new learner of Java
I start study Java since 2 weeks

one of my friends challenge me to type program that show the number which I enter , It's even or odd .

could you help me about the main things ,,,

Hi , I'm a new learner of Java
I start study Java since 2 weeks

one of my friends challenge me to type program that show the number which I enter , It's even or odd .

could you help me about the main things ,,,

Hi mate, im not totally sure what you mean. So when the user types in a number it is then checked to see if it is either odd or even? If so then here's the code:

import java.util.Scanner;

public class oddEven {
	
	public static void main(String [] args)
	{
		Scanner in = new Scanner(System.in);
		int num;
		
		System.out.println("Please enter a number: ");
		num = in.nextInt();
	
		checkNum(num);
		
	}
	
	public static void checkNum(int userNum)
	{
		if(userNum % 2 == 0 ){			
/* The % operator returns the remainder of two numbers, so if 2 % 2 equals 0 then no remainder
*/			
			System.out.println("The number "+userNum+" is even");
			}
		
		else{
			System.out.println("The number "+userNum+" is odd");
			
		}
	}

}

This is a pretty basic way of doing it. Hope this helps!

Hi , I'm a new learner of Java
I start study Java since 2 weeks

one of my friends challenge me to type program that show the number which I enter , It's even or odd .

could you help me about the main things ,,,

// For any Query's Contact me....... :)
import java.io.BufferedReader;
import java.io.InputStreamReader;
class Evenorodd{
public static void main(String args[]){
int a=0;
System.out.println("Enter a Number:");
try{
BufferedReader av = new BufferedReader(new InputStreamReader(System.in));
a = Integer.parseInt(av.readLine());
}
catch(Exception e)
{
System.out.println("ERROR");
}
if(a%2==0)
{
System.out.println("No is Even");
}
else
{
System.out.println("No is Odd");
}
}
}

Edited 6 Years Ago by Sahil Goel: n/a

thanks for all ,
but I need exactly to add the number which I need to test it in the code .
and when I prees RUN the outputs show me it's odd or even.
I repeat my thanks and I hope to understand me :)

thanks for all ,
but I need exactly to add the number which I need to test it in the code .
and when I prees RUN the outputs show me it's odd or even.
I repeat my thanks and I hope to understand me :)

Im not entirely sure what you mean. The program that I wrote takes in a number entered by the user and checks to see if its odd or even. Do you mean you need to just call the program and substitute in your value directly to the method?

Please don't give working code, give advice instead, if possible. To the OP, keep in mind that when running a program with command line arguments, those arguments are stored into the args array. So args[0] would be the first command line argument, args[1] the second, etc. Combined with the code you were given above you should be able to figure it out.

Do you mean you need to just call the program and substitute in your value directly to the method?

yes :)

Edited 6 Years Ago by memo1: n/a

You have been given the code that does what you want. You have been told the '%' remainder operator:

if(userNum % 2 == 0 ){			
			System.out.println("The number "+userNum+" is even");
			}
		else{
			System.out.println("The number "+userNum+" is odd");
		}

The challenge was for you to write the code, not us. Since you have been doing java for 2 weeks and you have been told how to find if a number is even or odd, then you should be able to write the program that you want on your own.

Post your code, which is something we haven't seen yet, and ask questions about it.
Although I doubt we can say anything more than what has already been said since you have been practically given the answer.

Edited 6 Years Ago by javaAddict: n/a

This article has been dead for over six months. Start a new discussion instead.