Even/Odd Program in C

Discussion in 'Quiz | Puzzles | Mathematics' started by techno_ishant, Dec 1, 2009.

  1. techno_ishant

    techno_ishant
    Expand Collapse
    Newbie

    write an program in C to find whether a given input number is EVEN or ODD
    Condition :- do not use conditional statement(if,while) ,ternary operator(?)
    only use printf and scanf statement and arithmetic operator .
    Example: input : Enter number- 22
    output : Even
     
  2. cooltwins

    cooltwins
    Expand Collapse
    Maniac

    Engineering Discipline:
    Electronics
    diclaimer:i am no computer genius. i just did c during 1st year and here is my idea...:)
    #include<stdio.h>
    void main()
    {
    printf("\n enter the number:");
    scanf("%d",&n);
    int l=0;
    l=n%2;
    for(;l=0; )
    printf("\n EVEN");
    for(;l=1; )
    printf("\n ODD");
    }

    this is prone to errors....
     
  3. Manish Goyal

    Manish Goyal
    Expand Collapse
    Star

    Engineering Discipline:
    Computer Science
    #include<iostream.h>
    #include<conio.h>
    char *a[2]={"even","odd"};
    int no;
    cin>>no;
    cout<<a[no%2]<<"no";
    }
    You can convert it into c language
     
  4. raj87verma88

    raj87verma88
    Expand Collapse
    Enthusiast

    Engineering Discipline:
    Mechanical
    @Techno: Do you mind sharing your own attempt at solving this question? It looks as if you want us to solve some assignment question for you. We do not tolerate this here.

    @Cooltwin and Goyal: We do not provide free lunches. We help others but do not spoon feed the answers to them.
     
  5. techno_ishant

    techno_ishant
    Expand Collapse
    Newbie

    ya i'll raj... i have given this program in national level technical event held in over institute
    and no one have solve this program
    so here it is:-

    main()
    {
    char *str[2]={"EVEN","ODD"};
    int num;
    printf("Enter Number :-");
    scanf("%d",&num);
    printf("%s",str[num%2]);
    }
     
  6. mohammad yusuf

    mohammad yusuf
    Expand Collapse
    Newbie

    Engineering Discipline:
    Computer Science
    ishan sir i have tried this code but didn't find the solution it has error in scanf statement
    #include<stdio.h>
    void main()
    {
    printf("\n enter the number:");
    scanf("%d",&n);
    int l=0;
    l=n%2;
    for(;l=0; )
    printf("\n EVEN");
    for(;l=1; )
    printf("\n ODD");
    }
     
  7. suyash

    suyash
    Expand Collapse
    Apprentice

    Engineering Discipline:
    Metallurgical
    Umm... I am not too sure that no one was able to solve this program if it was indeed a national level technical event :)
     
  8. suyash

    suyash
    Expand Collapse
    Apprentice

    Engineering Discipline:
    Metallurgical
    I guess the html encoding used in CE forums have goofed up the ampersand operator in the scanf statement. You should have at least edited the code before running it just like that by copy paste :)
    P.S.:- after editing the scanf statement, you might get one more error where the code is trying to divide the int variable by 2. :)
     
Draft saved Draft deleted

Share This Page