Note :This post is first published on Apr-2013 in my previous blog Techkindle. Moving the content here. Below code, without using BigInteger structure. https://gist.github.com/gopigujjula/5412295 Output Happy Learning :)