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
Printable View
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
sounds like homework, i'll give ya a hint :). All even numbers are divisible by 2.
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
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
1. use scanf
2. get the number and use the % operator
3. if it's an even number else print the odd number
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! :)
Hot or Not?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;
}
Just a suggestion, don't put everything inside the main function. Split it up, say getInput and print etc.
Quote:
Originally Posted by Deadlocked
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().
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.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;
}
Anyway it looks not too bad overall. :)
~/
Thanks for the suggestion but Id to do it my way.
That level of C>>>>>>>>>>>>> my level of C :p
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:
Is basicaly an if statement:Code:((input % 2) == 0) ? "" : "not ")
if input%2 == 0 then it would print nothing, if it != 0 then it would print not.