for loop with if/else
Write a C function that prints the numbers from 1 to 50, along with an indication of whether the number is odd or even and whether 5 is a prime factor in the number. The function should print something like:
1 is odd and 5 is not a prime factor 2 is even and 5 is not a prime factor .. 10 is even and 5 is a prime factor The C function should have the following signature: void mynumbers(void) ?? |
What's the question?
Is it "why the function signature is with no arguments and no return type"? If so, I guess it is expected of you to perform all the logic there and just call that function from main. |
Oh! Homework!
Here's a hint -- you'll want to use the modulus (%) operator. Now, go do the work. |
#include <stdio.h>
int main(){ int i; for(i=1;i<=50;i++){ i=i+1; if (i%2==0) { printf("%d is a even number and 5 is not a prime factor.\n",i); } else if(i%2==1) { printf("%d is a odd number and 5 is not a prime factor.\n",i); } else if ((i%2==0) && (i%5==0)) { printf("%d is a even number an 5 is a factor.\n",i); } else if ((i%2==1) && (i%5==0)) { printf("%d is a odd number and 5 is a factor.\n",i); } } return=1; } this is my code, but it doesnt work, ideas? |
FYI, you're incrementing i twice.
|
yea, i discovered that right after i posted it, but can you any more errors?
|
You did the work in the main function, rather than in the function where the assignment told you to do it.
As mentioned above, you incremented i twice per iteration of the loop. You indicated 5 is not a prime factor without actually testing for that, so the tests that ought to find 5 is a prime factor can't even be reached. |
Post your code between [code][/code] tags, please.
You're performing some useless tests, like this one: Code:
if (i%2==0) Also, those { } are useless if they only contain a single instruction, so you could get rid of them. You need to rationalize first the tests you actually need to do, if I may say. If you want to keep a structure similar to the current one, I guess you can inline one of the checks (odd/even or divisible by 5/not divisible by 5) using the ternary operator directly inside the printf: Code:
printf ("xyz and 5 is %s a prime factor, (i%5 == 0 ? "" : "not")); |
Quote:
I've seen it happen many times; not recommended imho. |
Since 5 is prime, you might want to ask you instructor what she meant by a "prime factor," and how would a "factor" would be any different from a "prime factor." Any integer can be written as a product of prime numbers - doing that is what "factoring a number" usually means.
|
Code:
mynumbers = mapM mynumber [1..50] |
Quote:
(Sample code intentionally not provided.) But when a beginner doesn't understand the simple/crude way of doing something, I don't think it is constructive to demonstrate the trickier ways an expert might attack the same problem. When you can provide explanations without showing the final answer, that is a better fit for the philosophy of answering homework questions at LQ and probably also better for helping the student learn. Quote:
Quote:
|
All times are GMT -5. The time now is 05:39 PM. |