How to determine whether an integer is even or odd without using if else or switch ?

This was the question in Level 1 of the C Programming competition at SRKNEC, Nagpur.

Write a program which accepts input as an integer and prints ODD if it is odd and EVEN if its even.
(Note: You cannot use if else, switch, loops. Only use printf and scanf)

and I couldn't code that.

Any Hint from you ?

Replies

  • Kaustubh Katdare
    Kaustubh Katdare
    Re: How to determine whether an integer is even or odd without using if else or switc

    gaurav.bhorkar
    This was the question in Level 1 of the C Programming competition at SRKNEC, Nagpur.

    Write a program which accepts input as an integer and prints ODD if it is odd and EVEN if its even.
    (Note: You cannot use if else, switch, loops. Only use printf and scanf)

    and I couldn't code that.

    Any Hint from you ?
    This is a simple question. How about trying to write an algorithm? ๐Ÿ˜€
  • sookie
    sookie
    Re: How to determine whether an integer is even or odd without using if else or switc

    Hi Gaurav,

    I guess "Ternary operators" are made to use in such cases only. I would have used ternary operators in that case. Following is my program in Java [ Note: NOT IN 'C'. 'C/C++' users can change it using scanf and printf ]

    package myjava;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    /**
     *
     * @author sookie
     */
    public class EvenOrOddTernaryProgram {
    
        public static void main(String[] args) throws IOException{
            Integer inputNo;
            BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));
            inputNo = Integer.parseInt(bin.readLine());
            System.out.println(inputNo%2==0?"Even number":"Odd number");
        }
    }
    
    Output:
    12
    Even number
    567
    Odd number

    Thanks !
  • Sahithi Pallavi
    Sahithi Pallavi
    Re: How to determine whether an integer is even or odd without using if else or switc

    I think this is a very simple question..! I used conditional operators to write the program.



    #include
    void main()
    {
    int x;
    printf("Enter a number");
    scanf("%d",&x);
    (x%2==0)?printf("even number"): printf("Odd number");
    }


    output :-


    Enter a number
    7
    Odd number

    --> Enter a number
    8
    Even number




    WINNERS DONT DO DIFFERENT THING..THEY DO THINGS DIFFERENTLY..
  • Saandeep Sreerambatla
    Saandeep Sreerambatla
    Re: How to determine whether an integer is even or odd without using if else or switc

    This can be done without using any operators.

    #include
    void main()
    {
    Char Array[0] = "Even";
    Char Array[1] = "Odd";
    int x;
    printf("Enter a number");
    scanf("%d",&x);
    printf("The number is %s", Array[x%2])
    }

    This function gives you the number which is even or odd, Please check for syntax errors and array declarations since I didnt compile this .
    But the basic idea is 0 location is even and 1 is Odd.
    I hope you understood!!
  • gaurav.bhorkar
    gaurav.bhorkar
    Re: How to determine whether an integer is even or odd without using if else or switc

    @sookie
    @sahiti pallavi
    We cannot use ternary operators or any decision control instructions in this question.

    I think English-Sacred's approach is correct. Maybe we can use an array of pointers to strings instead of two separate arrays. This program works when modified in this way:

    #include
    void main()
    {
         char *arr[] = {
                       "Even",
                       "ODD"
                       };
         int x;
         printf("Enter a number");
         scanf("%d",&x);
         printf("The number is %s", arr[x%2]);
         getch ();
    }
  • Sahithi Pallavi
    Sahithi Pallavi
    Re: How to determine whether an integer is even or odd without using if else or switc

    nice work Es..!



    WINNERS DONT DO DIFFERENT THING..THEY DO THINGS DIFFERENTLY..
  • sookie
    sookie
    Re: How to determine whether an integer is even or odd without using if else or switc

    @Gaurav: Cool man, just one question - in what real life scenarios such kind of programming constraints are put up[like not using decision control instructions] and in what all cases such kind of programming is useful? Why programmers are expected to write a program like this? Why they are taught these if..else, switch..ternary and blah blah things? Any ideas you have? Why such things are not removed from the syllabus?

    PS: You can ignore the question if don't have good answers.

    Thanks !
  • Saandeep Sreerambatla
    Saandeep Sreerambatla
    Re: How to determine whether an integer is even or odd without using if else or switc

    My take for this question Sookie --

    The actual scenario in programming is How simple you do the coding , since any program can be done in many ways .
    But the simple method is the best one to understand and to work on!!

    Well all other logics are said because we have to use them in all other programs.

    The simplest way will be the better way to understand anything generally.

    Anyway for the above question solving without use of conditional operators is to get the logic.

    There is a similar question in the same style, Can you swap the numbers without using third variable .

    Anyway we can do it in many ways but the logic is also important .
  • enthudrives
    enthudrives
    Re: How to determine whether an integer is even or odd without using if else or switc

    A great work buddy. Brilliant souls.. ๐Ÿ˜€
  • gaurav.bhorkar
    gaurav.bhorkar
    Re: How to determine whether an integer is even or odd without using if else or switc

    Yes, there are different ways to program and the simplest way is the best. The logic is the main part.
    Anyways, this was a part of a competition and thats why they asked to code it in such a way. But I learned some good things here. Thank You !
  • sookie
    sookie
    Re: How to determine whether an integer is even or odd without using if else or switc

    gaurav.bhorkar
    Yes, there are different ways to program and the simplest way is the best. The logic is the main part.
    Yeah ! though at times the simplest way is not OPTIMUM. Anyway , thanks a ton ๐Ÿ˜
  • Tim1
    Tim1
    Re: How to determine whether an integer is even or odd without using if else or switc

    Simplest answer in C/C++ to test if unsigned integer variable "x" is odd or even:

    isOdd = x & 0x1;
    isEven = ! (x & 0x1);

You are reading an archived discussion.

Related Posts

Somebody plz explain me combustion mechanism in SI and CI engines. Also could anyone tell me what is the effect of air-fuel ratio on the performance of engine?๐Ÿ˜•
I am a final year student in B.E. Electrical & Electronics......... I have decided to do my final year project on Energy conservation.......... What are the possible topics that can...
hey could u plz help me out with projects i could implement in lab view.
Hello friends... I am Jasdeep Singh. I am pursuing my B.Tech in civil engineering from DAVIET, Jalandhar.. I require some help urgently. Actually, we have freshers on 4th September. In...
I give some problem of double linked list program. so please give me a program for implementation of double linked list using c++ language. The functions are insert, delete, search,...