LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   for loop with if/else (https://www.linuxquestions.org/questions/linux-newbie-8/for-loop-with-if-else-4175427101/)

lma032 09-13-2012 12:59 PM

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) ??

414N 09-13-2012 01:57 PM

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.

tronayne 09-13-2012 02:00 PM

Oh! Homework!

Here's a hint -- you'll want to use the modulus (%) operator.

Now, go do the work.

lma032 09-13-2012 02:07 PM

#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?

YankeePride13 09-13-2012 02:22 PM

FYI, you're incrementing i twice.

lma032 09-13-2012 02:28 PM

yea, i discovered that right after i posted it, but can you any more errors?

johnsfine 09-13-2012 02:31 PM

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.

414N 09-13-2012 02:39 PM

Post your code between [code][/code] tags, please.
You're performing some useless tests, like this one:
Code:

if (i%2==0)
{
printf("%d is a even number and 5 is not a prime factor.\n",i);
}

else if(i%2==1)
{
 ...

If i is not divisible by 2, then you don't need to test it after the else, as it's implied.
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"));
This way, if i is divisible by 5, nothing gets printed after "is", else a not is added to the output string.

chrism01 09-13-2012 06:11 PM

Quote:

Also, those { } are useless if they only contain a single instruction, so you could get rid of them.
You COULD, but I wouldn't if I were you; that leads to a very common hard to find bug when you add in more code, even the odd debug statement.
I've seen it happen many times; not recommended imho.

PTrenholme 09-13-2012 06:47 PM

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.

hydraMax 09-14-2012 12:48 AM

Code:

mynumbers = mapM mynumber [1..50]
  where mynumber x =
          let oddness = if mod x 2 == 0 then "even" else "odd" in
          let byfive = if mod x 5 == 0 then "is" else "is not" in
          putStrLn
            $ show x ++ " is " ++ oddness ++ " and 5 " ++ byfive ++ " a factor"

*Main> mynumbers
1 is odd and 5 is not a factor
2 is even and 5 is not a factor
3 is odd and 5 is not a factor
4 is even and 5 is not a factor
5 is odd and 5 is a factor
6 is even and 5 is not a factor
7 is odd and 5 is not a factor
8 is even and 5 is not a factor
9 is odd and 5 is not a factor
10 is even and 5 is a factor
11 is odd and 5 is not a factor
12 is even and 5 is not a factor
13 is odd and 5 is not a factor
14 is even and 5 is not a factor
15 is odd and 5 is a factor
16 is even and 5 is not a factor
17 is odd and 5 is not a factor
18 is even and 5 is not a factor
19 is odd and 5 is not a factor
20 is even and 5 is a factor
21 is odd and 5 is not a factor
22 is even and 5 is not a factor
23 is odd and 5 is not a factor
24 is even and 5 is not a factor
25 is odd and 5 is a factor
26 is even and 5 is not a factor
27 is odd and 5 is not a factor
28 is even and 5 is not a factor
29 is odd and 5 is not a factor
30 is even and 5 is a factor
31 is odd and 5 is not a factor
32 is even and 5 is not a factor
33 is odd and 5 is not a factor
34 is even and 5 is not a factor
35 is odd and 5 is a factor
36 is even and 5 is not a factor
37 is odd and 5 is not a factor
38 is even and 5 is not a factor
39 is odd and 5 is not a factor
40 is even and 5 is a factor
41 is odd and 5 is not a factor
42 is even and 5 is not a factor
43 is odd and 5 is not a factor
44 is even and 5 is not a factor
45 is odd and 5 is a factor
46 is even and 5 is not a factor
47 is odd and 5 is not a factor
48 is even and 5 is not a factor
49 is odd and 5 is not a factor
50 is even and 5 is a factor

Dangit! Wrong language!

johnsfine 09-14-2012 06:22 AM

Quote:

Originally Posted by 414N (Post 4779670)
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:

You could inline both the tests with ternary operators inside a single printf.

(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:

Code:

printf ("xyz and 5 is %s a prime factor, (i%5 == 0 ? "" : "not"));

Note that a careful programmer would pay enough attention to get the white space correct (not just the words correct) in such code.

Quote:

Originally Posted by hydraMax (Post 4779951)
Dangit! Wrong language!

Impressive how many people here can do a trivial beginner programming assignment in a different way. But explanations are still better than code for answering homework questions.


All times are GMT -5. The time now is 05:39 PM.