How to find entered number is EVEN or ODD without using conditional statement(not using if.. else,if.. , else if..,while, do... while...., for....)

Questions by kapilmandge

Showing Answers 1 - 45 of 45 Answers

srilatha

  • Feb 16th, 2006
 

we can use for loop.

like for(i=1 ;  i%2 != 0 ;i++)

printf("no i is odd",i);

  Was this answer useful?  Yes

kannan

  • Feb 17th, 2006
 

We can use Conditional Operator
size=1>printf(n%2==0,"Even","Odd");

  Was this answer useful?  Yes

Srikanth M.N

  • Feb 19th, 2006
 

can we use conditional operator (i%2==0)?even:oddif there is nay answer pz let me know

  Was this answer useful?  Yes

Sreenath Reddy

  • Feb 22nd, 2006
 

we can find a number is odd or even by a simple programmain(){int a[2],i;a[0]=0; //0--means Even Numbera[1]=1; //1--means Odd numberscanf("%d",&i);printf("%d",a[i%2]);getch();}

  Was this answer useful?  Yes

corwin

  • Feb 25th, 2006
 

please, don't pay attention to synthax, i don't know C, C++ very well, I'm trying to explain the idea by this code. I assume that function i called "mod" takes the fractional part of division and numb is the number to be checked. I assume that you have compiler that supports exception handling.function check_even_odd(int numb) {dbl dummy;try dummy= 1/mod(numb) print('Odd') //i don't know correct operator to do it in Cexcept //catch divide by zero exception here (i don't know the operators) print('Even') //i don't know correct operator to do it in C end;}

  Was this answer useful?  Yes

chandu

  • Mar 2nd, 2006
 

we can use conditional operator ternary.

  Was this answer useful?  Yes

ganapathy

  • Mar 2nd, 2006
 

void main()

{

char a[2][]={"even","odd"};

scanf("%d",&n);

printf("%s",a[n%2]);

}

VishalJais

  • Mar 10th, 2006
 

#include<stdiio.h>

#include<conio.h>

void main()

{

  clrscr();

printf("\n Enter the number");

scanf("%d",&i);

switch(i%2)

{

 case 0:

     printf("\n %d is Even",i);

     break;

case 1:

      printf("\n %d is Odd",i);

      break;

} //End of Switch() 

getch();

}  //End of main()

Prashant

  • Mar 17th, 2006
 

(n&1) ? printf("Odd") : printf("Even") ;

Nagaraju

  • Mar 20th, 2006
 

How to find entered number is EVEN or ODD without using conditional statement(not using if.. else,if.. , else if..,while, do... while...., for....)

Since i used Conditional Operator. It is the simplest Solution.

(i%2==0)?printf("Given Number is Even: %d",i):printf("Odd:%d",i);

bala nagi reddy

  • Mar 22nd, 2006
 

Hi, The solution is very simple.every odd number will have 1 in the least significant bit in the binary format.so the solution is to find the least significant bit and that can be found by right shifting the number by 1 bit and using ternary operation to print the result. Any prob with my solution please let me know by email.Thanks,bala

  Was this answer useful?  Yes

Munish

  • Apr 14th, 2006
 

Every odd number has its least significant digit as 1. so if we check a number for the presence of 1 as the LSB then we can know if its a odd number or not. ANDing gives the solution to our problem 11001& 00001 ------------ 00001------------The result will be one only if last bit is one i.e no is odd.

  Was this answer useful?  Yes

haripanda

  • Apr 17th, 2006
 

as u haveto check thenumber is / even at the entery level so

printf("enterthe number");

scanf("%d",&n);

swich(n%2)

{

case 0:

printf("even");

break;

case 1:

printf("ood");

}

  Was this answer useful?  Yes

Praveen Kumar M

  • May 4th, 2006
 

main()
{
        int n=4;
        printf( (5*(n%2))+"Even\x00Odd");
}

  Was this answer useful?  Yes

veena

  • Jun 15th, 2006
 

main()

{

int a,b;

char even,odd;

printf("enter the no");

scanf("%d",&a);

b=(a%2?"odd":"even");

printf("%s",b);

}

Vinit

  • Jun 25th, 2006
 

Use showbits()

Check the last bit, if it is 1 its Odd else its Even.

  Was this answer useful?  Yes

K N

  • Jun 29th, 2006
 

Here is one moreprintf(num%2==0? "even":"odd");

  Was this answer useful?  Yes

ercans

  • May 12th, 2008
 

void isNumberEven(int Number)
{
    char StringArray[2][10];
    strcpy(&(StringArray[0][0]),"EVEN");
    strcpy(&(StringArray[1][0]),"ODD");

    printf("%d is %s n",Number, &(StringArray[Number%2][0]));

}

  Was this answer useful?  Yes

jintojos

  • May 22nd, 2008
 

// Finding Odd/Even Number Without If Clause
 #include
 #include 
 void main()
   {
         int num,rem;
         printf("Enter The Number :");
         scanf("%d",&num);
         rem=num%2;
         while(rem)
            {
                  printf("Odd Number..");
                  getch();
                  exit(0);
           }
         printf("Even Number....");
         getch();
   }

  Was this answer useful?  Yes

paragmalshe

  • May 29th, 2008
 

int isOdd(int num){
       return (num&1);
}
//logical anding is done with the last bit of the no. all the bits except last //1 are masked. which wud determine whther no. is even or odd.

  Was this answer useful?  Yes

cageordie

  • Jun 17th, 2011
 

    Assuming i contains the number we wish to evaluate...

    char *oddeven[] = {"even", "odd"};
    printf("number is %sn",oddeven[i%2]);

  Was this answer useful?  Yes

shweta

  • Sep 13th, 2014
 

(i%2==0)?even:odd

  Was this answer useful?  Yes

alok kumar

  • Nov 12th, 2014
 

// to check entered number is odd or even without using modulus or for or while or switch etc...//


Code
  1. // by alok kumar

  2. #include<stdiio.h>

  3. #include<conio.h>

  4. void main()

  5. {

  6. int i,j;

  7.   clrscr();


  8. Enter the number");

  9. scanf("%d",&i);

  10. j=i/2;

  11. if(i==j)


  12. Entered number is even");

  13. else


  14. Entered number is odd ");

  15. getch();

  16. }  //End of main()


Code
  1. #include<stdiio.h>

  2. #include<conio.h>

  3. void main()

  4. {

  5. int i,j;

  6.   clrscr();


  7. Enter the number");

  8. scanf("%d",&i);

  9. j=i/2;

  10. if(i==j)


  11. Entered number is even");

  12. else


  13. Entered number is odd ");

  14. getch();

  15. }  //End of main()

  Was this answer useful?  Yes

jbode

  • Nov 14th, 2014
 

See attached snippet. Creates a 2-element array with the strings "even" and "odd". Picks the string based on the result of the % operator (0 for even, 1 for odd), and prints the result. Does not use any sort of conditional statement or expression.

Code
  1. char *even_or_odd[] = {"even", "odd"};

  2. int val;

  3. scanf( "%d", &val );

  4. printf( "%d is %s

  5. ", val, even_or_odd[val % 2] );

  Was this answer useful?  Yes

elayaraja.v

  • Dec 17th, 2014
 

Code
  1. #include<stdio.h>

  2. main(){

  3. int n;

  4. puts("Enter the Number");

  5. Scanf("%d",&n);

  6. (n%2==0)?"Even":"Odd";

  7.  

  8. }

  Was this answer useful?  Yes

Mithun Agarwal

  • Mar 4th, 2015
 

Hello,
I have a small concern. The above snippets is purely based on c programming language. Appreciate it would be great if you can assist me the same snippet using coldfusion components
Regards,
Mithun Agarwal

  Was this answer useful?  Yes

ashik

  • Mar 8th, 2015
 

Even

Code
  1. #include <stdio.h>

  2. int main()

  3. {

  4. int n;

  5. printf ("plz input a number");

  6. scanf ("%d",&n);

  7. if (n%2==0)

  8. {

  9. printf ("EVEN")

  10. }

  11. else

  12. {

  13. printf ("ODD");

  14. }

  15. retrun 0;

  16. }

  Was this answer useful?  Yes

Blinkk

  • Mar 9th, 2015
 

Can you plz explain Printf statement?

  Was this answer useful?  Yes

Muhammad Ali

  • Mar 18th, 2015
 

If we use switch statement then without any loop, if, if..else we can find whether it is even or odd.

  Was this answer useful?  Yes

Vikas

  • Apr 4th, 2015
 

using switch statement this can be achieved using the and operator

  Was this answer useful?  Yes

WASIM

  • Apr 8th, 2015
 

(num%2==0)?printf("Even"):printf("Odd");{/geshibot}

  Was this answer useful?  Yes

Kiran

  • Feb 16th, 2017
 

I dont remember syntax of C
Ill give answer in Java

Code
  1. // find a given number odd or even without any loop, if-else, switch, conditional statement.

  2.                 int number = 41;

  3.                 String[] array = {"Even", "Odd"};

  4.                 System.out.println(array[(number%2)]);

  Was this answer useful?  Yes

Krishna

  • May 30th, 2017
 

Code
  1. #include<iostream>

  2. int main()

  3. {

  4. int x;

  5. cin>>x;

  6. x%2?cout<<"odd"<<endl:cout<<"even"<<endl;

  7. }

  Was this answer useful?  Yes

cageordie

  • Jun 6th, 2017
 

#include
#include
int main()
{
char *oddeven[2] = {"even", "odd"};
int n;
printf("number: ");
fscanf(stdin, "%d", &n);
printf("Hello n is %s
", oddeven[n&1]);
return 0;
}

  Was this answer useful?  Yes

Manoj Sah

  • Aug 21st, 2017
 

Using & operator.

Code
  1. main()

  2. {

  3.         int n;

  4.         printf("Ntr number:");

  5.         scanf("%d",&n);

  6.         if((n & 1 )== 0)

  7.         {

  8.                 printf("Entred number is even:%d",n);

  9.         }

  10.         else

  11.         {

  12.                 printf("Number is odd");

  13.         }

  14.         return 0;

  15. }

  Was this answer useful?  Yes

harikrishna

  • Oct 24th, 2017
 

Please can you explain this "printf( (5*(n%2))+"Evenx00Odd");" logic

  Was this answer useful?  Yes

Chris

  • Oct 28th, 2017
 

n%2 will give you 0 for even and 1 for odd. The string gives you the address of the start of the string. So for even it prints the string starting at the first character and for odd it starts at the 6th.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions