I'm trying to iterate and generate values based on certain conditions. My start value is 7, and I am iterating over it 15 times so the output should be 7, 14, 21 etc. But basically I am trying to add 7 to the start value 15 times with one caveat. If the
last digit is 9, then I want to replace the 9 with a 6 and then start the iteration from the next value onwards. Such as 49 will become 46, then for the next iteration it'll be 46 + 7 for the new value which would be 53.

Any ideas on how I can go about achieving this ?

This is what I've tried so far, it replaces the last value of 9 to 6, but it doesn't add 7 to that value it just detects the last value of 9 and replaces to 6, so that's where I'm having trouble.

using System;
public class Program
{
public static void Main()
{
int count = 15;
int StartValue = 0;
int Add = StartValue;
int increment = 7;
for (int i = 0; i <= count; i++)
{
Add = Add + increment;
int FindNine = Add % (10);
if(FindNine == 9)
{ FindNine = 6;}
Console.WriteLine(FindNine);
}
}
}

Hi rankone.
From your description, I suggest you could use the code below:

int count = 15;
int StartValue = 0;
int Add = StartValue;
int increment = 7;
for (int i = 0; i <= count; i++)
{
Add = Add + increment;
int FindNine = Add % (10);
if (FindNine == 9)
{
FindNine = 6;
int resul= Add-9+ FindNine ;
Console.WriteLine(resul + "*");
}
else
{
Console.WriteLine(Add + "*");
}
}

Hope this can help you. If you have any question and confusion about the problem. Please don't hesitate to let me know.
Best regards
Cathy

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

I think this would work fine, however I have another caveat. How would I go about removing the second last digit from a value instead of the last ?

I think I could accomplish this with string.remove then position of the digit. However I want this to be implied on the next value, and not the value containing the 9. If that makes sense.

So basically I think this can be accomplished if I can find the last two digits of a value instead of just the last one.

Or it is still a computation ie you could subtract Value%10*10 to remove the dizain digit (if I understand what you want to do ie if you have 164 you want to get 104 ?)

Or it is still a computation ie you could subtract Value%10*10 to remove the dizain digit (if I understand what you want to do ie if you have 164 you want to get 104 ?)

I think I found the solution, which would mean basically converting it to a string and change the %10 to %100 for the last two digits. Then if it finds 16, to remove the 1 and retain the 6 by String.Remove function.

Member

13 Points

163 Posts

## Replace the last number/digit of a value

Sep 26, 2016 06:14 AM|rankone|LINK

I'm trying to iterate and generate values based on certain conditions. My start value is 7, and I am iterating over it 15 times so the output should be 7, 14, 21 etc. But basically I am trying to add 7 to the start value 15 times with one caveat. If the last digit is 9, then I want to replace the 9 with a 6 and then start the iteration from the next value onwards. Such as 49 will become 46, then for the next iteration it'll be 46 + 7 for the new value which would be 53.

Any ideas on how I can go about achieving this ?

This is what I've tried so far, it replaces the last value of 9 to 6, but it doesn't add 7 to that value it just detects the last value of 9 and replaces to 6, so that's where I'm having trouble.

Any help on this would be appreciated.

All-Star

48940 Points

18275 Posts

## Re: Replace the last number/digit of a value

Sep 26, 2016 12:20 PM|PatriceSc|LINK

Hi,

Try :

if(FindNine == 9) Add-=3; that is if the last digit is 9 you'll subtract 3.

Star

8680 Points

2882 Posts

## Re: Replace the last number/digit of a value

Sep 26, 2016 12:26 PM|Cathy Zou|LINK

Hi rankone.

From your description, I suggest you could use the code below:

Hope this can help you. If you have any question and confusion about the problem. Please don't hesitate to let me know.

Best regards

Cathy

Please remember to click "Mark as Answer" the responses that resolved your issue.

If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

Member

13 Points

163 Posts

## Re: Replace the last number/digit of a value

Sep 26, 2016 02:28 PM|rankone|LINK

I think this would work fine, however I have another caveat. How would I go about removing the second last digit from a value instead of the last ?

I think I could accomplish this with string.remove then position of the digit. However I want this to be implied on the next value, and not the value containing the 9. If that makes sense.

So basically I think this can be accomplished if I can find the last two digits of a value instead of just the last one.

All-Star

48940 Points

18275 Posts

## Re: Replace the last number/digit of a value

Sep 26, 2016 02:45 PM|PatriceSc|LINK

Or it is still a computation ie you could subtract Value%10*10 to remove the dizain digit (if I understand what you want to do ie if you have 164 you want to get 104 ?)

Member

13 Points

163 Posts

## Re: Replace the last number/digit of a value

Sep 26, 2016 02:47 PM|rankone|LINK

Basically if 9 + 7 = 16, Where the value is 16, I want to drop the 1 to make it 6.

Member

13 Points

163 Posts

## Re: Replace the last number/digit of a value

Sep 26, 2016 03:30 PM|rankone|LINK

I think I found the solution, which would mean basically converting it to a string and change the %10 to %100 for the last two digits. Then if it finds 16, to remove the 1 and retain the 6 by String.Remove function.

Participant

1690 Points

964 Posts

## Re: Replace the last number/digit of a value

Sep 27, 2016 07:26 AM|PaulTheSmith|LINK

What does

mean? It is certain than 9+7=16, there is no 'if'.If you want 16 to become 6 then subtract 10. Arithmetic works very well with numbers. Sting manipulation of numbers is not a good idea.