Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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}

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.

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

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

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

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.");
}

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

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

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

![]() |
![]() |
![]() |

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