# COME ON CODE ON

A blog about programming and more programming.

## 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

Written by fR0DDY

April 8, 2010 at 10:05 AM

Posted in Beautiful Codes

Tagged with , ,

with one comment

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

Written by fR0DDY

August 7, 2009 at 2:35 PM

Posted in Beautiful Codes

Tagged with , , , , , ,

## 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 = "<

Written by fR0DDY

July 21, 2009 at 9:35 PM

Posted in Beautiful Codes

Tagged with , , ,

## Bit Play

The first one is to add two integers using bit operators.
{
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

Written by fR0DDY

February 23, 2009 at 5:13 AM

Posted in Beautiful Codes

Tagged with ,

## Program without main() ?

with one comment

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

Written by fR0DDY

February 18, 2009 at 12:54 PM

Posted in Beautiful Codes

Tagged with ,

## 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;
}
&#91;/sourcecode&#93;
And Python Solution :
&#91;sourcecode language='python'&#93;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

Written by fR0DDY

February 17, 2009 at 5:18 PM

Posted in Beautiful Codes, Programming

Tagged with ,

## 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

Written by fR0DDY

February 13, 2009 at 9:48 AM

Posted in Beautiful Codes

Tagged with