Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!

Notices

Welcome to LinuxQuestions.org, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.

Having a problem logging in? Please visit this page to clear all LQ-related cookies.

Introduction to Linux - A Hands on Guide

This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.

Click Here to receive this Complete Guide absolutely free.

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:

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.

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.

Last edited by 414N; 09-13-2012 at 02:43 PM.
Reason: Added some info

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.

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.

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

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

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.

LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.