![]() |
|
|
|
#21 |
|
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? Kevin Handy |
|
|
|
|
#22 |
|
Posts: n/a
|
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. #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) >> 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)) Jens Schicke |
|
|
|
#23 |
|
Posts: n/a
|
Jens Schicke wrote:
> 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. > > > #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 Martin Ambuhl |
|
|
|
#24 |
|
Posts: n/a
|
On Fri, 08 Aug 2003 07:57:42 -0700, Kelsey Bjarnason <> 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) >> >> 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). Don Starr |
|
|
|
#25 |
|
Posts: n/a
|
(Girish Pal Singh) wrote in message news:<. 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 R. Rajesh Jeba Anbiah |
|
|
|
#26 |
|
Posts: n/a
|
IF is not a looping construct. You can use recursion, so long as IF is
available. "Kelsey Bjarnason" <> wrote in message news > [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; ArWeGod |
|
|
|
#27 | |
|
Junior Member
Join Date: Jun 2008
Posts: 1
|
Quote:
lubna hussaini |
|
|
|
|