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

1. ## 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. I don't know how to test for prime/even/odd numbers... use some formula in the if-statements?

3. 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.

4. 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;

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. 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. 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.

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.

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.

7. 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.

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]

8. 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!