# Thread: Trying to write a program in C

1. ## Trying to write a program in C

Im trying to write a program using C that reads an integer and then determines wheather it is odd or even.

I have to use the if-else structure.

Any help will be apprectiated

2. sounds like homework, i'll give ya a hint . All even numbers are divisible by 2.

3. here's the logic.......
if a number divided by 2 gives remainder 0..it is even else it is odd.....you can make use of % operator to find remainder......try it and if you are stuck then post the code....someone will help

4. It is homework.

Im not looking for someone to post the code.

Just a hint in the right direction.

Ya I think the % operater will work.
Thanks

5. 1. use scanf
2. get the number and use the % operator
3. if it's an even number else print the odd number

6. here's some psuedocode

prompt user for number
input number
if the number has a remainder of 0 when divided by 2
the number is even
otherwise the number is odd

Just type up the right!

7. Code:
```#include <stdio.h>
#include <math.h>

int main()
{
int a, b;

printf("print a number:\n");

scanf("%d",&a);             /* Enter number here */

b=a%2;

if(b!=0)                                   /*if number divided by 2 and there is a remainder then number is odd*/

printf("this number is odd:\n");

else

printf("this number is even:\n");       /* prints wheather number is odd or even*/

return 0;
}```
Hot or Not?

8. Just a suggestion, don't put everything inside the main function. Split it up, say getInput and print etc.

Code:
```#include <stdio.h>
#include <math.h>

int main()
{
int a, b;

printf("print a number:\n");

scanf("%d",&a);             /* Enter number here */

b=a%2;

if(b!=0)                                   /*if number divided by 2 and there is a remainder then number is odd*/

printf("this number is odd:\n");

else

printf("this number is even:\n");       /* prints wheather number is odd or even*/

return 0;
}```
Hot or Not?

Well I won't comment on hot or not, but I would say that you don't really need to #include math.h for this version of your program to work. Also, you don't need the extra variable b. You can evalute a % 2 as an argument to printf().

Code:
```#include <stdio.h>

int main(void)
{

int input;

printf("\n\nEnter a number: ");
fflush(stdout);

if ((scanf("%d", &input)) != 1) {
puts("\nError: call to scanf() failed\n");
return 1;
}

printf("The number %d is %seven.\n", input,
((input % 2) == 0) ? "" : "not ");

return 0;
}```
When you get into some more complicated programs, you are likely going to find that you will want a more robust means of getting input from the user than a call to scanf(). If the user does not enter an int (as scanf() is expecting in this case) then the call to scanf() fails. At the very least, if you must use scanf() check the return value as I demonstrated. Later, you can do something like read the user input into a buffer with fgets() and then grab your number with sscanf() or the like.

Anyway it looks not too bad overall.

~/

10. Thanks for the suggestion but Id to do it my way.

That level of C>>>>>>>>>>>>> my level of C

11. That level of C isnt hard, he has simply added error checking by checking to see wether or not an input has been received. And the:

Code:
`((input % 2) == 0) ? "" : "not ")`
Is basicaly an if statement:

if input%2 == 0 then it would print nothing, if it != 0 then it would print not.