The C challenge
Results 1 to 7 of 7

Thread: The C challenge

  1. #1
    Senior Member PacketThirst's Avatar
    Join Date
    Aug 2004
    Posts
    258

    The C challenge

    Ok folks ..

    I've come across a really neat C problem.Try coding this !



    " A program in C to print whether a given number is even or odd without using any

    conditional statements and loops ! "


    I'm sure someone will do this ! ...

  2. #2
    Code:
    #include <stdio.h>
    
    int main(void)
    {
           int odd;
           int even;
           printf("enter an even number: ");
           scanf("%d",&even);
           printf("\n%d is an even number.",even);
           
           printf("\n\nEnter an odd number: ");
           scanf("%d",&odd);
           printf("\n%d is an odd number.",odd);
           
           return 0;
    }
    problem solved

  3. #3
    Senior Member
    Join Date
    Oct 2001
    Posts
    786

    Did it, Easy ;)

    This is pretty easy once you know how to distinguish it and analyze the problem. I'll even have it show some text like "ODD" and "EVEN" for you...


    Code:
    #include <stdio.h>
    
    int main(void)
    {
         int input;
         char strings[1][5];
         
         strcpy(strings[0], "ODD\0");
         strcpy(strings[1], "EVEN\0");
    
         printf("Give me a number: ");
         scanf("%i", &input);
    
         printf("This is a(n) %s number!", &strings[(input+1)%2]);
    
         getch();
         return 0;
    }

  4. #4
    ok, an serious answer then:

    i'm not sure if this is allowed, but i think it is:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
           
           int number, temp;
    
           printf("Enter a number: ");
           scanf("%d",&number);
    
           temp=number%2;
           switch(temp)
           {
                  case 0:
                          printf("\n%d is an even number",number);
                          break;
                  default:
                          printf("\n%d is an odd number",number);
                          break;
           }
           return EXIT_SUCCESS;
    }

  5. #5
    Senior Member PacketThirst's Avatar
    Join Date
    Aug 2004
    Posts
    258
    You got it right folks !!!! ... This question was actually asked for a job interview here !. The logic is pretty simple and because of the fact that it is so simple, some people never solve it .

  6. #6
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    I don't think I would get that job . I'm guessing it was lepricaun's first and Tim_axe's programs that were right? lepricaun's second had a switch which is a conditional statement, so that's not correct is it? /me works on some code. Peace.

  7. #7
    lepricaun's second had a switch which is a conditional statement, so that's not correct is it?
    i also don't
    think my second is allowed, but the first was more of a joke, since some of these questions are so tricky, that you think it the hard way, while the solution is very simple....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •