LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
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


Reply
  Search this Thread
Old 09-13-2012, 01:59 PM   #1
lma032
LQ Newbie
 
Registered: Sep 2012
Posts: 3

Rep: Reputation: Disabled
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) ??
 
Old 09-13-2012, 02:57 PM   #2
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 635

Rep: Reputation: 186Reputation: 186
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.
 
Old 09-13-2012, 03: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,477

Rep: Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020
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 03:39 PM.
 
Old 09-13-2012, 03:07 PM   #4
lma032
LQ Newbie
 
Registered: Sep 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
#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?
 
Old 09-13-2012, 03:22 PM   #5
YankeePride13
Member
 
Registered: Aug 2012
Distribution: Ubuntu 10.04, CentOS 6.3, Windows 7
Posts: 262

Rep: Reputation: 54
FYI, you're incrementing i twice.
 
Old 09-13-2012, 03:28 PM   #6
lma032
LQ Newbie
 
Registered: Sep 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
yea, i discovered that right after i posted it, but can you any more errors?
 
Old 09-13-2012, 03:31 PM   #7
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181Reputation: 1181
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.
 
Old 09-13-2012, 03:39 PM   #8
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 635

Rep: Reputation: 186Reputation: 186
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 03:43 PM. Reason: Added some info
 
Old 09-13-2012, 07:11 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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.
 
Old 09-13-2012, 07:47 PM   #10
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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.
 
Old 09-14-2012, 01:48 AM   #11
hydraMax
Member
 
Registered: Jul 2010
Location: Skynet
Distribution: Debian + Emacs
Posts: 467
Blog Entries: 60

Rep: Reputation: 51
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!
 
Old 09-14-2012, 07:22 AM   #12
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 04:49 PM
[SOLVED] Bash - While Loop reading from two lists simultaneously - nested while loop wolverene13 Programming 11 10-01-2011 06:00 PM
for loop or while loop to read the fields of a file.. visitnag Linux - Newbie 10 09-02-2010 09:47 PM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 04:50 AM


All times are GMT -5. The time now is 10:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration