1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

C programming

Discussion in 'C' started by vignesh1988i, Sep 23, 2009.

  1. vignesh1988i

    vignesh1988i Banned

    Joined:
    Sep 19, 2009
    Messages:
    72
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Chennai
    write a C program , to find whether a number is ODD or EVEN without using Control structures , looping, arrays, functions , operators(namely relational, arithmetic , logical )??????????:):):):):):)
     
  2. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    OK, we can't use relational, arithmetic, logical operators, but we can use bit-wise and operator.

    Code:
    if(A&1) {printf("%d is odd\n", A);}
    else      {printf("%d is even\n", A);}
    
     
  3. vignesh1988i

    vignesh1988i Banned

    Joined:
    Sep 19, 2009
    Messages:
    72
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Chennai
    but control structures(if... else) also should not be used...... but ur logic is right....
    try that!!!!!:)
     
  4. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    We can always replace the if...else with the conditional operator <condition> ? <true case> : <false case> .
    That way, the code becomes :
    Code:
    (A&1) ? printf("%d is odd\n", A) : printf("%d is even\n", A);
     
  5. vignesh1988i

    vignesh1988i Banned

    Joined:
    Sep 19, 2009
    Messages:
    72
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Chennai
    hmmmm :)then i ll put my question , in this way... there is a way to print without this conditional operator also.... try:)

    thank u
     
  6. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    Then I would do it this way ;)

    Code:
    char Result[2][5] = {"EVEN", "ODD"};
    
    printf("%d is %s\n", A, Result[A&1]);
    
     
  7. vignesh1988i

    vignesh1988i Banned

    Joined:
    Sep 19, 2009
    Messages:
    72
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Chennai
    hmmmm :)..in my question i already said without using arrays toooo u should do it!!!!!!!:):)
     
  8. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Code:
    printf("1 for odd, 0 for even -> %d \n",A&1);
    
    If that still doesn't answer, please copy and paste the ORIGINAL question, not your summary of what you think it says.
     
  9. vignesh1988i

    vignesh1988i Banned

    Joined:
    Sep 19, 2009
    Messages:
    72
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Chennai
    hmmmmmm this is the right answer..... :)
     
  10. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    My mistake :p

    Nice.
     

Share This Page