Ok folks ..
I've come across a really neat C problem.Try coding this !
" A program in C to print whether a given number is even or odd without using any
conditional statements and loops ! "
I'm sure someone will do this ! ...
Printable View
Ok folks ..
I've come across a really neat C problem.Try coding this !
" A program in C to print whether a given number is even or odd without using any
conditional statements and loops ! "
I'm sure someone will do this ! ...
problem solved ;)Code:
#include <stdio.h>
int main(void)
{
int odd;
int even;
printf("enter an even number: ");
scanf("%d",&even);
printf("\n%d is an even number.",even);
printf("\n\nEnter an odd number: ");
scanf("%d",&odd);
printf("\n%d is an odd number.",odd);
return 0;
}
This is pretty easy once you know how to distinguish it and analyze the problem. I'll even have it show some text like "ODD" and "EVEN" for you... :D
Code:#include <stdio.h>
int main(void)
{
int input;
char strings[1][5];
strcpy(strings[0], "ODD\0");
strcpy(strings[1], "EVEN\0");
printf("Give me a number: ");
scanf("%i", &input);
printf("This is a(n) %s number!", &strings[(input+1)%2]);
getch();
return 0;
}
ok, an serious answer then:
i'm not sure if this is allowed, but i think it is:
Code:#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int number, temp;
printf("Enter a number: ");
scanf("%d",&number);
temp=number%2;
switch(temp)
{
case 0:
printf("\n%d is an even number",number);
break;
default:
printf("\n%d is an odd number",number);
break;
}
return EXIT_SUCCESS;
}
You got it right folks !!!! ... This question was actually asked for a job interview here !. The logic is pretty simple and because of the fact that it is so simple, some people never solve it .
I don't think I would get that job :(. I'm guessing it was lepricaun's first and Tim_axe's programs that were right? lepricaun's second had a switch which is a conditional statement, so that's not correct is it? /me works on some code. Peace.
i also don'tQuote:
lepricaun's second had a switch which is a conditional statement, so that's not correct is it?
think my second is allowed, but the first was more of a joke, since some of these questions are so tricky, that you think it the hard way, while the solution is very simple....