C# quiz calculate percentage [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 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

Related

How to set a while loop? [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
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++;
}

C# loop on fraction [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 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

Making 2 ints merge but with the highest number in first spot and the lowest in the back [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 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;`

Get Number from Random 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 5 years ago.
Improve this question
How to get the whole number of this random.
Finally, I get a Debug.Log () to display my 757 number
You need to build up the whole number:
Random random = new Random();
int hundred = 100 * random.Next(1, 10);
int ten = 10 * random.Next(0, 10);
int one = random.Next(0, 10);
int result = hundred + ten + one;
Lets assume, the first random gives you 7, the second gives you 5 and the third gives you 7 again. you would end up with:
hundred = 100 * 7 = 700
ten = 10 * 5 = 50
one = 5
result = 700 + 50 + 7 = 757
Then just log result
What you describe is random 3 digit number. So simply:
Random.Range(100, 1000);

Retrieving a value that is stored as MB [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 9 years ago.
Improve this question
I have a table that stores the amount of RAM a server has in a biginit column with values such as 2470208.
But how I can apply a data annotation or other validations to show only 2 instead of s470208. ?
I mean to always divide by 1 million and get the number on the left side of the digit ?
1) Use this for automatic thousands-unit:
string GetByteString(long n) {
int k=0;
string u=" kMGTEP";
while(n>1024) {
n>>=10;
k++;
}
return n.ToString() + u[k];
}
Call:
string s= GetByteString(1234567890123);
Debug.WriteLine(s);
2) But if you simply always want MB just shift by 20:
long n = 123456789;
string MB = (n>>20).ToString();
But this will show 0 if n goes below 1MB.
Reason:
1 kB = 2^10 = 1<<10 = 1024;
1 MB = 2^20 = 1<<20 = 1024*1024 = 1048576;
1 GB = 2^30 = 1<<30 = 1024*1024*1024 = 1073741824;
You tagged C# but mentioned a bigint column so it isn't clear whether you're looking for a database or C# solution. The following C# method will take the number of bytes as an integer and return a formatted string...
public string FormattedBytes(long bytes)
{
string units = " kMGT";
double logBase = Math.Log((double)bytes, 1024.0);
double floorBase = Math.Floor(logBase);
return String.Format("{0:N2}{1}b",
Math.Pow(1024.0, logBase - floorBase),
units.Substring((int)floorBase, 1));
}

Categories

Resources