Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
This is a code that counts the number of digits in a number recursively. What can I add to this code to count how many different digits are in a number? Or maybe there is some other way?
int numberOfDigits(int n)
{
if(n==0)
return 0;
else
return numberOfDigits(n/10)+1;
}
Use sets!
static int NumberOfDigits(int a) {
return new HashSet<char>(Math.Abs(a).ToString()).Count;
}
We make a into a string and then turn the string into a set of characters. Since sets cannot contain duplicate values, the count of the set is the number of distinct digits.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
the syntax:
(9<result<16)
is appearing to be invalid when I'm using it in my C# code. I want the else if statement to consider the condition: when the sum of two integers is greater than 9 and less than 16, and return the line :"That's a poor grade" as a result. But the problem is that writing the condition as (9
Screen shot of code to give you context
Edit: ignore the following as I'm writing all this to make the edit more substantive, so that my post can be corrected
You can convert to
else if (9 < result && result < 16)
Also, you need return int value in Add method as
public static int Add (int num01, int num02){
//Console if need
return num01 + num02;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Thank you in Advance:
I have a parameter which gives the row_count after loading the data.
i need to insert that value into a file, which i am able to do it.
But I need the format in this manner:
if the row_count is 150 then
I am getting only Param name='INPUT_NUM_RECS value=150/
But I need the output as
Param name='INPUT_NUM_RECS value=000000150/
Thank you.
Assuming that 150 is an int variable name num.
int num = 15;
string output = $"Param name='INPUT_NUM_RECS value={num:D9}"
Here using D9 we will place the int value with leading zeroes if the number of digits in num is less than 9.
Number of leading zeroes = 9 - digits in num
If number of digits in num is more than 9, num will not be truncated
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
i have two textboxes 1 called 'mtb_NETPAIE02' and 2 called 'mtb_TAXE02'
the format in the two textboxes is currency/money (double) i want to fix the error on this Code :
if (double.Parse(mtb_NETPAIE02.Text) >= 100001 )
{
mtb_TAXE02.Text = (double.Parse(mtb_NETPAIE02.Text) / 5000 * double.Parse("12")
+ double.Parse("18").ToString("N2")).ToString();
}
12 (12,00) and 18 (18,00) are money but 5000 is int .
how to make this right ?
First of all, you should not be using double if you're working with money; you should be using decimal.
Secondly, you should use the literals for decimal, i.e. with the M suffix.
Finally, you are attempting to add the result of decimal.Parse("18") to the other numeric values, but you are converting it to a string first via ToString("N2"). You should move the latter outside your parens to convert the whole result from the calculation into a string:
if (decimal.Parse(mtb_NETPAIE02.Text) >= 100001M )
{
// notice the `M` in 5000M
mtb_TAXE02.Text = (decimal.Parse(mtb_NETPAIE02.Text) /
5000M * decimal.Parse("12") + decimal.Parse("18")).ToString("N2");
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I want to check for a match in a string such that the string ends in a dash and then two digits. Therefore "bill-01" will be a match, as will "jared-43" but "josh" and "allen47-" won't. I just need a bool true/false to tell if a match was found for the given string.
Thank you.
this will match anything as long as it ends with - followed by two numbers
.*-[0-9]{2}
http://txt2re.com/
http://www.regexr.com/
http://en.wikipedia.org/wiki/Regular_expression#POSIX_basic_and_extended
Have fun
string pattern = #".*-\d{2}";
Match match = Regex.Match(arg, pattern);
Console.Write("{0} ", arg);
if (match.Success)
{
Console.WriteLine("Matched");
}
else
{
Console.WriteLine("did NOT Match");
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Quick question.
I'm making a diagram so the numbers i'm passing to the function can be anything (depending on zoom and such). Lets say I want to round the number 3.086948353 to 3 but i still want other numbers like 2.199999999 to round to 2.2.
Currently it just looks like this:
Math.Round(value, 10)
You want to do two things in single shot:
Get the integer value if first digit after decimal is 0
Get the rounded value upto last 2 digits if its first digit after decimal is not 0.
For second option you can use:
newValue = Math.Round(value, 2)
Now comes the first requirement:
Once you get the decimal with 2 digits after decimal, get last two digits:
int decimalValue= (int)((newValue - (int)newValue ) * 100);
if(decimalValue < 10)
{
newValue = Math.Floor(value);
}