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 9 years ago.
Improve this question
I am trying to use the Math.Floor method on 2 user-input numbers but whenever I try to use the Math.Floor with my input3 and input3a it just doesn't work. I've seen examples for already set numbers such as in an array but not of numbers that the user inputs. Any help/clarification would be awesome.
static void Minimum()
{
Console.WriteLine("\n Enter two numbers and I shall determine the Minimum:\n");
Console.Write("> ");
Console.Write("\n> ");
// Read and parse input
string input3 = Console.ReadLine();
double d_input3 = Double.Parse(input3.Trim());
string input3a = Console.ReadLine();
double d_input3a = Double.Parse(input3a.Trim());
// Determine minimum of numbers
Console.WriteLine("\nThe Number {0} and {1}.\n", d_input3, d_input3a);
}
If think you don't need Math.Floor to determine a minimum :
Returns the largest integer less than or equal to the specified
double-precision floating-point number.
If you need to determine the minimum of both numbers, use Math.Min instead
Console.WriteLine("\nThe minimum number between {0} and {1} is {2}.\n", d_input3, d_input3a, Math.Min(d_input3, d_input3a));
The Math.Floor function takes one parameter and returns the value rounded down:
d_input3 = Math.Floor(d_input3);
d_input3a = Math.Floor(d_input3a);
The Math.Floor method is however not used to determine the lower value of two values. For that you would use the Math.Min function instead:
double lowest = Math.Min(d_input3, d_input3a);
Related
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 years ago.
Improve this question
I am trying to round a number and the expected output isn't correct. Here's what I have tried:
var percent = Math.Round(1.13451, 0);
That above return's 1, but I want it to return 1.13 if the 3rd number is less than 5. If it's >= 5 (the third number) I want to get something like 1.135. I am sure it's something simple I am missing, but not sure.
It appears you are causing confusion because you're using the term "rounding" to describe an operation that is not actually rounding.
I've read your description again, and I can see that you're trying to truncate your values into the highest discrete increment of 0.005 that does not exceed the value.
You can do this as follows:
var percent = Math.Floor(200.0 * x) / 200.0;
Or, if you want it to be more obvious what's happening, this is essentially the same thing:
var increment = 0.005
var percent = Math.Floor(x / increment) * increment;
you have to write it with the amount of decimals you want. var percent = Math.Round(1.13451, 2);
Updated: I think this is the easiest way to do it.
static void Main(string[] args)
{
var percent = 1.13551;
char[] percent1 = percent.ToString().ToCharArray();
if (percent1[4] <= 5)
{
percent = Math.Round(percent, 3);
}
else
{
percent = Math.Round(percent, 2);
}
Console.WriteLine(percent);
Console.Read();
}
Use:
var percent = Math.Round(1.13451, 2);
Inside an if statement
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
static int Sss()
{
int k = int.Parse(Console.ReadLine());
int[] numbers = new int[k];
numbers = GenerateRandomNumbers(numbers);
for (int i = 0; i < numbers.Length; i++)
{
if (numbers[i] < 10 || numbers[i] > 99)
{
Console.WriteLine(numbers[i]);
I need to do a method, Ssk(k), which generates k random numbers and returns the product of these numbers which are double digits and ends with 5.
A non-negative integer x ends in 5 when written as a decimal if and only if x % 10 == 5 is true.
I note that you are returning an int, but ints can only go up to about two billion. The product of five two-digit numbers is almost certainly over that. You should use long, decimal, double or BigInteger instead, depending on your use case.
For any number you can check what the last digit is using by modulo operation, which in C# is a %, but works ONLY for integers. In your case you should check if numbers[i]%10==5.
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 3 years ago.
Improve this question
Currently I need to calculate 2^N, however N can be as large as 1929238932899 and I'm stuck using a long data type which can't hold a number that large.
I've currently tried converting to 'BigInt' however I'm still stuck with the long data type restriction from what I've seen as well.
I have a function which calculates the power. However, with the long data type, it just returns 0 when the number gets too big. Note that this is just a generic recursive power function.
For example, with 2^6 its meant to return 64 and with 2^47 to return 140737488355328. However, when it becomes 2^8489289, it just returns 0.
To represent 2^N in binary form, you need N+1 bits (binary digits), that is
(1 929 439 432 949 324 + 1) / 8 = 241 179 929 118 665.6 bytes ~ 219 PiB for a single number, if you really want to work with it.
Or you can just write down 2^N in binary form: 1 followed by N zeroes.
Since 2^N is an integer, you can represented it using Integer factorization.
You can put that in a class like this:
class FactorizedInteger {
private Dictionary<long, long> _factors = new Dictionary<long, long>();
public FactorizedInteger(long radix, long exponent) {
_factors[radix] = exponent;
}
public void Add(FactorizedInteger other) {
foreach(var factor in other._factors) {
if (_factors.ContainsKey(factor.Key)) {
_factors[factor.Key] += factor.Value;
} else {
_factors[factor.Key] = factor.Value;
}
}
}
public override string ToString() {
return "(" + String.Join(" + ", _factors.Select(p => $"{p.Key}^{p.Value}")) + ")";
}
}
As you can see, you can even add some mathematical operations without exhausting the memory of the computer. I've included Add as an example.
To use it:
var e1 = new FactorizedInteger(2, 1929238932899);
var e2 = new FactorizedInteger(2, 64);
Console.WriteLine(e1);
e1.Add(e2);
Console.WriteLine(e1);
Output:
(2^1929238932899)
(2^1929238932963)
This example needs to be made much smarter to be really usefull, but it is a possible representation of such large numbers.
This question already has answers here:
How do I check if a number is positive or negative in C#?
(17 answers)
Closed 4 years ago.
I am trying to ask the user a question and the input can only be a positive number. If the user tries to enter a word or a negative number ask him the question again. I tried putting an if loop but it didn't work.. The problem is that it accepts negative numbers.
int age;
Console.WriteLine("How old are you?");
do
{
Console.WriteLine("The value must be of integer type");
} while (!int.TryParse(Console.ReadLine(), out age));
You are simply missing a condition in your while:
... } while (!int.TryParse(Console.ReadLine(), out age) || age < 0);
For your while loop, simply add an extra condition and use the or operator.
while (!int.TryParse(Console.ReadLine(), out age) || age < 0); //while not an integer or while the number is less than zero
{
Console.WriteLine("The value must a positive integer");
}
Also, I would not use a do while loop, as the do part of your loop will always be executed before the while condition is checked. The way you have it written now, it will tell the user they have to use an integer type even if they do enter a valid integer.
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 5 years ago.
Improve this question
I would like to make my 2 ints get together in one number but where the biggest of the 2 get in the front and the other one in the back
I have a var named num1 and one named num2.
The numbers of those 2 are getting assigned thru a random.
I would like to make them into one number but with the highest number first and the other one after it. I don't want to + them together but make a big number.
For example num1 = 5 and num2 = 6 my whole number should then be 65.
Thx in advance I tried googling this but I could not really find what I was. Looking for sry for bad English
Utilise Math.Max and Math.Min to find the largest and smallest of the two numbers, then concatenate and parse to an int.
int result = int.Parse(Math.Max(num1, num2) + ""+ Math.Min(num1, num2));
or if the number can get large after concatenation then use the long data type.
long result = long.Parse(Math.Max(num1, num2) + ""+ Math.Min(num1, num2));
It sounds like you wish to randomly generate the digits separately, then combine the digits to form a two-digit number. So:
var num1 = 5;
var num2 = 6;
var bigNumber = num1 + 10 * num2; //65
I found a solution i used a little of both of your answers
This is what it ended like
num1 = randomNum.Next(1, 7);
num2 = randomNum.Next(1, 7);
maxNum = Math.Max(num1, num2);
minNum = Math.Min(num1, num2);
wholeNum = minNum + 10 * maxNum;`