Writing a function to find even numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • luvnhandfull
    New Member
    • Mar 2008
    • 4

    Writing a function to find even numbers

    Can someone please explaine to me how to write a function program. Using prototypes just throws me.

    Here is my prompt:
    write a function called is _is even that has one input parameter and int called number which is a number the user wants to determine is even or not. The Function returns an integer which has a value of 1 if the number passed is even and has a value of 0 if the number passed is not even.

    I'll paste the mess I came up with below.... Ugh
    btw, i know it says to only write the function, but i'm going for the full program in hopes of better understanding it. My program compiles, but doesn't return anything.

    #include <stdio.h>


    int is_even(int number);

    int main(void)
    {
    int number;
    int answer;

    printf("Enter a number: ");
    scanf("%d", &number);

    answer=is_even( number);

    return;
    }
    int is_even(int number)
    {
    int answer=number%2 ;
    if(answer==0)
    return(1);
    else
    return(0);
    }
  • gsi
    New Member
    • Jul 2007
    • 51

    #2
    Hi,
    Your program indeed return's a garbage to the start up routine , since main() doesn't return a value. Regarding the expected output , the result is returned in to the variable answer in the line,
    answer=is_even( number);

    you have to print out the answer to stdout to view the result, say printf("%d",ans wer);

    Hope this helps,

    Thanks,
    Gsi.

    Comment

    • yomama
      New Member
      • Mar 2008
      • 11

      #3
      I don't understand the problem.

      As written, your function returns to main() 1 if even and 0 if odd. Are you looking for it to do something more?

      Comment

      • luvnhandfull
        New Member
        • Mar 2008
        • 4

        #4
        Originally posted by yomama
        I don't understand the problem.

        As written, your function returns to main() 1 if even and 0 if odd. Are you looking for it to do something more?
        yes, i'm looking for it to print out the 1, or the 0 to the screen. when i run the program, it prompts for a number. I type one in and enter, nothing happens.

        Comment

        • luvnhandfull
          New Member
          • Mar 2008
          • 4

          #5
          Originally posted by gsi
          Hi,
          Your program indeed return's a garbage to the start up routine , since main() doesn't return a value. Regarding the expected output , the result is returned in to the variable answer in the line,
          answer=is_even( number);

          you have to print out the answer to stdout to view the result, say printf("%d",ans wer);

          Hope this helps,

          Thanks,
          Gsi.
          Wow, thanks so much! I added a printf statement right after the
          answer=is_even( number);
          worked like a charm!! Thanks so much.

          That was a question from my prior exam and i couldn't figure it out.

          Comment

          • yomama
            New Member
            • Mar 2008
            • 11

            #6
            I see.

            Something definitely happens; the value (1 or 0) is indeed stored in answer. But that's all that happens. If you want to see that value printed to your screen, you need a subsequent printf() to print the value stored in answer. Hope that helps.

            Comment

            • luvnhandfull
              New Member
              • Mar 2008
              • 4

              #7
              Originally posted by yomama
              I see.

              Something definitely happens; the value (1 or 0) is indeed stored in answer. But that's all that happens. If you want to see that value printed to your screen, you need a subsequent printf() to print the value stored in answer. Hope that helps.
              it did! Thanks for the quick response.... i'm yanking my hair out trying to think of all the things i couldn't do on the last test, incase they come up again on the next one.

              Comment

              Working...