|
-
September 24th, 2004, 08:28 PM
#1
The C challenge
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 ! ...
-
September 24th, 2004, 11:23 PM
#2
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;
}
problem solved
-
September 25th, 2004, 01:11 AM
#3
Did it, Easy ;)
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... 
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;
}
-
September 25th, 2004, 10:58 AM
#4
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;
}
-
September 25th, 2004, 05:33 PM
#5
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 .
-
September 25th, 2004, 05:47 PM
#6
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.
-
September 25th, 2004, 08:28 PM
#7
lepricaun's second had a switch which is a conditional statement, so that's not correct is it?
i also don't
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....
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|