Welcome back, visitor. Today, I’ve come across a question **check if given number contains a digit** that is frequently asked in internship interviews. One of my friends encountered this question during his internship interview. Let’s delve into the solution to check whether given number contains a digit or not.

If you’re a new visitor seeking questions commonly asked in interviews, you’re in the right place. Our main aim is to assist programmers preparing for their technical interviews. We frequently share commonly asked questions on this blog. If you haven’t already, I highly recommend checking out our pattern and pyramid questions. It’s advisable to solve those before heading to an interview.

## Table of Contents

## Understand Question

Let’s begin by comprehending the question before delving into the coding aspect. It’s a best practice to grasp the question thoroughly and devise a logical approach in your mind on how to solve it.

If you comprehend the question, please comment out whether you’re able to check if a given digit is part of a number.

First, let’s break down the question. The interviewer provides you with a number, and your task is to determine whether a specified digit is a part of the given number. In other words, if the interviewer gives you a number—let’s assume it’s 2345—you need to identify whether the digit 2 is present in this number. If you can find it, return true; otherwise, return false.

## Create Logic to check a number contains a digit

Create a blueprint for your program. Now that you understand the interviewer will provide you with two numbers, you need to create two variables first. Then, to check if a digit is contained in the number or not, you must have a boolean variable to set true or false. Now you have all the required variables to begin the logical part.

For our regular visitors, the logic might seem quite simple if you’ve already visited our ‘Sum of Digits Program in C#‘ post. If you haven’t visited that post, don’t worry; I’m going to summarise that logic again.

To find the digit, your logic approach should be as follows: you need to remove a digit from the given number, and in each iteration, you have to check whether it is the number we are looking for or not. To find the remainder digit from the number, you must use the modulo (%) sign to divide your given number by 10. The result of that operation will give you a remainder, which is your digit.

Now, divide your given number by 10 to remove it from the given number. Then, add a condition: if the remainder is the digit we are looking for, then break the iteration loop, assign the boolean to true. Finally, check if the boolean is true or false, and display your logs accordingly

## Program to Check if given number contains a digit

For the code part, I won’t explain how to find the remainder and remove it from the given number because we already covered that in the ‘Sum of Digits Program in C#‘ post.

### Step 1

Create two integer variables named ‘givenNumber’ and ‘digitToCheck’, and create a boolean variable named ‘isContain’ and assign it to false. These variables are the primary variables we covered in the previous discussion.

```
static void Main(string[] args)
{
```**int givenNumber = 3242;
int digitToCheck = 3;
bool isContain = false;**
}

### Step 2

In the second step, we are going to find the remainder and remove the last value from ‘givenNumber’

```
static void Main(string[] args)
{
int givenNumber = 3242;
int digitToCheck = 3;
bool isContain = false;
```**while (givenNumber > 0)
{
var reminder = (givenNumber % 10);
givenNumber /= 10;
}**
}

### Step 3

Now, check if ‘remainder’ is equal to our ‘digitToCheck’ value. If yes, then break our iteration and check if it’s true or false, displaying logs accordingly. If not, the iteration will continue to the next digit until the ‘givenNumber’ becomes less than 0. If you are unable to find the ‘digitToCheck’, then your boolean variable would default to false.

```
static void Main(string[] args)
{
int givenNumber = 3242;
int digitToCheck = 3;
bool isContain = false;
while (givenNumber > 0)
{
var reminder = (givenNumber % 10);
```**if (reminder == digitToCheck)
{
isContain = true;
break;
}**
givenNumber /= 10;
}
**if (isContain)
{
Console.WriteLine("Yes given number contain given digit");
}
else
{
Console.WriteLine("No given number does not contain given digit");
}**
}

## Summarizing The program to check given number contains a digit

The task involves determining if a given digit is present in a provided number. First, two integer variables, ‘givenNumber’ and ‘digitToCheck’, along with a boolean variable named ‘isContain’, are initialized. ‘isContain’ is set to false initially. The approach includes iteratively extracting the last digit of ‘givenNumber’ using the modulo operation and comparing it with ‘digitToCheck’. If they match, the boolean variable is set to true, and the iteration breaks. If no match is found, the iteration continues until ‘givenNumber’ becomes less than 0. If ‘digitToCheck’ is not found, ‘isContain’ remains false.