Home

Wednesday, January 7, 2015

Sum of digit in a number in AX

static void sumOfDigit(Args _args)
{
  int n = 12345, t, adds = 0, remainder;
    t = n;
    while(t!=0)
    {
        remainder = t mod 10;
        adds =adds + remainder;
        //t = t/10; or
          t = t  div 10;
       
    }
 
   info(strFmt("Sum of digits of %1 is:-\n %2",n,adds));
}

===============================================
static void adddigits(Args _args)
{
    str a,b,c,d;
    int w,x,y,z,total, h, l, i;
   /*
    a = subStr("1234",1,1);
    b = subStr("1234",2,1);
    c = subStr("1234",3,1);
    d = subStr("1234",4,1);
    w = any2int(a);
    x = any2int(b);
    y = any2int(c);
    z = any2int(d);
    info(strFmt("%1",w+x+y+z));
  */
    h = 123;

    a = int2str(h);
    l = strLen(a);

    for(i=1; i<=l; i++)
    {
        w = w + any2int(subStr(a,i,1));
    }

    info(strFmt("%1", w));
}

No comments:

Post a Comment