English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I m having trouble to find even, odd, and prime numbers with this question...

In this program you must use nested loops to print out the following details for each number from
3 to 100:

• Is it even or odd?
• Is it prime?

A number is prime if it is only divisible by itself and 1. So 3 is a prime, because it is only
divisible by 3 and 1. But 4 is not a prime, because it is divisible by 2.

2006-11-17 21:20:48 · 5 answers · asked by tbwada 1 in Computers & Internet Programming & Design

This is what i have rightnow, and i try to do a [even if], but it does not work at it all. Only thing it does is list the even, and prime number..

#include

int main()

{
int i;

printf("Printing information about numbers from 3-100...\n");

for( i = 3; i <= 100; i++){
printf("%d\n", i);
}
return 0;
}

2006-11-17 21:35:15 · update #1

5 answers

Well, I can tell you - you don't have enough in your program to answer the question. I won't do your homework for you - but this is what you need.
1) Remember that when divide two integers you ALWAYS get an integer. FOr example 5/2 is 2!
2) So, use this fact to test for even. In other words take the number and divide by 2. Now take the answer and multiply by 2. If that result is the original number you have an even.

Example:
7/2 = 3. 3*2 = 6, 6 is not 7 so odd.
12/2 = 6 6*2 =12. 12 is original number so even.
For primes all you really need to go up to is the square root of the number. If you have not found a factor by then you won't!
Example...
49....1, 7, 49 7 is the square root of 49. You cannot think of a number who has BOTH factors larger than the square root of that number. One of them HAS to be smaller than the square root.
Example:
100. Square root is 10. You cannot think of two numbers larger than 10 (call them a, b). Such that a*b = 100. They both must be 10 or ONE of them has to be smaller than 10!

So, now hopefully you can do your homework!

Get busy! :D

2006-11-17 21:43:00 · answer #1 · answered by The Ultimate Nerd 4 · 0 0

Use the Modulus(%) operator to determine if a number is even or odd. This is a convenient method to use if you use "2" as the second argument because it will return a boolean value.
eg.

if(N % 2) {
// Even
} else {
// Odd
}

The easiest way to test if a number is prime is to create a dynamic array to store a list of prime numbers. To check a number for primality, you only need to check all the prime numbers up to the square root of N.

eg.
N = 50
sqrt( N) = 7.0710...

You would only need to compare it against a set of prime numbers up to 7. Since you are starting at the number 3, you will need to initialise the first cell of an array of prime numbers with the number 2. You'll want to use the modulus operator again to compare a given number to each prime.

I'll leave it up to you to figure out a way to implement this. It's rather intuitive if you give it some thought. You'll need two FOR loops, one nested in the other, to implement this.

Ram's code will also work, however it is inefficient. Here is a better version.

#include
#include

int main() {

int i,j,prime;

for(i=3;i<=20;i++) {
if(i % 2 == 0) {
printf("%d IS AN EVEN NUMBER.\n",i);
}
else {
prime = 1;
for(j=2;j <= sqrt(i);j++) {
if(i % j == 0) {
prime = 0;
}
}
if( prime) {
printf("%d IS AN ODD NUMBER AND PRIME.\n",i);
}
else {
printf("%d IS AN ODD NUMBER.\n",i);
}
}
}

return 0;
}

2006-11-18 10:45:31 · answer #2 · answered by Kookiemon 6 · 0 0

I don't know what u are exactly asking for. but here, i give you the complete C program for the question you posed. The program is:

#include
main()
{
int i,j;
for(i=3;i<=100;i++) /*loop starts at 3*/
{
if(i%2!=0) /*checks only odd no.s for primes*/
{
for(j=1;j if(j%i==0) break;
if(j==i)
printf("\n%d IS AN ODD NUMBER AND ALSO A PRIME NUMBER",i);
else printf("%d IS AN ODD NUMBER BUT NOT A PRIME NUMBER",i);
}
else if(i%2==0) printf("%d IS AN EVEN NUMBER",i);
}
}

That's all and that simple it is. bye.............

2006-11-17 21:48:49 · answer #3 · answered by Ram 1 · 0 0

So what's the problem exactly? Or do you just want us to do your homework for you?

2006-11-17 21:23:33 · answer #4 · answered by Anonymous · 0 0

what do u want ? program...

2006-11-17 23:38:24 · answer #5 · answered by rama 2 · 0 0

fedest.com, questions and answers