Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having a number let us assume i.e 345624354 and as the index of this number is 012345678 i would like to add them as 3+0, 4+1 5+2 and so on any idea please
Posted
Updated 29-Oct-10 22:30pm
v2
Comments
OriginalGriff 30-Oct-10 4:58am    
"Can i have an detailed one please - Dorababu743"
What detail do you want? How to add two numbers together?

/// <summary>
    /// Gives Number with each digit added with it's according index
    /// </summary>
    /// <param name="num">Number you want to have Index adding with each digit i.e 345624354</param>
    /// <returns>returns number with index added i.e 35796991212</returns>
    private long GetMyNo(int num)
    {
        string input = num.ToString();
        StringBuilder output = new StringBuilder();
        int i = 0;
        foreach (char c in input)
        {
            output.Append(int.Parse(c.ToString()) + (i++));
        }
        return long.Parse(output.ToString());
    }


Please vote and Accept Answer if it Helped.
 
Share this answer
 
Comments
demouser743 30-Oct-10 5:41am    
Hi small help i would like to display only 1,2 when i have 10,11,12, and so on
demouser743 30-Oct-10 5:51am    
Got it
Either split the number into the various digits using a loop and divide / remainder, or convert it to a string and use a foreach loop on the characters:
while (myInt > 0)
   {
   myDigit = myInt % 10;
   myInt /= 10;
   ... deal with myDigit ...
   }

string myNumber = myInt.ToString();
int index = 0;
foreach (character c in myNumber)
   {
   ... deal with each digit ...
   }
 
Share this answer
 
Comments
demouser743 30-Oct-10 4:52am    
Can i have an detailed one please

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900