Thread: Need a bit of help. New to C programming

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    2

    Need a bit of help. New to C programming

    Hello everyone!

    I'm new to the world of programming, and need some advice.

    I am taking a C programming class that is needed to complete my degree, but my teacher is not doing a great job at teaching us what he should.

    What I need help with is writing a simple program that will ask a user for a number, and then will spit a response back ut stating if the number is even, odd, or prime.

    Does anyone have any ideas on how I would... structure... I guess the word is.. the code or do I use formulas or what do I do!

    I can actually write a simple code that says hi my name is whatever.. but that's about it.

    Any help or advice would be great. Thanks!

  2. #2
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    I don't know how to test for prime/even/odd numbers... use some formula in the if-statements?

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This will get you started. Work out what you need in words on paper. Then break it down into logical steps to get the job done.

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

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    46
    Getting code handed to you probably isn't the best way to learn but here you go. The code may have bugs so don't blame me if you get an F. (N.B. This code will say that zero is even.)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main(void){
      int number;
      int primeflag=1;
      int i;
      int temp;
      
      printf("please enter a number: ");
      scanf("%i", &number);
      printf("you entered %d\n", number);
      
      if(abs(number)!=number){
        number=-number;
      }
      
      /*method one*/
      if (number%2==0.0){
        printf("the number is even\n");
      } else {
        for (i=3;i<number;i++){
            if (number%i==0.0){
                    printf("the number is odd\n");
                    primeflag=0;
                    break;
            }
        }
        if (primeflag==1){
            printf("the number is prime\n");
        }
      }
    
      /*method two: a bit fancier*/
      if (number%2==0.0){
        printf("the number is even\n");
      } else {
        temp = (int)ceil(sqrt(number));
        for (i=3;i<=temp;i++){
            if (number%i==0.0){
                    printf("the number is odd\n");
                    break;
            }
        }
        if (i>temp){
            printf("the number is prime\n");
        }
      }
    
      return 0;
    }

  5. #5
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    The modulo operator % does not return a floating point value as you imply by writing 0.0.
    From the ANSI C99 Standard:
    The operands of the % operator shall have integer type.
    [...] the result of the % operator is the remainder. [...] if the value of the second operand is zero, the behavior is undefined.
    Code:
    number%2==0

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    38
    The easiest and fastest way of checking for an even number is to look at the binary form of the numbers:

    0000 : 0 'even'
    0001 : 1 odd
    0010 : 2 even
    0011 : 3 odd
    0100 : 4 even
    0101 : 5 odd
    0110 : 6 even

    a pattern should be recognizable (look at the least significant bit).

    When looking for prime numbers, 2 is the only even prime number, so if your number is even but not 2, it is not prime. Don't bother dividing your number (modulus-ing it) by an even number since even numbers only divide 'evenly' (with no remainder) into even numbers which are not prime. So start your divisor at 3 and increment by 2 every iteration of your loop.

    Also, the largest value that will divide into a specific number 'evenly' (with no remainder) is 1/2 of that number:

    200 -> 1/2 of 200 is 100, no value larger than 100 will divide evenly into 200 since 200/100 = 2 which is the smallest whole number above 1.

    So start your divisor at 3, increment by 2 for every loop iteration, and end when your divisor is greater than 1/2 of the number in question.

    [addition]
    After thinking a minute, I realized you don't even have to go that far. You could stop looking when your divisor is greater than 1/3 of the number in question, since a divisor which is 1/2 of the number would mean the number was even to begin with and thus not prime.
    There was in an above post what seemed to be a limit of stopping when the divisor was greater than the square root of the number. This is possible, but I'm not certain that it is correct. I'll have to do some pondering to make sure it doesn't miss any primes.
    [/addition]

    [further addition]
    After pondering for a moment more, I do believe that is correct. Take the number 187 for example.

    The square root of 187 is 13.6747943312
    Since we are dealing with integers, we want to step up to the next whole integer (14).
    13.6747 yadda yadda is the largest number that can be multiplied to itself to get 187, in other words:
    13.6747 * 13.6747 = 187
    So if one of the multipliers is increased, the other has to decrease in order to get 187 as the answer. And the decreased multiplier will therefore be less than 13.6747 which is the square root of 187.
    The square root of 187 is not a whole number, so it doesn't help (that could be another short cut though: if the square root is a whole number then it is not prime, though with floating point math, this may not be an accurate enough test.) 187 is evenly divisible by 17 though, which is greater than the square root which means there _has_ to be a whole number smaller than the square root which will divide 187 as well.

    Hmm, I learn something new every day.

    [/further addition]
    Last edited by Rutabega; 12-19-2003 at 10:15 AM.

  7. #7
    Registered User
    Join Date
    Dec 2003
    Posts
    14
    I think everyone is make a bigger deal of this than needs be. You shouldn't have to use fancy code for something simple as this. I'm half tempted to post at 30 line program here here, but I don't want to do this person's homework for him.

    Hint1: use the modulus operator (%) If n % 2 == 0 then n is even, if not it's odd. If it's even it's impossible for the number to be prime unless the number is 2.

    Hint2: Use a for loop to modulus every number less than the input number. If 0 was never found (except on 1) the number's gotta be prime.

    [edit]
    If I remember correctly from my math classes, when talking about even, odd, and prime numbers it only refers to integers. Not point decimals, so there is no need to even consider using a floating point. After all, an even number is a number than can be evenly divided by 2. Therefor 5 is not an even number although technically 5/2 = 2.5.
    [/edit]
    Last edited by LittleJohn; 12-19-2003 at 04:45 PM.

  8. #8
    Registered User
    Join Date
    Dec 2003
    Posts
    2
    Wow! Thank you all for the help. This was deffinently something that I needed.

    I appreciate everything.

    I will take everything into account, learn what I can, then try to create a program of my own coding. I might post back here if I need more help, but you guys have been the best!

    Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bit manipulation
    By Neo1 in forum C++ Programming
    Replies: 8
    Last Post: 03-24-2008, 11:53 AM
  2. porting application from 32 bit to 64 bit error
    By gandalf_bar in forum Linux Programming
    Replies: 1
    Last Post: 09-14-2005, 09:20 AM
  3. Bit processing in C
    By eliomancini in forum C Programming
    Replies: 8
    Last Post: 06-07-2005, 10:54 AM
  4. Bitwise Operators - Setting and Retreiving a Bit
    By Spono in forum C Programming
    Replies: 2
    Last Post: 11-04-2003, 02:09 PM
  5. Bit Manipulation Questions
    By CPPNewbie in forum C++ Programming
    Replies: 7
    Last Post: 08-12-2003, 02:17 PM