## Archive for the ‘**Beautiful Codes**’ Category

## Multiply two Numbers Without Using * Operator

Lets have a fun question. **Multiply two numbers without using the * operator.**

Here’s the code in C/C++:

main(a,b,m) { while (~scanf("%d%d",&a,&b)) { m=0; while (a) { if (a&1) m+=b; a>>=1; b<<=1; } printf("%d\n",m); } }

The above code is a implementation of an algorithm better known as the Ethiopian Multiplication or the Russian Peasant Multiplication.

Here’s the algorithm :

```
```
- Let the two numbers to be multiplied be a and b.
- If a is zero then break and print the result.
- If a is odd then add b to the result.
- Half a, Double b. Goto Step 2.

NJOY!

-fR0DDY

## Program without header file?

So continuing in the series of program without, here are two more programs. The first one is just a valid C program without any header file.

int main() { return 0; }

The above program will actually compile and run. But you will say it did nothing. So lets have a program without header file which prints something.

extern "C" { int printf(const char *format,...); } int main() { printf("Hello World"); return 0; }

The only unexplained feature is extern “C”. For more about it read this.

NJOY!

fR0D

## Programs without semicolons?

Let’s have a little fun. First of all try to write a C code to print Hello World! without using semicolons. Here’s the answer in C

#include <stdio.h> int main() { if (printf("Hello World!\n")) {} }

or if you are a hardcore C++ fan then

#include

int main()

{

if (std::cout << "Hello world!" << std::endl)
{}
}[/sourcecode]
But the real gem is the following program. A program to find the factorial of a number.
[sourcecode language='cpp']#include

int main(int n)

{

if (std::cin>>n)

{

if (int f=1 )

{

while (n != 1)

{

if ( f = n * f )

{

if (–n){}

}

}

if(std::cout<<"fac = "<

## Bit Play

The first one is to add two integers using bit operators.

int add(int a,int b)

{

if(!a)

return b;

else

return(add((a&b)<<1,a^b));
}[/sourcecode]
The next is to find maximum and minimum of two numbers :

r = y + ((x – y) & -(x < y)); // min(x, y) r = x – ((x – y) & -(x < y)); // max(x, y) where r is the result.

NJOY!!!

-fR0D

## Program without main() ?

See this code first :

#include"stdio.h" #define decode(s,t,u,m,p,e,d) m##s##u##t #define begin decode(a,n,i,m,a,t,e) int begin() { printf(" hello "); }

Compile and run this. It works fine but how?Actually its a very good example of the **preprocessor directive #define** and **token pasting or token merging operator ‘##’**. Kool program isn’t it?

-fR0D

## Size Contest

Looks like i am having some affair with shorter codes. Anyways, there is this question on SPOJ. You have to write the shortest code for finding the sum of all positive integers,given a set of T integers followed by T.

As always i am far away from optimal result :

#include"iostream" int a,t,s; main() { for(std::cin>>t;t--;std::cin>>a,s+=a>0?a:0); std::cout<<s; } [/sourcecode] And Python Solution : [sourcecode language='python']t=input() s=0 while t: x=input() s+=x*(x>0) t-=1 print s

If you have a shorter C++ or Python code, help me out. Between try this line to see the path of your file.

printf("The source file name is %s\n",__FILE__);

-fR0D

## Binary Palindromes

There is this question from a programming competition which requires you to find whether the binary representation of a number is palindrome or not. The input begins with integer T followed by T cases.

The obvious code would be(my solution) :

main(v,r,t,x) {scanf("%d",&t);while(t--){ for(scanf("%d",&v),x=v,r=0;v;r<<=1,r|=v&1,v>>=1){} puts(r-x?"NO":"YES");}}

But there were better(shorter) solutions which used recursive main functions :

a,t;main(c,b) {c?main(c/2,b*2+c%2): t++/2&&puts(a-b?"NO":"YES"), ~scanf("%d",&a)&&main(a,0);}

-fR0D