even or odd

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • melsayid

    even or odd

    The program always shows that the input is odd.

    int main ()
    {
    int n, d;

    printf ("Enter a Number: ");
    scanf ("%d", &n);
    d=1;
    if (d==n)
    {
    printf ("The number is odd\n");
    }
    else if (d < n)
    {
    for (; d<n; d+=2);
    printf ("The number is odd\n");
    }
    else
    {
    printf ("The number is even\n");
    }


    --
    Mustafa El Sayid
  • Richard Bos

    #2
    Re: even or odd

    melsayid <melsayid@gmail .comwrote:
    The program always shows that the input is odd.
    No, it doesn't. Try entering 0.

    You might want to look up the modulo operator, BTW.

    Richard

    Comment

    • Morris Dovey

      #3
      Re: even or odd

      melsayid wrote:
      >
      The program always shows that the input is odd.
      >
      int main ()
      {
      int n, d;
      >
      printf ("Enter a Number: ");
      scanf ("%d", &n);
      d=1;
      if (d==n)
      {
      printf ("The number is odd\n");
      }
      else if (d < n)
      {
      for (; d<n; d+=2);
      printf ("The number is odd\n");
      }
      else
      {
      printf ("The number is even\n");
      }
      >
      This should work for twos complement machines:

      #include <stdio.h>
      int main(void)
      { int n;
      printf ("Enter a Number: ");
      scanf ("%d", &n);
      printf("n is %s\n",(n & 1)?"odd":"even" );
      return 0;
      }

      --
      Morris Dovey
      DeSoto Solar
      DeSoto, Iowa USA

      Comment

      • melsayid

        #4
        Re: even or odd

        On Mar 19, 4:57 pm, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
        melsayid <melsa...@gmail .comwrote:
        The program always shows that the input is odd.
        >
        No, it doesn't. Try entering 0.
        >
        You might want to look up the modulo operator, BTW.
        >
        Richard
        Try entering 2 or any higher even number.

        --
        Mustafa Zaza

        Comment

        • Richard Tobin

          #5
          Re: even or odd

          In article <6927a02c-a8e1-44c8-979d-4c00d949b724@e2 3g2000prf.googl egroups.com>,
          melsayid <melsayid@gmail .comwrote:
          else if (d < n)
          {
          for (; d<n; d+=2);
          printf ("The number is odd\n");
          }
          What is the point of the loop here? You don't test anything after
          it; you just print "The number is odd".

          -- Richard
          --
          :wq

          Comment

          • Richard Tobin

            #6
            Re: even or odd

            In article <47E12D13.7AC26 60B@iedu.com>,
            Morris Dovey <mrdovey@iedu.c omwrote:
            >This should work for twos complement machines:
            >
            >#include <stdio.h>
            >int main(void)
            >{ int n;
            printf ("Enter a Number: ");
            scanf ("%d", &n);
            printf("n is %s\n",(n & 1)?"odd":"even" );
            return 0;
            >}
            It will work for non-negative ints regardless of whether the machine
            uses twos complement.

            -- Richard
            --
            :wq

            Comment

            • melsayid

              #7
              Re: even or odd

              On Mar 19, 5:19 pm, rich...@cogsci. ed.ac.uk (Richard Tobin) wrote:
              In article <6927a02c-a8e1-44c8-979d-4c00d949b...@e2 3g2000prf.googl egroups.com>,
              >
              melsayid <melsa...@gmail .comwrote:
              else if (d < n)
              {
              for (; d<n; d+=2);
              printf ("The number is odd\n");
              }
              >
              What is the point of the loop here? You don't test anything after
              it; you just print "The number is odd".
              >
              -- Richard
              --
              :wq
              I've removed the semicolon and it still doesn't work probably.
              Here is how I thought about it,
              *The input is n
              1-Equals the d with 1
              2-Check if the n is equal to d, if yes it prints that the number is
              odd
              3-If not it checks if the d is less with n, if not it keeps on adding
              +2 to the d till it's equal to the value of the n then output that
              it's odd
              4-Anything else should output that the input is even

              @Morris
              It works for me, but I want to figure out what's wrong with my code.
              Also I don't understand your code, I just started learning C.

              --
              Mustafa El Sayid

              Comment

              • Richard Tobin

                #8
                Re: even or odd

                In article <cc4b6752-b7cb-40de-9025-48b41d39041f@e2 3g2000prf.googl egroups.com>,
                melsayid <melsayid@gmail .comwrote:
                for (; d<n; d+=2);
                printf ("The number is odd\n");
                >3-If not it checks if the d is less with n, if not it keeps on adding
                >+2 to the d till it's equal to the value of the n then output that
                >it's odd
                Suppose n is 8. You add 2 to d until it's greater than or equal to n.
                In this case, that will happen when d is 9. But you still print out
                that n is odd! You don't do anything different depending on whether d
                stopped at n or went one past it.

                -- Richard
                --
                :wq

                Comment

                • Morris Dovey

                  #9
                  Re: even or odd

                  Richard Tobin wrote:
                  >
                  In article <47E12D13.7AC26 60B@iedu.com>,
                  Morris Dovey <mrdovey@iedu.c omwrote:
                  This should work for twos complement machines:

                  #include <stdio.h>
                  int main(void)
                  { int n;
                  printf ("Enter a Number: ");
                  scanf ("%d", &n);
                  printf("n is %s\n",(n & 1)?"odd":"even" );
                  return 0;
                  }
                  >
                  It will work for non-negative ints regardless of whether the machine
                  uses twos complement.
                  You noticed that, eh?

                  :-D

                  --
                  Morris Dovey
                  DeSoto Solar
                  DeSoto, Iowa USA

                  Comment

                  • melsayid

                    #10
                    Re: even or odd

                    On Mar 19, 5:35 pm, rich...@cogsci. ed.ac.uk (Richard Tobin) wrote:
                    In article <cc4b6752-b7cb-40de-9025-48b41d390...@e2 3g2000prf.googl egroups.com>,
                    >
                    melsayid <melsa...@gmail .comwrote:
                    for (; d<n; d+=2);
                    printf ("The number is odd\n");
                    3-If not it checks if the d is less with n, if not it keeps on adding
                    +2 to the d till it's equal to the value of the n then output that
                    it's odd
                    >
                    Suppose n is 8. You add 2 to d until it's greater than or equal to n.
                    In this case, that will happen when d is 9. But you still print out
                    that n is odd! You don't do anything different depending on whether d
                    stopped at n or went one past it.
                    >
                    -- Richard
                    --
                    :wq
                    Ok, it's working now.
                    Thanks.

                    --
                    Mustafa El Sayid

                    Comment

                    • Kenneth Brody

                      #11
                      Re: even or odd

                      melsayid wrote:
                      >
                      The program always shows that the input is odd.
                      Not "always", just "for any positive number". (See also Richard Bos'
                      reply.)
                      int main ()
                      {
                      int n, d;
                      >
                      printf ("Enter a Number: ");
                      scanf ("%d", &n);
                      You should probably check for failure here, but you can get away
                      without it for simple test cases where you are entering the data.
                      d=1;
                      if (d==n)
                      {
                      printf ("The number is odd\n");
                      }
                      else if (d < n)
                      This statement will be true for any "n" greater than 1.
                      {
                      for (; d<n; d+=2);
                      Here, you simply increment d by 2 until it is at least equal to n.
                      Consider, however, how long your program will run if you enter a
                      number such as 2000000000. Also consider that this may never
                      terminate if you enter 2147483647.

                      <pedant>
                      Note my use of the word "may" in the preceding sentence. :-)
                      </pedant>
                      printf ("The number is odd\n");
                      And then unconditionally print "The number is odd".
                      }
                      else
                      Only zero or negative numbers will ever get here.
                      {
                      printf ("The number is even\n");
                      And then unconditionally print "The number is even".
                      }
                      Look up the modulo operator -- % -- as it will suit your needs.

                      --
                      +-------------------------+--------------------+-----------------------+
                      | Kenneth J. Brody | www.hvcomputer.com | #include |
                      | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
                      +-------------------------+--------------------+-----------------------+
                      Don't e-mail me at: <mailto:ThisIsA SpamTrap@gmail. com>

                      Comment

                      • Robbie Hatley

                        #12
                        Re: even or odd


                        "melsayid" sed:
                        The program always shows that the input is odd.
                        No it doesn't.

                        It prints
                        "even" for all numbers < 1
                        "odd" for all numbers >= 1

                        Which, of course, makes no sense whatsoever.
                        int main ()
                        {
                        int n, d;
                        >
                        printf ("Enter a Number: ");
                        scanf ("%d", &n);
                        d=1;
                        if (d==n)
                        {
                        printf ("The number is odd\n");
                        }
                        else if (d < n)
                        {
                        for (; d<n; d+=2);
                        printf ("The number is odd\n");
                        }
                        else
                        {
                        printf ("The number is even\n");
                        }
                        Write it in English first. Here's what you just wrote, translated into
                        plain English:

                        if d is 1 or greater, print "odd"
                        else print "even"

                        Does it make any sense in English? No. So it won't make any sense in C,
                        either!

                        Look-up "modulo" in a math book. Then look-up "modulo" in a C book.
                        (Hint: its the "%" operator.)

                        Then ask yourself, "what do the words 'even' and 'odd' mean?".
                        (Hint: it has to do with whether a number is divisible by 2.)

                        Then ask yourself, "what is the relationship betwen 'divisibility' and
                        'modulo'?"

                        I could easily re-write your program for you to correctly print
                        "odd" or "even", but I won't be a prick and spoil the fun you
                        could have by forcing yourself to learn these things for yourself.

                        --
                        Cheers,
                        Robbie Hatley
                        lonewolf aatt well dott com
                        www dott well dott com slant user slant lonewolf slant


                        Comment

                        • Kenneth Brody

                          #13
                          Re: even or odd

                          melsayid wrote:
                          >
                          On Mar 19, 5:35 pm, rich...@cogsci. ed.ac.uk (Richard Tobin) wrote:
                          In article <cc4b6752-b7cb-40de-9025-48b41d390...@e2 3g2000prf.googl egroups.com>,

                          melsayid <melsa...@gmail .comwrote:
                          for (; d<n; d+=2);
                          printf ("The number is odd\n");
                          >3-If not it checks if the d is less with n, if not it keeps on adding
                          >+2 to the d till it's equal to the value of the n then output that
                          >it's odd
                          Suppose n is 8. You add 2 to d until it's greater than or equal to n.
                          In this case, that will happen when d is 9. But you still print out
                          that n is odd! You don't do anything different depending on whether d
                          stopped at n or went one past it.

                          -- Richard
                          --
                          :wq
                          >
                          Ok, it's working now.
                          Thanks.
                          Could you post your updated "working" code?

                          Have you considered the ramifications of your logic should the
                          person enter 2000000000 (or 2147483647) as the number?

                          --
                          +-------------------------+--------------------+-----------------------+
                          | Kenneth J. Brody | www.hvcomputer.com | #include |
                          | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
                          +-------------------------+--------------------+-----------------------+
                          Don't e-mail me at: <mailto:ThisIsA SpamTrap@gmail. com>


                          Comment

                          • Mahesh

                            #14
                            Re: even or odd

                            On Mar 20, 6:19 pm, Kenneth Brody <kenbr...@spamc op.netwrote:
                            melsayid wrote:
                            >
                            On Mar 19, 5:35 pm, rich...@cogsci. ed.ac.uk (Richard Tobin) wrote:
                            In article <cc4b6752-b7cb-40de-9025-48b41d390...@e2 3g2000prf.googl egroups.com>,
                            >
                            melsayid  <melsa...@gmail .comwrote:
                                     for (; d<n; d+=2);
                                     printf ("The number is odd\n");
                            3-If not it checks if the d is less with n, if not it keeps on adding
                            +2 to the d till it's equal to the value of the n then output that
                            it's odd
                            >
                            Suppose n is 8.  You add 2 to d until it's greater than or equal to n.
                            In this case, that will happen when d is 9.  But you still print out
                            that n is odd!  You don't do anything different depending on whetherd
                            stopped at n or went one past it.
                            >
                            -- Richard
                            --
                            :wq
                            >
                            Ok, it's working now.
                            Thanks.
                            >
                            Could you post your updated "working" code?
                            >
                            Have you considered the ramifications of your logic should the
                            person enter 2000000000 (or 2147483647) as the number?
                            >
                            --
                            +-------------------------+--------------------+-----------------------+
                            | Kenneth J. Brody        |www.hvcomputer .com| #include              |
                            | kenbrody/at\spamcop.net |www.fptech.com    |    <std_disclaimer .h|
                            +-------------------------+--------------------+-----------------------+
                            Don't e-mail me at: <mailto:ThisIsA SpamT...@gmail. com>- Hide quoted text -
                            >
                            - Show quoted text -
                            Lets keep it simple

                            check for the least significant bit of the number, if its 0 then its
                            even else its odd.
                            something like

                            if(num & 0x1)
                            printf(" ODD \n");
                            else
                            printf(" EVEN \n");

                            happy coding

                            Comment

                            • santosh

                              #15
                              Re: even or odd

                              Mahesh wrote:

                              <code to check host endianness>
                              Lets keep it simple
                              >
                              check for the least significant bit of the number, if its 0 then its
                              even else its odd.
                              something like
                              >
                              if(num & 0x1)
                              printf(" ODD \n");
                              else
                              printf(" EVEN \n");
                              >
                              happy coding
                              But this won't detect mixed-endian machines.

                              Comment

                              Working...