Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I am trying to find Number that contains Digit 5.
For Example if value = 1999009, the method should return false
but if value = 501, the method should return true.
Update:
Also it should work for negative numbers too:
if value = -9005 it should return True:


I tried to complete this exercise, however when I Run Tests in Solution Explorer
Tests go through infinite Loop - Screen: 1z — ImgBB[^] . My solution should contain int property Digit and method bool Verify(int value). Also it is recommended that recursion should be used to complete the task.

Thank You

What I have tried:

public int _digit;
        public int Digit
        {
            get => _digit;
            set
            {
                if ((value == 5) // I think something is wrong here)
                {
                    _digit = value;
                }
                            }
        }

        public bool Verify(int value)
        {
            bool isFive = false;
            while (value != 0)
            {
                int _digit = value % 10;
                if (_digit == 5)
                {
                    isFive = true;
                }
                value /= 10;
            }

            return isFive;
        }
Posted
Updated 5-Feb-22 13:07pm
v8

Look at your code:
C#
public bool Verify(int value)
{
    bool isFive = false;
    while (value != 0)
    {
        int _digit = value % 10;
        if (_digit == 5)
        {
            isFive = true;
        }
    }

    return isFive;
}
Where inside your loop do you change value?
If value doesn't change, then whenever you call Verify with a non-zero value it can never exit the loop, and thus never return.

Perhaps you want to divide value by ten inside the loop?

To be honest, 2 minutes running this with the debugger and you would have found it for yourself, and saved half an hour of your time ... if you get a problem in future, reach for the debugger first and see exactly what is happening while you code is running!
 
Share this answer
 
Comments
LuckyChloe 5-Feb-22 14:26pm    
Thank You OriginalGriff ♥. After adding value /= 10
6 out of 9 tests passed. I am now trying to figure out how to improve my code to pass all the tests. Debugger is not working inside the exercise but I created new console app and tested there.
OriginalGriff 5-Feb-22 14:58pm    
You're welcome!
To stay away from strings, like in M Imran Ansari solution, you need to do something like:
C#
int GetDigit(int number, int digit)
{
    return (number / (int)Math.Pow(10, digit - 1)) % 10;
}

To use:
C#
int x = 123456;

for (int i = 1; i <= 6; i++)
{
    Console.WriteLine(GetDigit(x, i));
}

which produces:
6
5
4
3
2
1
 
Share this answer
 
In your code, there is a an error and that is you are taking digit = value%10 and it always same value every time and loop condition remains always true. Change your code like:
public bool Verify(int value)
{
    bool isFive = false;
    while (value != 0)
    {
        int _digit = value % 10;
        if (_digit == 5)
        {
            isFive = true;
        }
        value = value / 10;
    }

    return isFive;
}

Secondly, You can also achieve this through String function. Use Contains method of String to find your number, like:
public static bool Verify(int value, string findingNo)
{
	bool isFive = false;
	while (value != 0)
	{
		if (value.ToString().Contains(findingNo))
		{
			isFive = true;
		}
	}
	return isFive;
}

Call Function like: Verify(501, "5");
 
Share this answer
 
v2
Comments
LuckyChloe 5-Feb-22 14:28pm    
Thank You.
What do you think Is my Digit property correct?
because 3 out of 9 tests are failing and can't figure out the reason.
LuckyChloe 5-Feb-22 14:39pm    
Ah I think what the problem is.
-70005 should also return True. Not false. WIll update my question.
LuckyChloe 5-Feb-22 14:49pm    
Solved. Thank You! I am very happy ♥
M Imran Ansari 5-Feb-22 14:58pm    
Pleasure. HaPpy Coding!!

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