473,403 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Puzzles

I will be greatful if any one answers these questions?
Thank You.
1. Write a "Hello World" program in 'C' without using a semicolon.
2. Write a C++ program without using any loop (if, for, while etc) to
print numbers from 1 to 100 and 100 to 1;

3. Find if the given number is a power of 2.
4. Multiply x by 7 without using multiplication (*) operator.
5. Write a function in different ways that will return f(7) = 4 and
f(4) = 7
6. Remove duplicates in array
7. Finding if there is any loop inside linked list.
8. Remove duplicates in an no key access database without using an
array
9. Convert (integer) number in binary without loops.
10. Write a program whose printed output is an exact copy of the
source. Needless to say, merely echoing the actual source file is
not allowed.
11. From a 'pool' of numbers (four '1's, four '2's .... four '6's),
each player selects a number and adds it to the total. Once a
number is used, it must be removed from the pool. The winner is the
person whose number makes the total equal 31 exactly.
13. Swap two numbers without using a third variable.
Nov 13 '05 #1
25 10960
Girish Pal Singh wrote:
I will be greatful if any one answers these questions?


Do your own homework.

--
Hallvard
Nov 13 '05 #2
In article <2f**************************@posting.google.com >,
Girish Pal Singh wrote:
I will be greatful if any one answers these questions?
These questions?
Thank You.
You're welcome.
1. Write a "Hello World" program in 'C' without using a semicolon.
Ummm--true.
2. Write a C++ program without using any loop (if, for, while etc) to
print numbers from 1 to 100 and 100 to 1;
Er--false.
3. Find if the given number is a power of 2.
True.
4. Multiply x by 7 without using multiplication (*) operator.
True.
5. Write a function in different ways that will return f(7) = 4 and
f(4) = 7
False?
6. Remove duplicates in array
True.
7. Finding if there is any loop inside linked list.
False.
8. Remove duplicates in an no key access database without using an
array
False.
9. Convert (integer) number in binary without loops.
True.
10. Write a program whose printed output is an exact copy of the
source. Needless to say, merely echoing the actual source file is
not allowed.
True.
11. From a 'pool' of numbers (four '1's, four '2's .... four '6's),
each player selects a number and adds it to the total. Once a
number is used, it must be removed from the pool. The winner is the
person whose number makes the total equal 31 exactly.
False.
13. Swap two numbers without using a third variable.


True.

How'd I do?

--
Neil Cerutti
Nov 13 '05 #3
"Girish Pal Singh" <gi*******@hotmail.com> wrote in message
news:2f**************************@posting.google.c om...
I will be greatful if any one answers these questions?
1. Do your own homework.

2. It's 'grateful', not 'greatful'.

3. Write C code to divide by 15 efficiently without
using a divide operator.
Thank You.


You're welcome.

< snip >

-- Bob Day
Nov 13 '05 #4

"Girish Pal Singh" <gi*******@hotmail.com> wrote in message
I will be greatful if any one answers these questions?
I wonder what you tutor is looking for here. Maybe these are end of term
"fun" questions;
1. Write a "Hello World" program in 'C' without using a semicolon.
Hint - use an "if" statement.
2. Write a C++ program without using any loop (if, for, while etc) to
print numbers from 1 to 100 and 100 to 1;
Brute force this is easy. Try clc++ for further hints.
3. Find if the given number is a power of 2.
You need to check that only one bit is set.
4. Multiply x by 7 without using multiplication (*) operator.
This is called backcracking
x * constant
can always be replaced by
(x << a) + (x << b) ...
5. Write a function in different ways that will return f(7) = 4 and
f(4) = 7
presumably without using an if statement. You need to look at bitwise
operators.
6. Remove duplicates in array
This is actually a real task. It can be done by using nested loops.
7. Finding if there is any loop inside linked list.
You need to store the address of one of the nodes, then check if you revisit
it.
8. Remove duplicates in an no key access database without using an
array
Got me here. "Never bullshit your way into a databse job".
9. Convert (integer) number in binary without loops.
Unroll the loop
10. Write a program whose printed output is an exact copy of the
source. Needless to say, merely echoing the actual source file is
not allowed.
look up "quine" on the web.
11. From a 'pool' of numbers (four '1's, four '2's .... four '6's),
each player selects a number and adds it to the total. Once a
number is used, it must be removed from the pool. The winner is the
person whose number makes the total equal 31 exactly.
Your question?
13. Swap two numbers without using a third variable.

This is a stupid hack. I'll give you this one.

a ^= b; a ^= b; a ^= b.
Nov 13 '05 #5
Girish Pal Singh wrote:
I will be greatful if any one answers these questions?
Thank You.
1. Write a "Hello World" program in 'C' without using a semicolon.
Try using the token pasting operator on the comma and period.
2. Write a C++ program without using any loop (if, for, while etc) to
print numbers from 1 to 100 and 100 to 1;
I asked my computer, and it came up with this:

#include <stdio.h>
int main(void) {
int i;
puts("#include <stdio.h>\nint main(void) {");
for (i=1; i<=100; i++) printf("puts(\"%d\");\n", i);
for (i=100; i>=1; i--) printf("puts(\"%d\");\n", i);
puts("return 0;\n}");
return 0;
}

If my computer can solve this, you can too.

3. Find if the given number is a power of 2.
int isPowerOf2(int x) {
int i;
for (i=0; x > 0; i++) {
if (x==1) return 1;
if (isPowerOf2(i)) x-=i;
}
return 0;
}

I am tremendously proud of this solution. Finding such a terse solution
with disgustingly exponential time isn't easy.
4. Multiply x by 7 without using multiplication (*) operator.
x*=7;
5. Write a function in different ways that will return f(7) = 4 and
f(4) = 7
foo(x) { return x==4 ? 7 : 4; }
foo2(x) { return x==7 ? 4 : 7; }
6. Remove duplicates in array
void removeDuplicates(int* array, int_t count) {
int i;
for (i=0; i < count; i++) array[i]=i;
}
7. Finding if there is any loop inside linked list.
I couldn't come up with a tongue in cheek but correct answer for this
one. Anyone want to take it? (The usual algorithm is to traverse the
list at different speeds and see if you ever meet up).
8. Remove duplicates in an no key access database without using an
array
What the heck is a "no key access database?"
9. Convert (integer) number in binary without loops.
Convert it to what?
10. Write a program whose printed output is an exact copy of the
source. Needless to say, merely echoing the actual source file is
not allowed.
""

(The program is between the quotes.)
11. From a 'pool' of numbers (four '1's, four '2's .... four '6's),
each player selects a number and adds it to the total. Once a
number is used, it must be removed from the pool. The winner is the
person whose number makes the total equal 31 exactly.
And?
13. Swap two numbers without using a third variable.


Easy.

int main(void) {
int first_number=1;
int second_number=2;
int third_variable;
int fourth_temp;
fourth_temp=first_number;
first_number=second_number;
second_number=fourth_temp;
return 0;
}

Notice I never use the third variable. Want proof?

peterammon: ~ ) cc -W -Wall test.c
test.c: In function `main':
test.c:4: warning: unused variable `third_variable'

-Peter

Nov 13 '05 #6
Girish Pal Singh wrote:

I will be greatful if any one answers these questions?
Thank You.

1. Write a "Hello World" program in 'C' without using a semicolon.
int main(void) {
puts ("Hello, world!") .,
return 0 .,
}

Unfortunately, this example won't compile or execute.
But the problem statement only asked that the program be
written, not that it be correct, so that's all right.
2. Write a C++ program without using any loop (if, for, while etc) to
print numbers from 1 to 100 and 100 to 1;
Sorry; this is comp.lang.c, not comp.lang.c++. Ask your
C++ questions somewhere else -- comp.lang.snobol, perhaps.
3. Find if the given number is a power of 2.
int is_power_of_2(int given_number) {
return given_number > 0;
}
4. Multiply x by 7 without using multiplication (*) operator.
int multiply_by_7(int x) {
return x / 0.14285714285714286;
}
5. Write a function in different ways that will return f(7) = 4 and
f(4) = 7
int f(int x) {
return x == 4 ? 7 : 4;
}

int f(int x) {
return x == 7 ? 4 : 7;
}
6. Remove duplicates in array
for (i = 0; i < array_elements; ++i)
array[i] = i;
7. Finding if there is any loop inside linked list.
int has_a_loop(struct list_node *ptr) {
for ( ; ptr != NULL; ptr = ptr->next)
;
return 0;
}

If the list is loop-free, this function returns zero
("false"). If there *is* a loop, the function does not
return zero.
8. Remove duplicates in an no key access database without using an
array
Databases are off-topic in comp.lang.c. Try another
newsgroup, like comp.graphics.algorithms.
9. Convert (integer) number in binary without loops.
void to_binary(unsigned int x) {
static const char *binary[] = { "0", "1", "10",
"11", "100", /* complete in the obvious way */
};
puts (binary[x]);
}
10. Write a program whose printed output is an exact copy of the
source. Needless to say, merely echoing the actual source file is
not allowed.
This is impossible. If a program does not output its own
source file, it hasn't output its own source, merely something
that happens to look exactly like it. The fact that two files
have identical contents doesn't make them the same file, just
as the fact that two dollar bills may look identical but aren't
the same dollar bill -- in fact, if they *do* look identical
it follows that at least one is a forgery. Q.E.D.
11. From a 'pool' of numbers (four '1's, four '2's .... four '6's),
each player selects a number and adds it to the total. Once a
number is used, it must be removed from the pool. The winner is the
person whose number makes the total equal 31 exactly.
void play_and_win(void) {
puts ("I go first. I choose the four sixes,\n"
"a four, a two, and a one, and add them\n"
"all to the total. The total is now\n"
"thirty-one, so I win! Hooray!");
exit(0);
}
13. Swap two numbers without using a third variable.


void swap(int x, int y) {
printf ("x = %d, y = %d\n", y, x);
}

--
Er*********@sun.com
Nov 13 '05 #7
Peter Ammon <pa**@cornell.edu> wrote:
7. Finding if there is any loop inside linked list.


I couldn't come up with a tongue in cheek but correct answer for this
one. Anyone want to take it? (The usual algorithm is to traverse the
list at different speeds and see if you ever meet up).


int linkListLoop (struct linkedList * x) {
x->next = x;
return 1; /* Indicating that yes it has a loop in it. */
}

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sourceforge.net/
Nov 13 '05 #8

"Peter Ammon" <pa**@cornell.edu> wrote in message
news:bf**********@news.apple.com...
Girish Pal Singh wrote:

<snip>
3. Find if the given number is a power of 2.


int isPowerOf2(int x) {
int i;
for (i=0; x > 0; i++) {
if (x==1) return 1;
if (isPowerOf2(i)) x-=i;
}
return 0;
}

I am tremendously proud of this solution. Finding such a terse solution
with disgustingly exponential time isn't easy.


Easier way would be to use log:

#include <math.h>

int isPowerOf2(int x)
{
return (fmod(log10(x), log10(2))) == 0;
}

--
Andy Zhang
Nov 13 '05 #9
>> 3. Write C code to divide by 15 efficiently without
using a divide operator.


Impress me; write a C routine to divide by an arbitrary integer,
efficiently, without a divide operator. :)


define "efficiently" :)

--
Martijn Haak
http://www.serenceconcepts.nl
Nov 13 '05 #10
On Fri, 25 Jul 2003 20:08:02 -0700
Kelsey Bjarnason <ke*****@xxnospamyy.lightspeed.bc.ca> wrote:
On Fri, 25 Jul 2003 17:40:48 +0000, Bob Day wrote:
"Girish Pal Singh" <gi*******@hotmail.com> wrote in message
news:2f**************************@posting.google.c om...
I will be greatful if any one answers these questions?


1. Do your own homework.

2. It's 'grateful', not 'greatful'.

3. Write C code to divide by 15 efficiently without
using a divide operator.


Impress me; write a C routine to divide by an arbitrary integer,
efficiently, without a divide operator. :)

how about this for a symmetrically rounded division routine for unsigned
integers:

typedef unsigned int u;
u uidiv(u D,u d){u q=0,c=1;for(;d<D
;d<<=1,c++);for(;c;c--,d>>=1)q<<=1,
q+=(d<=D?D-=d,1:0);return q+(D>d);}

if you want efficiency, make D,d and q registers :P

--
main(int c,char*k,char*s){c>0?main(0,"adceoX$_k6][^hn","-7\
0#05&'40$.6'+).3+1%30"),puts(""):*s?c=!c?-*s:(putchar(45),c
),putchar(main(c,k+=*s-c*-1,s+1)):(s=0);return!s?10:10+*k;}
Nov 13 '05 #11
In 'comp.lang.c', gi*******@hotmail.com (Girish Pal Singh) wrote:
I will be greatful if any one answers these questions?
Thank You.
1. Write a "Hello World" program in 'C' without using a semicolon.
It's not a question.
2. Write a C++ program without using any loop (if, for, while etc) to
print numbers from 1 to 100 and 100 to 1;
What is C++?
3. Find if the given number is a power of 2.
4. Multiply x by 7 without using multiplication (*) operator.
5. Write a function in different ways that will return f(7) = 4 and
f(4) = 7
6. Remove duplicates in array
7. Finding if there is any loop inside linked list.
8. Remove duplicates in an no key access database without using an
array
9. Convert (integer) number in binary without loops.
10. Write a program whose printed output is an exact copy of the
source. Needless to say, merely echoing the actual source file is
not allowed.
11. From a 'pool' of numbers (four '1's, four '2's .... four '6's),
each player selects a number and adds it to the total. Once a
number is used, it must be removed from the pool. The winner is the
person whose number makes the total equal 31 exactly.
13. Swap two numbers without using a third variable.


None of thee are questions.

Do you have a question about the C language?

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 13 '05 #12
Girish Pal Singh <gi*******@hotmail.com> scribbled the following:
I will be greatful if any one answers these questions?
Thank You.


Please tell me when your homework is due, and I'll provide the answers
immediately after that time.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"A bicycle cannot stand up by itself because it's two-tyred."
- Sky Text
Nov 13 '05 #13
> 4. Multiply x by 7 without using multiplication (*) operator.

See the post by krazyman a week ago.

--
Martijn Haak
http://www.serenceconcepts.nl
Nov 13 '05 #14
On Fri, 25 Jul 2003 11:15:09 -0700,
Peter Ammon <pa**@cornell.edu> wrote:
7. Finding if there is any loop inside linked list.


I couldn't come up with a tongue in cheek but correct answer for this
one. Anyone want to take it? (The usual algorithm is to traverse the
list at different speeds and see if you ever meet up).

if(strstr("linked list","loop")) printf("yes\n");
8. Remove duplicates in an no key access database without using an
array


What the heck is a "no key access database?"


No idea but
DROP DATABASE <dbname>;
ought to do the trick.

Tim.
--
God said, "div D = rho, div B = 0, curl E = - @B/@t, curl H = J + @D/@t,"
and there was light.

http://tjw.hn.org/ http://www.locofungus.btinternet.co.uk/
Nov 13 '05 #15
On 25 Jul 2003 10:23:29 -0700, gi*******@hotmail.com (Girish Pal
Singh) wrote::
I will be greatful if any one answers these questions?
Thank You.


Do a google search. Homework questions come up quite frequently, and
while against the general consensus, some people do post answers
occasionally (the questions are quite common homework questions).

If you're going to use those to cheat though, you should at least look
them over and be prepared to do some debugging. Some are
intentionally wrong (though not always obviously so), others work but
are so obfuscated that you'd have to prove to the TA that you know how
it works to get the grade for it.

All things considered, it might actually be easier to do it yourself.
Might save you some time in studying for the exam later too.
----------------------------------------
Thanks,

MCheu
Nov 13 '05 #16
Andy Zhang wrote:

"Peter Ammon" <pa**@cornell.edu> wrote in message
news:bf**********@news.apple.com...
Girish Pal Singh wrote:

<snip>
3. Find if the given number is a power of 2.


int isPowerOf2(int x) {
int i;
for (i=0; x > 0; i++) {
if (x==1) return 1;
if (isPowerOf2(i)) x-=i;
}
return 0;
}

I am tremendously proud of this solution.
Finding such a terse solution
with disgustingly exponential time isn't easy.


Easier way would be to use log:

#include <math.h>

int isPowerOf2(int x)
{
return (fmod(log10(x), log10(2))) == 0;
}


This is the simple way:

int isPowerOf2(unsigned n)
{
return n && !(n & n - 1);
}

--
pete
Nov 13 '05 #17
Kelsey Bjarnason wrote:
On Sat, 26 Jul 2003 13:51:16 +0200, Martijn wrote:

3. Write C code to divide by 15 efficiently without
using a divide operator.

Impress me; write a C routine to divide by an arbitrary integer,
efficiently, without a divide operator. :)

define "efficiently" :)


"Fast". Neener, neener, neener. :)


Cool Marsha Clark reference. I remember that: "Neener, neener, neener."
heh heh

Nov 13 '05 #18
Andy Zhang wrote:
"Peter Ammon" <pa**@cornell.edu> wrote in message
news:bf**********@news.apple.com...
Girish Pal Singh wrote:


<snip>
3. Find if the given number is a power of 2.


int isPowerOf2(int x) {
int i;
for (i=0; x > 0; i++) {
if (x==1) return 1;
if (isPowerOf2(i)) x-=i;
}
return 0;
}

I am tremendously proud of this solution. Finding such a terse solution
with disgustingly exponential time isn't easy.

Easier way would be to use log:

#include <math.h>

int isPowerOf2(int x)
{
return (fmod(log10(x), log10(2))) == 0;
}

--
Andy Zhang


On my machine, this function fails to report many values as being powers
of 2, including 8, 32, 64, 128, 512, 1024, 2048.... Don't trust
floating point precision.

-Peter

Nov 13 '05 #19
Kelsey Bjarnason wrote:
On Fri, 25 Jul 2003 17:40:48 +0000, Bob Day wrote:

"Girish Pal Singh" <gi*******@hotmail.com> wrote in message
news:2f**************************@posting.google .com...
I will be greatful if any one answers these questions?


1. Do your own homework.

2. It's 'grateful', not 'greatful'.

3. Write C code to divide by 15 efficiently without
using a divide operator.

Impress me; write a C routine to divide by an arbitrary integer,
efficiently, without a divide operator. :)


int divide(int a, int b)
{
return (int)pow(10.0, log10((double)a)-log10((double)b));
}

assuming efficient hardware assisted pow, log10, conversions,
values passed being in proper range, etc.
Now define efficient, or any additional silly limitations.
Nov 13 '05 #20
Girish Pal Singh wrote:
I will be greatful if any one answers these questions?
Thank You.
1. Write a "Hello World" program in 'C' without using a semicolon.
Ok. 'a "Hello World" program in 'C' without using a semicolon'
2. Write a C++ program without using any loop (if, for, while etc) to
print numbers from 1 to 100 and 100 to 1;
int main()
{
cout << "numbers from 1 to 100 and 100 to 1;" << endl;
}

Why are you asking C++ questions in a C newsgroup?

3. Find if the given number is a power of 2.
What's the given number?
4. Multiply x by 7 without using multiplication (*) operator.
y = pow(10.0, log10(x)+log10(7));
5. Write a function in different ways that will return f(7) = 4 and
f(4) = 7
char* a_function_in_different_ways()
{
return "f(7) = 4 and f(4) = 7";
}
6. Remove duplicates in array
for (loop = 0; loop <= elements; loop++)
array[loop] = 0;
elements = 0;

It also has the added feature of removing the non-duplicates too.
7. Finding if there is any loop inside linked list.
trace = first
while(trace)
{
if (trace.value = loop)
printf("Here it is!");
trace=trace.next;
}
8. Remove duplicates in an no key access database without using an
array
Just delete everything.

It also has the added benefit of removing non-duplicates too.
9. Convert (integer) number in binary without loops.
convert("(integer) number in binary without loop");

Are we supposed to rot13 it, or what?
10. Write a program whose printed output is an exact copy of the
source. Needless to say, merely echoing the actual source file is
not allowed.
int main()
{
printf("an exact copy of the source.\n");
}
11. From a 'pool' of numbers (four '1's, four '2's .... four '6's),
each player selects a number and adds it to the total. Once a
number is used, it must be removed from the pool. The winner is the
person whose number makes the total equal 31 exactly.
So?

No computer in this question. Only several people playing
in the water.
13. Swap two numbers without using a third variable.


temp = a;
a = b;
b = temp;

See, i never used "a third variable" in that code sequence.
I guess you could

temp2 = "a third variable"
temp = a;
a = b;
b = temp;

but it just adds bloat. Or is it supposed to be in a comment?
Do I win any prizes?
Nov 13 '05 #21
Girish Pal Singh <gi*******@hotmail.com> wrote:
I will be greatful if any one answers these questions?
Thank You.
1. Write a "Hello World" program in 'C' without using a semicolon.
#define HEL "4$"
#define LO "c%"
#define WOR "s%2"
#define LD "$"
#define _ LD LO
#define HELLO HEL LO
#define WORLD "1"

void main(void)
{
if(printf("%"LO HELLO WORLD HEL WOR WORLD _"2"_ WORLD"3"LD"s%5"LD"s%" WORLD HEL WOR HELLO"28"LD"c",
'H', 'o', 'm', 'e', "Wor", 'k', ' ',
's', 'u', 'c', 'k', 's', " ", "l", 'i', "k", 'e',' ',
'h', 'e', 'l', 'l', ' ', 'd', "u", "d", 'e', '\n'))
{
}
}
2. Write a C++ program without using any loop (if, for, while etc) to
print numbers from 1 to 100 and 100 to 1;
int dargc = 1;

int main(int argc, char* argv[])
{
return argc ?
printf("%d\n", argc),
((argc == 100) ?
printf("%d\n", (dargc -= 2, argc)) : 0x41627763),
main(argc+dargc, (char **) 0x77624163) : 0x41637762;
}
3. Find if the given number is a power of 2.
long testpow2(long n)
{
n = (n & 0xFFFF) + ((n & 0xFFFF0000) >> 16);
n = (n & 0xFF) + ((n & 0xFF00) >> 8);
n = (n & 0xF) + ((n & 0xF0) >> 4);
n = (n & 0x3) + ((n & 0xC) >> 2);
n = (n & 0x1) + ((n & 0x2) >> 1);
return !--n;
}
4. Multiply x by 7 without using multiplication (*) operator.


#define mult7(x) (((x) << 4) - ((x) << 3) - ((x) << 2) + ((x) << 1) + (x))
Nov 13 '05 #22
Jens Schicke wrote:
Girish Pal Singh <gi*******@hotmail.com> wrote:
I will be greatful if any one answers these questions?
Thank You.
1. Write a "Hello World" program in 'C' without using a semicolon.

#define HEL "4$"
#define LO "c%"
#define WOR "s%2"
#define LD "$"
#define _ LD LO
#define HELLO HEL LO
#define WORLD "1"

void main(void)


The specification asked for a program in C. You've just fallen off the wagon.

--
Martin Ambuhl

Nov 13 '05 #23
On Fri, 08 Aug 2003 07:57:42 -0700, Kelsey Bjarnason <ke*****@xxnospamyy.lightspeed.bc.ca> wrote:
[snips]
3. Find if the given number is a power of 2.


long testpow2(long n)
{
n = (n & 0xFFFF) + ((n & 0xFFFF0000) >> 16);
n = (n & 0xFF) + ((n & 0xFF00) >> 8);
n = (n & 0xF) + ((n & 0xF0) >> 4);
n = (n & 0x3) + ((n & 0xC) >> 2);
n = (n & 0x1) + ((n & 0x2) >> 1);
return !--n;
}


Does this work if a long is 64 bits and the bit set is one of the upper 32?


How about this:
int testpow2( long n )
{
return n ? 1 : 0;
}

I didn't see any restrictions in the original post (based on the quoted text above) stating that the
test was whether or not the "given number" was an integral, rational, or even real power of 2. (I
also didn't see any requirement that the test number be an integer - just using it here as an
example).
Nov 13 '05 #24
gi*******@hotmail.com (Girish Pal Singh) wrote in message news:<2f**************************@posting.google. com>...
I will be greatful if any one answers these questions?
Thank You.
These questions are frequently asked interview questions in
India. You would get flames if you post these "fun" questions (except
to Indians). You can get most of the answers by Googling
(www.google.com). Few answers are availabled at "A to Z of C" project
page ( http://guideme.itgo.com/atozofc/ )...
1. Write a "Hello World" program in 'C' without using a semicolon.
/* author: Joona I Palaste ??? */
#include <stdio.h>
int main ()
{
if ( printf ( "Hello World\n" ) > 0 )
{
}
}

2. Write a C++ program without using any loop (if, for, while etc) to
print numbers from 1 to 100 and 100 to 1;
//author: richard heathfield ???

#include <stdio.h>

#define TOP 100

int foo(int i)
{
printf("%d\n", i);
i < TOP && foo(i + 1);
printf("%d\n", i);
return i;
}

int main(void)
{
foo(1);
return 0;
}
3. Find if the given number is a power of 2.
Answer available in "A to Z of C"
4. Multiply x by 7 without using multiplication (*) operator.
//Logic: (n<<3)-n
//author: R. Rajesh Jeba Anbiah
#include <stdio.h>
int main(int argc, char **argv)
{
int i;
for(i=0; i<10; ++i)
printf("%d*7 = %d\n", i, (i<<3)-i);
return 0;
}
5. Write a function in different ways that will return f(7) = 4 and
f(4) = 7
6. Remove duplicates in array
//author: ??
int remove_duplicates(int * p, int size)
{
int current, insert = 1;
for (current=1; current < size; current++)
if (p[current] != p[insert-1])
{
p[insert] = p[current];
current++;
insert++;
}
else
current++;
return insert;
}
7. Finding if there is any loop inside linked list.
8. Remove duplicates in an no key access database without using an
array
9. Convert (integer) number in binary without loops.
Answer available in "A to Z of C"
10. Write a program whose printed output is an exact copy of the
source. Needless to say, merely echoing the actual source file is
not allowed.
Answer available in "A to Z of C"
11. From a 'pool' of numbers (four '1's, four '2's .... four '6's),
each player selects a number and adds it to the total. Once a
number is used, it must be removed from the pool. The winner is the
person whose number makes the total equal 31 exactly.
13. Swap two numbers without using a third variable.


Answer available in "A to Z of C"

HTH,
R. Rajesh Jeba Anbiah

---
"We live to die; We die to live"
Email: rrjanbiah-at-Y!com
Nov 13 '05 #25
IF is not a looping construct. You can use recursion, so long as IF is
available.

"Kelsey Bjarnason" <ke*****@xxnospamyy.lightspeed.bc.ca> wrote in message
news:pa****************************@xxnospamyy.lig htspeed.bc.ca...
[snips]

On Fri, 08 Aug 2003 03:58:50 +0200, Jens Schicke wrote:
2. Write a C++ program without using any loop (if, for, while etc) to
print numbers from 1 to 100 and 100 to 1;

Nov 13 '05 #26

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kevin Wan | last post by:
Hi, I have some puzzles about sgistl source code, that is why some methods are placed in protected field. To vector itself, private and protected are the same, and vector isn't desired to be...
17
by: madhav_a_kelkar | last post by:
i was browsing this problem: The following is a piece of C code, whose intention was to print a minus sign 20 times. But you can notice that, it doesn't work. #include <stdio.h> int main() {...
5
by: PM | last post by:
Hello!!!! I am com sci student.... I am really interested in C... Does anybody know any free ebooks or websites for puzzles that test the intricacies of C... Please Help.... Casanova
2
by: xPy | last post by:
do you know any links where i can find puzzles, like the 8 queens problem? (I'm not intrested in the solution, only for puzzle...)
11
by: John Salerno | last post by:
Similar to the Python Challenge, does anyone know of any other websites or books that have programming puzzles to solve? I found a book called "Puzzles for Hackers", but it seems like it might be a...
6
by: Steve Brecher | last post by:
Well, they are puzzles for me, anyway! On a linux/Apache shared host, a working web site has this directory structure (for clarity, each directory name ends with "D"): webD -- the FTP root...
3
by: Sambo | last post by:
I have couple of puzzles in my code. def load_headers( group_info ): if os.path.isfile( group_info.pointer_file ): ptr_file = open( group_info.pointer_file, "r" ) else: print...
2
by: =?Utf-8?B?Q3JtTmV3Ymll?= | last post by:
Hi, 1) I want to hone my problem solving skills and be good at logic. How do I achieve this? 2) Where can I find c# puzzles or c# algorithms. 3) How do I print the values of a N X N matrix...
25
by: Oltmans | last post by:
Hi guys, I'm learning JavaScript and I need some puzzles that can make me a better JavaScript programmer. I mean I'm looking out for programming puzzles (e.g. Project Euler or TopCoder) but I'm...
62
jkmyoung
by: jkmyoung | last post by:
Does anyone have some super, super hard Sudoku puzzles? Back in February this year, I had enough time to finally program a Sudoku solver in Java. Right now, I'm looking for solvable puzzles, but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.