We are excited to announce that the ASP.NET Forums are moving to the new Microsoft Q&A experience. Learn more >

How to make function remove one digit 5 from number ? RSS

7 replies

Last post Apr 29, 2021 09:59 AM by yij sun

• ahmedbarbary

Member

42 Points

378 Posts

How to make function remove one digit 5 from number ?

I need to write function by c# that given Integer N return maximum Possibe value obtainable by deleting one '5' digit

from decimal representation of N .

it is guranteed that N will contain at least one '5' digit

Given N=15958, the function should return 1958

Given N=-5859, the function should return -589

Given N=-5000, the function should return 0

because after remove 5 from digit will give me multiple 0 so value will be 0
assume that

N is integer within range [-999,995 and 999,995]

N will contain at least one '5' digit on decimal representation

N consist at least two digit on decimal representation

• PaulTheSmith

Participant

1680 Points

965 Posts

Re: How to make function remove one digit 5 from number ?

You have told us that you have an assignment to do.  You have not told us about a problem.

What code do you have?  What is not working?  Where are you stuck?

(Please do my job/homework is not a question!)

• ahmedbarbary

Member

42 Points

378 Posts

Re: How to make function remove one digit 5 from number ?

what i try is :

``` static int MaximumPossible(int num, int digit)
{
// edge case
if (num == 0)
{
return digit * 10;
}

// -1 if num is negative number else 1
int negative = num / Math.Abs(num);
// get the absolute value of given number
num = Math.Abs(num);
int n = num;
// maximum number obtained after inserting digit.
int maxVal = int.MinValue;
int counter = 0;
int position = 1;

// count the number of digits in the given number.
while (n > 0)
{
counter++;
n = n / 10;
}

// loop to place digit at every possible position in the number,
// and check the obtained value.
for (int i = 0; i <= counter; i++)
{
int newVal = ((num / position) * (position * 10)) + (digit * position) + (num % position);

// if new value is greater the maxVal
if (newVal * negative > maxVal)
{
maxVal = newVal * negative;
}

position = position * 10;
}

return maxVal;
}
}
}```

but it not remove 5 from number

• PaulTheSmith

Participant

1680 Points

965 Posts

Re: How to make function remove one digit 5 from number ?

1. Place a breakpoint on the first line of the code.

2. Run the code. Execution will stop at the first line.

3. Make a single step.

4. Check the values of all local variables.

5. Are they correct?

6. If they are correct go to 3

7. Change the code to fix the problem in the statement that was just executed

8. Go to 2

• PaulTheSmith

Participant

1680 Points

965 Posts

Re: How to make function remove one digit 5 from number ?

I can't help but notice that this thread is called '... remove one digit ...' but the code comment says "... place digit at every possible position ..."

• ahmedbarbary

Member

42 Points

378 Posts

Re: How to make function remove one digit 5 from number ?

I need to remove

so if possible can you help me modify function above for remove please

• PaulTheSmith

Participant

1680 Points

965 Posts

Re: How to make function remove one digit 5 from number ?

If you want to remove then you should write some code that at least tries to remove rather than add.  My advice is not to take a random piece of code which does the opposite of what yo want to achieve.

Get a blank piece of paper and a rubber duck.  Explain to the duck how they could solve the problem without any use of a computer. Develop an algorithm.

Write code that does what you have explained to the duck.  Test it.  Fix it.

• yij sun

Contributor

4220 Points

1671 Posts

Re: How to make function remove one digit 5 from number ?

Apr 29, 2021 09:59 AM|yij sun|LINK

Hi ahmedbarbary,

As far as I think,you should do this:

1.find the position of '5'.

2.get the value based of the position.

3.do the function again of other possible.

More details,you could refer to below article:

https://www.geeksforgeeks.org/largest-number-possible-after-removal-of-k-digits/

Best regards,

Yijing Sun

ASP.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >