Latest LQ Deal: Linux Power User Bundle
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org for loop with if/else
 User Name Remember Me? Password
 Linux - Newbie This 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

 09-13-2012, 12:59 PM #1 lma032 LQ Newbie   Registered: Sep 2012 Posts: 3 Rep: 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) ??
 09-13-2012, 01:57 PM #2 414N Member   Registered: Sep 2011 Location: Italy Distribution: Slackware Posts: 635 Rep: 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.
 09-13-2012, 02:00 PM #3 tronayne Senior Member   Registered: Oct 2003 Location: Northeastern Michigan, where Carhartt is a Designer Label Distribution: Slackware 32- & 64-bit Stable Posts: 3,541 Rep: Oh! Homework! Here's a hint -- you'll want to use the modulus (%) operator. Now, go do the work. Last edited by tronayne; 09-13-2012 at 02:39 PM.
 09-13-2012, 02:07 PM #4 lma032 LQ Newbie   Registered: Sep 2012 Posts: 3 Original Poster Rep: #include 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?
 09-13-2012, 02:22 PM #5 YankeePride13 Member   Registered: Aug 2012 Distribution: Ubuntu 10.04, CentOS 6.3, Windows 7 Posts: 262 Rep: FYI, you're incrementing i twice.
 09-13-2012, 02:28 PM #6 lma032 LQ Newbie   Registered: Sep 2012 Posts: 3 Original Poster Rep: yea, i discovered that right after i posted it, but can you any more errors?
 09-13-2012, 02:31 PM #7 johnsfine LQ Guru   Registered: Dec 2007 Distribution: Centos Posts: 5,286 Rep: 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.
 09-13-2012, 02:39 PM #8 414N Member   Registered: Sep 2011 Location: Italy Distribution: Slackware Posts: 635 Rep: 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
09-13-2012, 06:11 PM   #9
chrism01
LQ Guru

Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,406

Rep:
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.

 09-13-2012, 06:47 PM #10 PTrenholme Senior Member Contributing Member   Registered: Dec 2004 Location: Olympia, WA, USA Distribution: Fedora, (K)Ubuntu Posts: 4,186 Rep: 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.
 09-14-2012, 12:48 AM #11 hydraMax Member   Registered: Jul 2010 Location: Skynet Distribution: Debian + Emacs Posts: 467 Blog Entries: 60 Rep: 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!
09-14-2012, 06:22 AM   #12
johnsfine
LQ Guru

Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep:
Quote:
 Originally Posted by 414N 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.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM wolverene13 Programming 11 10-01-2011 05:00 PM visitnag Linux - Newbie 10 09-02-2010 08:47 PM br8kwall Programming 10 04-30-2008 03:50 AM

LinuxQuestions.org

All times are GMT -5. The time now is 07:23 AM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -