what is condition to find digits in java program
Answers
Answer:
Perhaps the easiest way of getting the number of digits in an Integer is by converting it to String, and calling the length() method. This will return the length of the String representation of our number: ? int length = String.valueOf(number).length()
Answer:
Explanation:
Given two integer number n and d. The task is to find the number between 0 to n which contain the specific digit d.
Examples:
Input : n = 20
d = 5
Output : 5 15
Input : n = 50
d = 2
Output : 2 12 20 21 22 23 24 25 26 27 28 29 32 42
Take a loop from 0 to n and check each number one by one, if the number contains digit d then print it otherwise increase the number. Continue this process until loop ended.
// C# program to print the number which
// contain the digit d from 0 to n
using System;
class GFG {
// Returns true if d is present as digit
// in number x.
static bool isDigitPresent(int x, int d)
{
// Breal loop if d is present as digit
while (x > 0)
{
if (x % 10 == d)
break;
x = x / 10;
}
// If loop broke
return (x > 0);
}
// function to display the values
static void printNumbers(int n, int d)
{
// Check all numbers one by one
for (int i = 0; i <= n; i++)
// checking for digit
if (i == d || isDigitPresent(i, d))
Console.Write(i + " ");
}
// Driver code
public static void Main()
{
int n = 47, d = 7;
printNumbers(n, d);
}
}
// This code contribute by parashar.
Output:
The number of values are
7 17 27 37 47