#JAVA CHALLENGERS#
Write a Java Program to find weather the INPUT no is even or Odd.
Restrictions:-
1)-Nither use % nor %= operators.
2)-Not to use Any String functions.
3)-Not to use Any Bitwise operators.
4)-Loop is also not allowed.
Answers
- Write a program in java to accept a number and find whether the number is odd or even
- Not to use % or %= operators.
- Not to use any string functions.
- Not to use any Bitwise operators.
- Loops not allowed.
import java.util.*;
class EvenOdd
{
public static void main(String s[ ])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter an integer number: ");
int n=sc.nextInt();
// condition to check even....
if(n/2==n/2.0)
System.out.println("Given number is even. ");
else
System.out.println("Given number is odd. ");
} // end of main()
} // end of class.
Enter a number:- 10
Number is Even.
Consider a number, say 10.
Now,
n=10.
Here n/2=10/2=5(integer division)
Also,
n/2.0=10/2.0=5.0
As both of them are same, hence the number is even.
Similarly, consider a number say 5.
n=5.
So,
n/2=5/2=2(integer division)
and
n/2.0=5/2.0=2.5
So,
2 not equals to 2.5,
Hence the number is odd.
Write a program in java to accept a number and find whether the number is odd or even
⋆Restrictions:−
Not to use % or %= operators.
Not to use any string functions.
Not to use any Bitwise operators.
Loops not allowed.
import java.util.*;
class EvenOdd
public static void main(String s[ ])
Scanner sc = new Scanner(System.in);
System.out.print("Enter an integer number: ");
int n=sc.nextInt();
condition to check even....
if(n/2==n/2.0)
System.out.println("Given number is even. ");
else
System.out.println("Given number is odd. ");
} // end of main()
} // end of class.
⋆Output:−
Enter a number:- 10
Number is Even.
Consider a number, say 10.
Now,
n=10.
Here n/2=10/2=5(integer division)
Also,
n/2.0=10/2.0=5.0
As both of them are same, hence the number is even.
Similarly, consider a number say 5.
n=5.
So,
n/2=5/2=2(integer division)
and
n/2.0=5/2.0=2.5
So,
2 not equals to 2.5,
Hence the number is odd.