web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

How to find Sum of odd and even characters of string

(0) ShareShare
ReportReport
Posted on by
Hi, 
 
I have a requirement where I have a string input where I have to find the sum of all even characters and multiply it with 3 and similarly find sum of all odd characters and multiply it with 1 and then sum all of it together and find mod of 10(i.e. if sum of odd + even = 44 then 44 mod 10 = 6, i should get 6 as the output)
How do i achieve the same using x++ code?
 
 
I have the same question (0)
  • Martin Dráb Profile Picture
    239,029 Most Valuable Professional on at
    What do you mean by a sum of characters? Do you expect numbers only and no other characters? Or do you want to support other characters too (e.g. letter, punctuation or so) and somehow convert them to numbers?
     
    Regarding how to iterate characters, this is a native X++ way:
    for (int i = 1; i <= strLen(inputString); i++)
    {
        subStr(inputString, i, 1);
    }
    Another option is using .NET classes. You could use String.ToCharArray() to convert the string to an array of characters, which then allows you to write logic for indexes that are divisible by two (mod 2) and the rest.
  • CU02071804-0 Profile Picture
    on at
    Hi Martin my requirement says that 
     
    if strinput = 2143658790
     
    then evenSum = 1+3+5+7 +0 = 16
            evenmultiply = 16*1 = 16
     
            oddSUm = 2+4+6+8+9=  29 
            oddmultiply = 29*3 = 87
    then,
     
           sum = ((16+87) mod 10)   =    (103 mod 10) = 7
       hence sum = 7
     
    This s the mathematical overview of my requirement how do i achieve it through x++     
     
  • Verified answer
    Martin Dráb Profile Picture
    239,029 Most Valuable Professional on at
    I already showed how to get the characters, therefore there are just a few small things to add:
    str input = '2143658790';
    str numChars = strKeep(input, '0123456789');
    
    if (strLen(input) != strLen(numChars))
    {
        throw error("Input contains invalid characters.");
    }
    
    int oddSum;
    int evenSum;
    
    for (int i = 1; i <= strLen(numChars); i++)
    {
        int digit = str2Int(subStr(numChars, i, 1));
    
        if (i mod 2)
        {
            evenSum += digit;
        }
        else
        {
            oddSum += digit;
        }
    }
    
    int checksum = (evenSum + oddSum * 3) mod 10;
  • CU02071804-0 Profile Picture
    on at
    Thanks Martin
     
    this solves the problem
  • CU02071804-0 Profile Picture
    on at
    Hi Martin how would i achieve it if 
     
    if strinput = 2143658790
     
     even str1 = 1*1 =1
     even str2 = 3*1 = 3
    even str3 = 5*1 = 5
    even str4 = 7*1 = 7
    3v3n str5 = 0*1 = 0
    then evenSum = 2+3+5+7+0 = 16
     
    odd str1 = 2*3 = 6
    odd str2 = 4*3 = 12
    odd str3 = 6*3 = 18
    odd str4 = 8*3 = 24
    odd str5 = 9*3 = 27
     
      then oddSum = 6+12+18+24+27 = 87
           
    then,
     
           sum = ((16+87) mod 10)   =    (103 mod 10) = 7
       hence sum = 7
     
    This s the mathematical overview of my requirement how do i achieve it through x++  
  • Martin Dráb Profile Picture
    239,029 Most Valuable Professional on at
    I'm sorry, but it's not clear to me what problem you have.
     
    It seems to be the same thing that we discussed before and you confirmed that the code I gave you did the job.
  • CU02071804-0 Profile Picture
    on at
    Yes Martin the code you gave did the job all i am asking you if we have scenario like the one below how ill we proceed with the code, can you please let me know the x++ code for the scenario.
    if strinput = 2143658790
     
     even str1 = 1*1 =1
     even str2 = 3*1 = 3
    even str3 = 5*1 = 5
    even str4 = 7*1 = 7
    3v3n str5 = 0*1 = 0
    then evenSum = 2+3+5+7+0 = 16
     
    odd str1 = 2*3 = 6
    odd str2 = 4*3 = 12
    odd str3 = 6*3 = 18
    odd str4 = 8*3 = 24
    odd str5 = 9*3 = 27
     
      then oddSum = 6+12+18+24+27 = 87
           
    then,
     
           sum = ((16+87) mod 10)   =    (103 mod 10) = 7
       hence sum = 7
     
    This s the mathematical overview of my requirement how do i achieve it through x++  
  • Martin Dráb Profile Picture
    239,029 Most Valuable Professional on at
    Please explain the difference. It looks identical to me.
  • CU02071804-0 Profile Picture
    on at
    i understand here the calculation looks identical but if we consider the scenario of bar code calculation then the output might differ, so considering that i wanted to know the x++ code
  • Martin Dráb Profile Picture
    239,029 Most Valuable Professional on at
    I'm sorry, but I still don't understand what you want from me. You'll have to be much more specific if you want to get an answer.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 617

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 461 Super User 2026 Season 1

#3
Syed Haris Shah Profile Picture

Syed Haris Shah 298 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans