Saturday 30 November 2013

Reverse number logic using integer

Reverse number logic using integer :


        public static int reverseNumber(int i)
        {
            int temp = 0;
            while (i > 10)
            {
                int num = i % 10;
                temp = (temp * 10) + num;
                i = i / 10;
            }
            temp = (temp * 10) + i;

            return temp;

        }

Find trailing zero of n factorial


Hello,
Here is small logic program to find trailing zero in n factorial. (n > 0). Here we are dividing the passing parameter with power of 5.
e.g .  Take 51.   How many trailing zero for 51 factorial. Let’s start. 
          Now Take 5  and divide 51 with 5 will ans. = 51/5 = 10  let  save  10 in variable.
          Now increase power of 5 = 52  = 25 and divide 51 with 25 = 51/25 = 2.
         Add ans in previous answer 10+2 = 12
         Now increase power of 5 = 53 = 125.  Now check 51/124  < 0.
         so it is not possible more to divide 51.
          So  your answer  = 12

Here is C# implementation for this logic.

        public static double ZeroCnt(int num)
        {
            int cnt, pow, number;
            cnt = 0;
            pow = 1;
            number = 5;
            while (number < num)
            {             
                cnt +=  num / number;
                pow ++;
                number = Convert.ToInt32(Math.Pow(5, pow));
            }
            return cnt;
        }


Saturday 5 October 2013

Set Page as Home Page from Javascript



function setHomepage() {
if (document.all) {
   document.body.style.behavior = 'url(#default#homepage)';
   document.body.setHomePage('http://www.google.co.in');
}
else if (window.sidebar) {
   if (window.netscape) {
      try {
          netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
      }
      catch (e) {
        alert("this action was aviod by your browser,if you want to enable,
please enter about:config in your address line,and change the value of signed.applets.codebase_principal_support to true");
      }
   }
var prefs = Components.classes['@mozilla.org/preferences-service;1']
                      .getService(Components.interfaces.nsIPrefBranch);
prefs.setCharPref('browser.startup.homepage', 'http://www.google.co.in');
  }
}

Wednesday 11 September 2013

Make your own JQuery plugin


JQuery plugin :   

        (function ($) {
            $.fn.HelloWorld = function (e) {
                alert(e);
            }
        }(jQuery)); 

Use Plugin :
 
        $(document).ready(function () {
            $("#temp").HelloWorld("Test Plugin");
        })

Thursday 5 September 2013

Multicasting in Delegate



Multicast Delegates :
    Delegate multicast is that we can point more than one function at a time. A multicast delegate maintains a list of functions that will all be called when the delegate is invoked

    public class MyClassName
    {
        public delegate void delgName();
        public void main()
        {
            delgName dgt = new delgName(main1);
            dgt += new delgName(main2);
            dgt();
        }
        public void main1()
        {
        /// this will called first
        }
        public void main2()
        {
        /// this will called secondly
        }
    }

Thursday 22 August 2013

Signature control using Canvas



<script type="text/javascript">
        var ctx, cnvs;
        $(document).ready(function () {
            cnvs = document.getElementById("cnvs");
            ctx = cnvs.getContext("2d");

            cnvs.onmousedown = function (e) {
                ctx.beginPath();
                ctx.moveTo(e.offsetX, e.offsetY);
                cnvs.onmousemove = function (e) {
                    ctx.lineWidth = 2;
                    ctx.lineTo(e.offsetX, e.offsetY);
                    ctx.stroke();
                }
            }
            cnvs.onmouseup = function (e) {
                cnvs.onmousemove = null;
            }

            $("#btnSave").click(function () {               
                var imgUrl = cnvs.toDataURL("");
                $("#imgSign")[0].src = imgUrl;
            });
        });       
    </script>

<div>
   <canvas id="cnvs" width="300" height="200" style="border: 1px solid #808080"></canvas>
  <input type="button" value="Save" id="btnSave"/>
  <br />
  <img id="imgSign" />
</div>