Computer Science, asked by Vyomsingh, 7 months ago

#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

Answered by anindyaadhikari13
9

 \boxed{  \sf Challenge \: accepted}

\star\:\:\:\sf\large\underline\blue{Question:-}

  • Write a program in java to accept a number and find whether the number is odd or even

\star\:\:\:\sf\large\underline\blue{Restrictions:-}

  1. Not to use % or %= operators.
  2. Not to use any string functions.
  3. Not to use any Bitwise operators.
  4. Loops not allowed.

\star\:\:\:\sf\large\underline\blue{Source\:Code:-}

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.

\star\:\:\:\sf\large\underline\blue{Output:-}

Enter a number:- 10

Number is Even.

\star\:\:\:\sf\large\underline\blue{Logic:-}

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.

Answered by Anonymous
2

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.

Similar questions