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
I'm trying to set my code for counting math games, all math operations are fine except for division. I don't know how to set the while loop correctly. There is a problem with division, such that I would like the division to be residual, so I came up with one method which is given below. It is all in WPF Application. I would like to count only single-digit numbers.
Random number = new Random();
int maxValue = 10;
int total = 0;
int firstNumber = number.Next(1, maxValue);
int secondNumber = number.Next(1, firstNumber);
while (firstNumber % secondNumber != 0);
{
secondNumber++;
}
total = firstNumber / secondNumber;
Why does it still show me the values that have a residual division?
Thank you for any advice
The semi colon at the end of line:
while (firstNumber % secondNumber != 0);
...ends the while loop. The code in the remaining block is executed without any condition (as it in fact is a anonymous block):
{
secondNumber++;
}
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 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 am trying to utilize the Math.Cos() function in c# to print a range of of values from 0 to 2pi increasing by .1pi, so 20 iterations. The problem is I cannot get my x value to change as the for loop is executing.
public void Cos()
{
double x = 0;
double a = Math.PI * x;
double b = Math.Cos(a);
for (int i = 0; i < 21; i++)
{
Console.WriteLine("Cos({0})pi = {1}", x, b);
x += .1;
}
}
When I print the results to the console, it only remembers the value of cos at x = 0. So I just get 1, 20 times as a result for Cos(.1)pi, Cos(.2)pi, etc...
I am trying to utilize the Math.Cos() function in c# to print a range of of values from 0 to 2PI increasing by .1PI
This sounds like a job for a for loop, where we start with a value of 0, and increment by .1 * PI on each iteration until it reaches 2 * PI.
Since a for loop has an initializer part, a condition part, and an incrementing part, it is the perfect construct. No need for an extra variable that increments from 0 to 20 - we can use the for loop to do our incrementing of x and our testing the exit condition!
public static void Cos()
{
for (double x = 0; x <= Math.PI * 2; x += Math.PI * .1)
{
Console.WriteLine("Cos({0})PI = {1}", x, Math.Cos(x));
}
}
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 4 years ago.
Improve this question
I need someone to give me an idea on how to go on about this problem.Using a loop to calculate the fraction , There is no common value.I want to get the sum
Eg for fraction :
1 1/5 1/10 1/15 1/20 … 1/290 1/295 1/300
code snippet:-
int sum=0;
for(int i=1;i<=60 ;i++)
{
int sum=1
}
These sort of problems are actually surprisingly non-trivial due to issues with working with floating point, and decimal types for that matter.
Accepting that you want a loop solution for this (a closed form solution for n terms does exist), first note that your series can be written as
1 + 1/5(1 + 1/2 + 1/3 + ... + 1/60)
Then note that a good rule of thumb when working with floating point types is to add the small terms first.
So an algorithm would be of the form
double sum = 0.0;
for (int i = 60; i >= 1; --i){
sum += 1.0 / i;
}
sum = sum / 5 + 1;
Note the 1.0 in the numerator; that's there to defeat integer division.
Reference: Is floating point math broken?
͏Since you asked for a hint:
float sum = 1.0;
for (int i = 5; i <= ??; i += ??) {
sum += 1.0/i;
}
What goes in place of the ??s?
try this code:
double sum=1;
for(int i=5; i<=300; i+=5)
sum += (double) 1 / i;
The value of sum will be 1.93597408259035
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 6 years ago.
Improve this question
I have a quiz that have many questions and 5 possibilities of answers.
Lets take one question,it has follow answers:
148 - Good
5 - N/A
268 - Great
5 - Regular
11 - Bad
These are numbers collected directly from database.Now i need to show it as percentage.i.E:
Great - 45%
Good - 40
[..]
and so on
Any ideas?
int na = 5;
int good = 148;
int great = 268;
int regular = 5;
int bad = 11;
int sum = na + good + great + regular + bad;
naPercent = getPercent(na,sum);
float getPercent(int value, int sum)
{
return (value*100.0)/sum;
}
This is not a programming question, it is a math question. The percentage of each item is equal to the number of that item divided by the total number. In your example, the total number is 148+5+268+5+11 = 437. Great = 268 / 437 = 61.327%
total count for the answer / total count for all answers to this question combined * 100