Can you explain me what this method does? [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
We found this method in a book.
From what I understand wants to check if a number is ordered ascending.
For example, the number 54321 (all figures have ordered ascending)
However, I do not understand how this method works ... why returns 0 or 1?
Can you explain to me in a simple way what happens in this method?
static int f(long n)
{
while(n>10)
{
if (n % 10 > n / 10 % 10) return 0;
n = n / 10;
}
return 1;
}

n % 10 gets you the digit in the unit's place and n / 10 % 10 gets you the digit in the ten's place.
The author is comparing these two digits and returning 0 if the digit in the unit's place is larger than the one in the ten's place.
If not, he is dividing n by 10 to discard the number in the unit's place. Now, the number that was in the ten's place is now in the unit's place and the one in the hundred's place is now in the ten's place and the previous steps are repeated.
If the number becomes less than or equal to 10 after you keep discarding the last number, if it hasn't returned 0, it will return 1, which suggests that all the digits in n are in descending order from left to right.

Related

C# infinite Loops [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 days ago.
Improve this question
Can someone help me understand why the loop below is infinite and how to make it finite?
int index = 1;
while (index != 10)
{
Console.WriteLine("Hello");
index += 2;
}
I tried putting it in Visual Studio but it did not run. I need to understand the logic. I am struggling with this course
It starts with index at 1 and increases by 2 (index += 2)
1
1 + 2
3
------:
-----:
3 + 2
5
------:
-----:
5 + 2
7
------:
-----:
7 + 2
9
------:
-----:
9 + 2
11
------:
-----:
11 + 2
13
Since you only stop at 10, it runs infinite.
(If you stop at >= 10, it will stop at 11)
Try using a debugger.
Visual studio will give you an error 'While' does not exist in the current context. Intellisense is your friend when learning a new language. Your error here is that While should be lower case while.
int index = 1;
while(index != 10)
{
Console.WriteLine("Hello");
index += 2;
}
Why is the loop infinite?
Set a breakpoint on the debugger to step through each line of the while loop. Hover over index to see it's new value upon each iteration. You'll see that your design only increments by odd numbers thus always satisfying your while condition index != 10.
There's many ways to make the loop finite. Start index at 0, or increment by one, or adjust your while condition. But I think the main thing from this question is that you need to brush up on c# syntax and VS intellesense\debugging. These tools will help you eleminate questions about the fundamentals of c#.

C# Best way to find the middle character(s) of a string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
What is the best way to find the middle character or characters of a string using C#
I know I could count how many characters are in the string then work out which index or indexs to get, but is there a better way, faster way, maybe regEx, something built in to C#
So if the input was "car" the output would be "a" but if the input was cars the out put would be "ar"
We first need to determine if the string has an even or odd number of characters.
If there's an odd number of characters, we take the integer division of Length / 2, and then take 1 character from that position.
But if there's an even number of characters, then we have to subtract 1 from the integer division of Length / 2, and we want to take 2 characters from that position.
In code we can use the modulus operator (%) to determine if the Length is even, and set an offset variable to the appropriate value (1 for even, 0 for odd), and then use that to get the start position and length of substring to take:
var offset = input.Length % 2 == 0 ? 1 : 0;
var middle = input.Substring(input.Length / 2 - offset, offset + 1);
You could do something like
string myString = "yourtexthere";
if(myString.Length % 2 == 0){
Console.WriteLine(myString.Substring((myString.Length / 2 ) - 1, 2));
}else{
Console.WriteLine(myString[myString.Length / 2]);
}
Although I'm sure there's a prettier way to do this.

Generating a nice amount of range elements between two numbers [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I'm trying to figure out a good way to write a little "algorithm", which would be able to find a mathematical a range between these two numbers:
Let's suppose maximum number is 1500 and minimum number would be 1;
By performing some sort of mathematical formula, method would be able to determine that best range between these two numbers is lets say 100;
So range would be:
100,200,300,400,500,600,700,800,900,1000,1100,1200,1300,1400,1500
Other example:
Maximum is 10, minimum 1;
Best range would be (let's say):
2,4,6,8,10
Are there any libraries in c# which offer this kind of solution or is there some neat mathematical formula used to determine this?
P.S. Guys there can be a remainder in the number as well...
I'm guessing I can divide the maximum number into let's say 7 fixed groups, and then just add up the divided number until I get the max value , no ?
Okay guys I've figured out an idea, lets suppose maximum number is a floating point number and is: 1326.44..., while the minimum is 132.5
I'm going to say that maximum range can be 7... So what I can do is divide 1326.44 with 7 and I'll get 189.49
So the first amount in range is:
var ranges = new[] { 132.5, 189.5 ... /*Now I just need to dynamically somehow add the rest of the range elements?*/ };
This is actually super easy. You have a min range value and a max range value, and you want a particular number of items in your range. Therefore, you simply need to calculate a step value, and then add that recursively to the minimum value until you're at the maximum value. For example:
var min = 132.5;
var max = 1326.44;
var count = 7;
var step = (max - min) / count;
var items = new List<double>();
for (var i = min; i <= max; i += step)
{
items.Add(i);
}

how do i display how many digits the number has? [closed]

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 7 years ago.
Improve this question
Currently very new to C# and coding , so i will be more than happy if someone will explain me how to display how many digits the number has. For example the number 12345 has 5 digits.the main theme in the class is while loops so the answer probably need to contain while loop.TY
You can either use this
Math.Abs(myint).ToString().Length
and if you absolutely must use a while loop then
number = Math.Abs(number);
int length = 1;
while ((number /= 10) >= 1)
length++;
To test code
string.Trim().Replace("-","").Length
so if you have a number you should make it a string first using ToString()
The Length returns the number of characters that you hold within your string minus your white spaces (Because of the Trim()),i don't see why you would want to use the while loop in the first place.
Edit : if you have a minus number the .Replace() will take care of that.

Check if number is divisible by 24 [closed]

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
I'd like to put up a if function, that will see if the variable is dividable by 24, if it's then it does the function else not, same logic however, I want to see if the output is a perfect number, e.g if we do 24/24 that will get 1, that's a perfect number. If we do 25/24 then it'll get 1.041 which is not a perfect number, the next perfect number will come when it reaches 48 that will be 48/24 which will get 2 that's a perfect number.
Use the Modulus operator:
if (number % 24 == 0)
{
...
}
The % operator computes the remainder after dividing its first operand
by its second. All numeric types have predefined remainder operators.
Pretty much it returns the remainder of a division: 25 % 24 = 1 because 25 fits in 24 once, and you have 1 left. When the number fits perfectly you will get a 0 returned, and in your example that is how you know if a number is divisible by 24, otherwise the returned value will be greater than 0.
How about using Modulus operator
if (mynumber % 24 == 0)
{
//mynumber is a Perfect Number
}
else
{
//mynumber is not a Perfect Number
}
What it does
Unlike / which gives quotient, the Modulus operator (%) gets the remainder of the division done on operands. Remainder is zero for perfect number and remainder is greater than zero for non perfect number.

Categories

Resources