Interesting tricky program using C & C++:

Program 1:
Create a Program which produce output "Hello World" , the program must not contains semicolon ; in other word any statement of the program will not have a termination semicolon.

Program 2:
Write a C or C++ program which run without main() function or in other word we can say the function should not have any function named main().

Program 3:
Write a C program which takes one integer number from user and then checks whether number is Even or Odd without using Modulus (%) and Division (/) operator.

Program 4:
C program which creates numeric array num and print its value like 1[num], 2[num], 3[num]…n[num] check will it run.


First try to implement all program your self if you can't success the go to below link to see the solution of all above program.
<link removed>

Edited 5 Years Ago by Narue: Snipped self-promotion link

Stupid questions teaching useless trivia with horrible code examples. I'd appreciate if you didn't propagate the rampant stereotypes of Indian programmers while at the same time leading everyone else astray. :icon_rolleyes:

Interesting tricky program using C & C++:

Program 1:
Create a Program which produce output "Hello World" , the program must not contains semicolon ; in other word any statement of the program will not have a termination semicolon.

Program 2:
Write a C or C++ program which run without main() function or in other word we can say the function should not have any function named main().

Program 3:
Write a C program which takes one integer number from user and then checks whether number is Even or Odd without using Modulus (%) and Division (/) operator.

Program 4:
C program which creates numeric array num and print its value like 1[num], 2[num], 3[num]…n[num] check will it run.


First try to implement all program your self if you can't success the go to below link to see the solution of all above program.
<link removed>

This is like asking people to drive their cars around, but allowing only three tires to touch the road.

#1 is stupid - semi-colons are supposed to serve as the end of an expression marker, in C.

#2 also dumb. C programs are meant to always have a main() function.

#3 This one is an OK problem. Get that problem solving mojo working!

#4 Trying to use multiple arrays, to simulate multiple columns of data, maybe? Very odd, but I'm not sure I know yet, what you are looking for here.

You could use stuff like this in an obfuscation contest, but there's no practical need for it.

I can see how the first one could be solved (using an if() statement with an empty block as it's body), but the problem itself is pointless - no one would do such a thing for any practical purpose. The only point I can see for this question is either to get you to think 'outside the box', or else to underscore the fact that printf() has a return value.

I am at a loss as to how the second problem would work.

Edited 5 Years Ago by Schol-R-LEA: n/a

The only point I can see for this question

The point is that the people who thought it up couldn't come up with a better exercise, or thought trivia would somehow make the students better programmers, which is evidence that they're crappy teachers.

I am at a loss as to how the second problem would work.

The link I removed "solved" it with the preprocessor by defining a macro:

#include <stdio.h>

#define foo main

int foo(void)
{
    printf("Hello, world!\n");
    return 0;
}

It's a cheap trick that doesn't really solve the problem. A real solution exists but isn't portable; some compilers will allow you to specify an entry point aside from main(), and you also have options such as WinMain() with Win32 applications.

The first one would work in MS DOS with a Microsoft compiler using __asm{}, but
that's stretching the "C or C++" concept a bit.
Later versions of windows throw an error using this.
I guess you could also do a direct memory write to screen memory.

// DW_380503.cpp : Defines the entry point for the console application.
#include "stdafx.h"
void main(void)
{
	__asm
	{
		mov ah, 0ah
		mov al, 'H'
		int 10h

		mov al, 'e'
		int 10h

		mov al, 'l'
		int 10h

		mov al, 'l'
		int 10h

		mov al, 'o'
		int 10h
	}
}

#3 (lots of conditions)

#include "stdafx.h"
#include <stdlib.h>

int CheckNum(int num)
{
	int iCount = 0;
	while(num > 0)
	{
		num -= 2;
		iCount ++;
	}

	return iCount;
}

bool isOdd(int num)
{
	return CheckNum(num) != CheckNum(num-1);
}

void main(void)
{
	char pstrData[128] = {0};
	printf("%s", isOdd(atoi(gets(pstrData))) ? "true" : "false");
}

Edited 5 Years Ago by thines01: too much

#include <stdio.h>

void main()
{
    if(printf("Hello World"))
    { 
    }
}

This is the simplest i can make.

PROGRAM 3 :

#include<stdio.h>
void main() {
    int no;
    printf("Enter any number : ");
    scanf("%d",&no);
    ((no & 1) && printf("Odd")) || printf("Even");
}

i had used bitwise oferator instead;

thnxx for such questions. Please post more such questions looking for more articles from you. and I too have collected some of the tricky programming questions from my personal experience and from my friends. Please have a review on this and i hope that it will really help you to improve your logics. :)

Edited 3 Years Ago by deceptikon: Spammy link removed

3 without any bitwise operators simple way with loops

#include<stdio.h>
main
{
int n;
scanf("%d",&n);
while(n>2)
{
n=n-2;
}
if(n==2)
printf("even");
else
printf("odd");
}

first 3 problems taken care in one..

#include<stdio.h>
#define prog main
void prog()
{ 
  if((6&1==1?printf("number is odd"):printf("number is even")))

}

Edited 2 Years Ago by Maheshkumar123

answer for q3;

#include<stdio.h>
    main()
    {
    int num;
    printf("enter a number\n");
    scanf("%d",&num);
    for(;num!=0 && num!=1;num-=2);
    if(num==1)
    {
    printf("num is odd\n");
    }
    else
    {
    printf("num is even\n");
    }
    }

I don't normally resurrect dead threads, but I will make an exception.
These types of esoteric programming problems are often frowned upon for teaching bad programming practices. This is not their purpose.
As the IOCCC explains, these types of problems promote thinking into what might occur that would be bad to see. Awareness of these esoteric features is the goal, not promotion of them.

Now to nail the coffin of people posting their solutions to individual problems in this thread, here is #1-4 in standard compliant C89/99/11:

#include <stdio.h>
#include <stdlib.h>
#define hand ma##in
int hand(int argc, char *argv[]) {
    if (printf("Hello, world!\n")||
        scanf("%d",&argc)){}
    if (argc&1){if(printf("Even")){}}
    else       {if(printf("Odd")){}}
    while (exit(0),0[argv]){}
}

Some truly useful examples of these techniques:

char toHexDigit(int x)
{
    if (x>15) return '\0';
    return x["0123456789ABCDEF"];//legit, even though x is not an array!
}

#define DEBUG(item,desc) (printf(#item ":\t" desc),0)//example
if (!ERROR_HAS_OCCURRED ??!??! DEBUG(ERROR_HAS_OCCURED,"Why?!"))
//you can replace ??!??! with ||, but ??!??! is funny
{
    //deal with error-free code here
}
else
{
    //deal with error-heavy code here
}
#include<stdio.h>
void main()
{
int i,j,k=0,n=8100;
char s[100],t[100];
clrscr();
k=n&1;
if(k==1)
printf("%d is odd number",n);
else
printf("%d is even no",n);

getch();
}
#include<stdio.h>
#define prog main
int prog(int argc,char *argv[])
{ 
  if (printf("Hello world\n") && scanf("%d",&argc)){}
  if((argc&1==1?printf("number is odd"):printf("number is even"))){}
}

Edited 1 Year Ago by Ahamed_1: Simplified to #1-4 cheers.....

This article has been dead for over six months. Start a new discussion instead.