hi folks,

plz try sending answers to folllowing questions....i know they are a lot..still...send as many as possible.....

Write a "Hello World" program in 'C' without using a semicolon.

Write a C++ program without using any loop (if, for, while etc) to print numbers from 1 to 100 and 100 to 1;

C/C++ : Exchange two numbers without using a temporary variable.

C/C++ : Find if the given number is a power of 2.

C/C++ : Multiply x by 7 without using multiplication (*) operator.


C/C++ : Write a function in different ways that will return f(7) = 4 and f(4) = 7

Remove duplicates in array

Finding if there is any loop inside linked list.

Remove duplicates in an no key access database without using an array

Convert (integer) number in binary without loops.

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.

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.

Swap two numbers without using a third variable.

Given an array (group) of numbers write all the possible sub groups of this group.

Dani AI

Generated

These are classic interview puzzles; a few comments up front will save time and incorrect assumptions. is right to warn about undefined behavior — any answer that relies on signed-integer overflow is not portable. demonstrates the common “use a function call inside a control construct to avoid a semicolon” trick; that’s fine for showing possibilities, but the puzzle intent often prefers clearer solutions. ’s puts gag is a reminder that clever answers can be off-topic.

Common, safe snippets (original variants):

Check power of two (works for unsigned):

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

Multiply by 7 without * (use shifts; avoid shifting negatives):

int times7(int x) { return (x << 3) - x; }  // 8*x - x

Swap without a temporary (two options — each has caveats):

void xor_swap(int &a, int &b) { if (&a==&b) return; a ^= b; b ^= a; a ^= b; }
void arith_swap(int &a, int &b) { a = a + b; b = a - b; a = a - b; } // watch signed overflow

Linked-list loop detection (Floyd’s algorithm):

Node* detect_cycle(Node* head) {
    Node *slow=head, *fast=head;
    while (fast && fast->next) {
        slow = slow->next; fast = fast->next->next;
        if (slow == fast) return slow; // collision => cycle exists
    }
    return NULL;
}

To find the cycle start, reset one pointer to head and move both one step until they meet.

Remove duplicates: if order doesn't matter, sort then unique; if order must be preserved, use a hash set to remember seen values. Convert-to-binary without loops is done with recursion (or std::bitset in C++). The 31-game: first player takes 3, then always reply with 7 - (opponent's choice). That makes running totals 3,10,17,24,31; four copies of each number are sufficient because each pair (x + (7-x)) consumes one copy of two numbers and after four such pairs the target 31 is reached. Quines and the exact “print-the-source” trick are standard — look for short C quines that store program text in a string and print it with proper quoting.

Recommended Answers

All 9 Replies

Ummm. Do your own work

Write a "Hello World" program in 'C' without using a semicolon

Who comes up with these brilliant ideas?

Write a C++ program without using any loop (if, for, while etc) to print numbers from 1 to 100 and 100 to 1;

puts("5 9 2 37");

These are all numbers from 1 to 100 and 100 to 1, and I'm printing them.

questions 3 and 13 are identical. Both questions use undefined behavior of data overflow, so results will also be undefined.

Write a "Hello World" program in 'C' without using a semicolon.

Not possible.

Write a C++ program without using any loop (if, for, while etc) to print numbers from 1 to 100 and 100 to 1;

use recursion

Write a "Hello World" program in 'C' without using a semicolon.

#include <stdio.h>

int main()
{
	while( !printf( "%s", "Hello World\n" ) )
	{
	}
}

C/C++ : Write a function in different ways that will return f(7) = 4 and f(4) = 7

int f ( int x )
{
       if ( x == 7 )
      {
            return 4;
      }
      if ( x == 4 )
      {
            return 7;
      }
}

:cheesy:

Ok -- so it can be done.

hmmm. It compiled with the Visual Studio 8 Compiler ( 2005 Beta ). Maybe you are using the wrong file extention ( remember it says in C ).
This was my command to compile

cl /EHsc main.c

edit: I checked without the /EHsc switch. that also works.

cl main.c

hmmm. ...

I deleted my previous objection -- my error.

questions 3 and 13 are identical. Both questions use undefined behavior of data overflow, so results will also be undefined.

They are not identical. (And I'm not telling you why! Nya ha ha!) :mrgreen:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.