Computing.Net > Forums > Programming > Help w/ Simple C Program

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Help w/ Simple C Program

Reply to Message Icon

Name: Kendra
Date: September 15, 2003 at 18:45:18 Pacific
OS: XP
CPU/Ram: 512
Comment:

I'm new to programming in C and taking a beginner course. My homework is to write a program that reads an integer and determines and prints whether it is odd or even. I'm supposed to use the remainer operator (even number is multiple of two and remainder of zero). We haven't learned the "if" statements yet. Is there a way to write this without "if?" Here's what I have so far. I'm stuck. Any help would be greatly appreciated.

#include
int main()
{
printf( "Enter an integer. \n");
scanf("%d", number);
number % 2 == 0

}



Sponsored Link
Ads by Google

Response Number 1
Name: zeroguy
Date: September 15, 2003 at 19:00:32 Pacific
Reply:

I don't see how you could possible write this without any if statements at all. Here's a way to do it, but it would probably be obvious that someone else did it.

printf("That number is %s!\n", ((number%2)?"odd":"even"));

Good luck.


0

Response Number 2
Name: saddam
Date: September 15, 2003 at 19:06:16 Pacific
Reply:

hello,

an alternative to if statements is switch case. not too sure if you have learnt it.
learn it in advance and impress your tutor.
:-)

saddam


0

Response Number 3
Name: Infinite Recursion
Date: September 15, 2003 at 20:10:28 Pacific
Reply:

A solution to this is impossible without some form of comparison for even or odd... so, since the IF statement is the most basic... I suggest you use it. But instead of using the "advanced" ? : extras... I suggest using a standard IF statement.

Checking for the value of

if (number % 2 == 0)
{
// even or odd - u decide
}
if (number % 2 != 0)
{
// even or odd - u decide
}

I know this is bulky and it could be resolved on a single line of code using the ? : notation... but I suspect a beginning student would submit a huge block of code like the one I listed, as opposed to digging for the ? : ...

Being that you don't know about IF statements, leads me to believe that you haven't been introduced to a key component of the solution to your assignment. Switch statements are more "advanced" than if statements so again, I suggest the standard IF statement.

Up to you, either way will work... the ? : is faster and overall better. Just be sure you know how to explain it when the professor asks what it is.

IR


0

Response Number 4
Name: borelli35
Date: September 15, 2003 at 21:10:44 Pacific
Reply:

I would use the if statement suggested. I really think you must have missed something their because it is very unlikely that any instructor would not teach the use of if before asking you to understand the modulo operator.

borelli35


0

Response Number 5
Name: Kendra
Date: September 16, 2003 at 13:35:10 Pacific
Reply:

Thanks to all who have helped so far. This is what I have now. It compiles, but I get an error "Segmentation Fault (core dumped)" when I run the program. Can anyone help?

#include <stdio.h>
int main()
{
printf ( "Enter a number. \n");
int number;
scanf("%d", number);
if (number % 2 == 0)
printf ( "The number is even.");
else printf ( "The number is odd.");
}


0

Related Posts

See More



Response Number 6
Name: Infinite Recursion
Date: September 16, 2003 at 13:40:55 Pacific
Reply:

For starters you are missing the { and } of your if statement... the structure for a multiline if statement needs to be enclosed with the { and }... since you are using an else, there is a little more too it than that, but it will get you on the right track.

IR



0

Response Number 7
Name: Ronin1
Date: September 16, 2003 at 21:48:18 Pacific
Reply:

No if statement...

#include [stdio.h]
#include [stdlib.h]
#include [string.h]

int main(void)
{
static char *eORo[] = { "even", "odd" };
char temp[10], *p;
int theNum;

printf("Enter a number ");
theNum = (int)strtol((fgets(temp, sizeof(temp), stdin)), &p, 10);

printf("%d is %s\n", theNum, eORo[theNum % 2]);

getchar();
return 0;
}

One of these days, I'll get the code thingie for the board, but the brackets are for the lt and gt symbols. :)



0

Response Number 8
Name: Ronin1
Date: September 16, 2003 at 22:08:16 Pacific
Reply:

Oops

Thanks to all who have helped so far. This is what I have now. It compiles, but I get an error "Segmentation Fault (core dumped)" when I run the program. Can anyone help?

#include <stdio.h>
int main()
{
int number;
printf ( "Enter a number. \n");
scanf("%d", &number);
if (number % 2 == 0)
printf ( "The number is even.");
else printf ( "The number is odd.");
return EXIT_SUCCESS; /* defined in stdlib */
}

You have declared "number" after a printf statement. C requires declaration of variables at the start of a code block if I'm not mistaken, you forgot the address of "number" in the scanf statement... &number, and you forgot to return the exit code to your host OS. The if else structure looks ok to me.

HTH


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Help w/ Simple C Program

newbie needs help in simple C++ Program www.computing.net/answers/programming/newbie-needs-help-in-simple-c-program/23.html

Help with my C++ program www.computing.net/answers/programming/help-with-my-c-program/1813.html

C Program Simple Question (HELP...) www.computing.net/answers/programming/c-program-simple-question-help/10559.html