Check if number is divisible by 24 [closed] - c#

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.

Related

Why is Math.Round() not resulting as I expect in C# [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 1 year ago.
Improve this question
Why does Math.Round(1.444445M, 2, MidpointRounding.AwayFromZero) return 1.44 instead of 1.45?
Because this is how rounding works.
Like #Sweeper said, 1.444445 is closer to 1.44 than to 1.45.
You take a number 1.444445.
Now you want to round it to 2 decimal places, so select 2 digits after dot: 1.[44]4445
Then look at the next digit after [44], which is 4 also
4 < 5, so rounding should not be applied. Edit: I mean it will stay the same [44], rest of number will be zeroed ofcourse
Your expectation seems to be similar to this recursive function that will result in 1.45 after a sequence of Math.Round.
double RecursiveMathRound(double val, int precisionLevel, int decimalPlace)
{
return (precisionLevel <= decimalPlace)
? Math.Round(val, precisionLevel, MidpointRounding.AwayFromZero)
: RecursiveMathRound(Math.Round(val, precisionLevel, MidpointRounding.AwayFromZero), precisionLevel - 1, decimalPlace);
}
RecursiveMathRound(1.444445d, 5, 2);
However, any form of rounding is less 'accurate' or 'correct' then a value that was not rounded at all. The RecursiveMathRound snippet above produces a less accurate or correct approximation because of the series of rounding occurrences.

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.

What relevance is 1.307 to the series 1 + 1/2 + 1/3 + 1/4... + 1/n [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 4 years ago.
Improve this question
I'm currently working my way through the Nakov book, Fundamentals of Computer Programming in C#. In Chapter 4 question 12 states:
Write a program that calculates the sum (with the precision of 0.001) of the following sequence: 1 + 1/2 - 1/3 + 1/4 - 1/5 + …
It seemed to me to be a relatively straightforward question. The series is a diminishing fraction that does not have an asymptote. Stopping the loop at a certain point due to diminished changes in value meets the precision requirements AFAIC. However, the solution given in both the Hungarian and English versions of the book makes reference to an obscure (to me) value of 1.307. As follows:
Accumulate the sum of the sequence in a variable inside a while-loop (see the chapter "Loops"). At each step compare the old sum with the new sum. If the difference between the two sums Math.Abs(current_sum – old_sum) is less than the required precision (0.001), the calculation should finish because the difference is constantly decreasing and the precision is constantly increasing at each step of the loop. The expected result is 1.307.
Can someone explain what this might mean?
Note that header contains "harmonic sequence" that has no limit.
But question body shows alternate sign sequence that converges towards value 2 - ln(2)
The expected result is 1.307.
I think they are simply saying what the result of the calculation is, so you can check your answer.
The sequence you've got
1 + 1/2 - 1/3 + 1/4 + ...
is the same as the Alternating Harmonic Series on Wikipedia, except with the signs from 1/2 onwards flipped:
1 - 1/2 + 1/3 - 1/4 + ... = ln 2
and the natural logarithm of 2, ln 2, = 0.693. Hence your 1.307 here = 2 - ln 2.

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);
}

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

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.

Categories

Resources