Thread: Odd or even

  1. #16
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Check the return value. If it's true for even, then it's not odd. In other words, basic logic.


    Quzah.
    Hope is the first step on the road to disappointment.

  2. #17
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72
    Are you saying I should put in an if statement? And which "return" are you wanting me to look at?

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int even(int n) // return sum from 1 to n
    {
        return ( n % 2 == 0 );
    }
    Explain to me exactly what this code does, segment by segment. If you can, then explain to me why it is you can't figure out how to tell if something is odd.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #19
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72
    Ok, all that is doing is determining what the remainder is and printing it out. And the reason it is not printing "10 is even" is because I have not told it how to print out even.

  5. #20
    Registered User
    Join Date
    Sep 2009
    Location
    USA
    Posts
    63
    Quote Originally Posted by BB89 View Post
    Ok, did some work but still doesnt workout.

    My output is

    10 is 1
    11 is 0
    .
    .
    .
    .
    19 is 0
    20 is 1

    Code:
    #include <stdio.h>
    
    int even(int n) // return sum from 1 to n
    {
        return ( n % 2 == 0 );
    }
    
    int main (void)
    {
        int i, sum;
    
        for (i = 10; i <= 20 ; i++)
        {
            sum = even(i);
            printf("%d is %d\n", i, sum);
        }
        return 0;
    }
    How do I make it say 10 is even, 11 is odd, etc..?
    i see what you mean

    ok so the even function returns 1 for even number

    and returns 0 for odd number.

    so just put an if loop

    Code:
     printf("%d is %d\n", i, sum);
    sum --> gives either 1 or 0

    best of luck

  6. #21
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72
    Ok, I put in an if statement but it is working but putting out an odd answer.

    it is saying:

    10 is 4even
    11 is 5odd

    Code:
    #include <stdio.h>
    
    int is_even(int n) // return sum from 1 to n
    {
        if(n % 2 == 0)
            printf("odd\n");
        else
            printf("even\n");
    }
    
    int main (void)
    {
        int i, sum;
    
        for (i = 10; i <= 20 ; i++)
        {
            sum = is_even(i);
            printf("%d is %d", i, sum);
        }
        return 0;
    }

  7. #22
    Registered User
    Join Date
    Sep 2009
    Location
    USA
    Posts
    63
    Quote Originally Posted by BB89 View Post
    Ok, I put in an if statement but it is working but putting out an odd answer.

    it is saying:

    10 is 4even
    11 is 5odd

    Code:
    #include <stdio.h>
    
    int is_even(int n) // return sum from 1 to n
    {
        if(n % 2 == 0)
            printf("odd\n");
        else
            printf("even\n");
    }
    
    int main (void)
    {
        int i, sum;
    
        for (i = 10; i <= 20 ; i++)
        {
            sum = is_even(i);
            printf("%d is %d", i, sum);
        }
        return 0;
    }
    ok almost there..but if you do that how will you retun either 1 or 0?.

    try putting the if loop inside the main method ..

    this loop explains by itself.

    Code:
    if(sum==1) 
    {
      print number is even
    }
    else
       number is odd.

  8. #23
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72
    How would I use my function then?

  9. #24
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How can you not be understanding this already? Check to see if mod-2 is 1. If it is ... ta dah ... it's ODD! Otherwise it's even! Now how can that possibly still confuse you?


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #25
    Registered User BB89's Avatar
    Join Date
    Sep 2009
    Location
    Dallas, Texas
    Posts
    72
    Yes, I understand

    This is what I dont understand


    Code:
    10 is 4even
    11 is 5odd
    What is the 4 in front of even and the 5 in front of odd?

  11. #26
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Whoa horsey!!! Stop. Calm down. Now, here is what you need to do. WITHOUT WRITING ANY CODE tell us what you need to do and how you plan to do it. Programming is not about the language you are using. Who cares that you are doing this in C? Do you even know how to get this answer? What would you do if I asked you to PROVE that each number from 10 to 20 was odd or even? How would you do that? Write it down, post it here and let us see that you know what you are doing FIRST. Then, we can help you get your thoughts to code.

    Currently, I don't think you have a handle on the problem.

  12. #27
    Registered User
    Join Date
    Sep 2009
    Location
    USA
    Posts
    63
    THIS IS NOT THE EXACT CODE.

    Quote Originally Posted by BB89 View Post
    Yes, I understand

    This is what I dont understand


    Code:
    10 is 4even
    11 is 5odd
    What is the 4 in front of even and the 5 in front of odd?
    ok here

    Code:
     printf("%d is %d", i, sum);
    delete the 2nd %d and sum

    do this

    Code:
    printf("%d is ", i);
    Last edited by Obelisk; 10-21-2009 at 09:37 PM.

  13. #28
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by Obelisk View Post
    ok here

    Code:
     printf("%d is %d", i, sum);
    delete the 2nd %d and sum

    do this

    Code:
    printf("%d is ", i);
    If you listen to this, then you've done this project incorrectly.

  14. #29
    Registered User
    Join Date
    Sep 2009
    Location
    USA
    Posts
    63
    Quote Originally Posted by Kennedy View Post
    If you listen to this, then you've done this project incorrectly.
    i know but the question he asked was why was the presence of 4even and 5odd i just

    showed him how to remove it.

  15. #30
    Registered User
    Join Date
    Sep 2009
    Location
    USA
    Posts
    63
    your almost there actually, with your permission i can show you the code if you want to

    understand the logic

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. odd function is odd
    By plain in forum C Programming
    Replies: 3
    Last Post: 10-05-2009, 12:01 AM
  2. Odd and even
    By swgh in forum C++ Programming
    Replies: 12
    Last Post: 09-18-2006, 05:56 PM
  3. adding odd numbers only
    By CheyenneWay in forum C++ Programming
    Replies: 12
    Last Post: 05-06-2004, 12:22 AM
  4. new type: even or odd
    By nbo10 in forum C++ Programming
    Replies: 7
    Last Post: 09-05-2003, 11:17 AM
  5. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 05:56 PM