Thursday, July 9, 2009

C Programming?

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.

C Programming?
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
Reply:what do u want ? program...
Reply: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%26lt;stdio.h%26gt;


main()


{


int i,j;


for(i=3;i%26lt;=100;i++) /*loop starts at 3*/


{


if(i%2!=0) /*checks only odd no.s for primes*/


{


for(j=1;j%26lt;i;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.............
Reply:So what's the problem exactly? Or do you just want us to do your homework for you?
Reply: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 %26lt;stdio.h%26gt;


#include %26lt;math.h%26gt;





int main() {





int i,j,prime;





for(i=3;i%26lt;=20;i++) {


if(i % 2 == 0) {


printf("%d IS AN EVEN NUMBER.\n",i);


}


else {


prime = 1;


for(j=2;j %26lt;= 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;


}


No comments:

Post a Comment