Thread: Trying to write a program in C

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    4

    Trying to write a program in C

    Im trying to write a program using C that reads an integer and then determines wheather it is odd or even.

    I have to use the if-else structure.

    Any help will be apprectiated

  2. #2
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    sounds like homework, i'll give ya a hint . All even numbers are divisible by 2.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  3. #3
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    here's the logic.......
    if a number divided by 2 gives remainder 0..it is even else it is odd.....you can make use of % operator to find remainder......try it and if you are stuck then post the code....someone will help

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    4
    It is homework.

    Im not looking for someone to post the code.

    Just a hint in the right direction.

    Ya I think the % operater will work.
    Thanks

  5. #5
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    1. use scanf
    2. get the number and use the % operator
    3. if it's an even number else print the odd number

  6. #6
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    here's some psuedocode

    prompt user for number
    input number
    if the number has a remainder of 0 when divided by 2
    the number is even
    otherwise the number is odd

    Just type up the right!
    Don't quote me on that... ...seriously

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    4
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main() 
    {
           int a, b;
    
      printf("print a number:\n");
    
      scanf("%d",&a);             /* Enter number here */
    
      b=a%2;
    
      if(b!=0)                                   /*if number divided by 2 and there is a remainder then number is odd*/
    
      printf("this number is odd:\n");
    
      else
    
      printf("this number is even:\n");       /* prints wheather number is odd or even*/
    
    
         return 0;
    }
    Hot or Not?

  8. #8
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    Just a suggestion, don't put everything inside the main function. Split it up, say getInput and print etc.

  9. #9
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by Deadlocked
    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main() 
    {
           int a, b;
    
      printf("print a number:\n");
    
      scanf("%d",&a);             /* Enter number here */
    
      b=a%2;
    
      if(b!=0)                                   /*if number divided by 2 and there is a remainder then number is odd*/
    
      printf("this number is odd:\n");
    
      else
    
      printf("this number is even:\n");       /* prints wheather number is odd or even*/
    
    
         return 0;
    }
    Hot or Not?

    Well I won't comment on hot or not, but I would say that you don't really need to #include math.h for this version of your program to work. Also, you don't need the extra variable b. You can evalute a % 2 as an argument to printf().

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    
            int input;
    
            printf("\n\nEnter a number: ");
            fflush(stdout);
    
            if ((scanf("%d", &input)) != 1) {
                    puts("\nError: call to scanf() failed\n");
                    return 1;
            }
    
            printf("The number %d is %seven.\n", input,
                   ((input % 2) == 0) ? "" : "not ");
                 
            return 0;
    }
    When you get into some more complicated programs, you are likely going to find that you will want a more robust means of getting input from the user than a call to scanf(). If the user does not enter an int (as scanf() is expecting in this case) then the call to scanf() fails. At the very least, if you must use scanf() check the return value as I demonstrated. Later, you can do something like read the user input into a buffer with fgets() and then grab your number with sscanf() or the like.

    Anyway it looks not too bad overall.

    ~/
    Last edited by kermit; 10-26-2005 at 04:37 PM.

  10. #10
    Registered User
    Join Date
    Oct 2005
    Posts
    4
    Thanks for the suggestion but Id to do it my way.

    That level of C>>>>>>>>>>>>> my level of C

  11. #11
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    That level of C isnt hard, he has simply added error checking by checking to see wether or not an input has been received. And the:

    Code:
    ((input % 2) == 0) ? "" : "not ")
    Is basicaly an if statement:

    if input%2 == 0 then it would print nothing, if it != 0 then it would print not.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie needs help..
    By xpress urself in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2007, 07:22 PM
  2. how could i write this program: cat < apa | wc | wc > bepa
    By strugglingman in forum C Programming
    Replies: 2
    Last Post: 04-26-2006, 04:40 PM
  3. Is there another way to write this program?
    By agentxx04 in forum C Programming
    Replies: 1
    Last Post: 11-23-2004, 09:28 PM
  4. Replies: 1
    Last Post: 10-13-2004, 12:15 PM
  5. Challenge to write a program
    By Twisted.alice in forum A Brief History of Cprogramming.com
    Replies: 40
    Last Post: 05-15-2003, 12:00 PM