while loop, windows form exercise c# -
updated thank replies. appreciated. apologize noob status. first assignment in c#, , first c# class. without previous programming experience @ all. lot of these concepts being first introduced. have done complete first section of assignment.
public partial class form1 : form { public bool oddnum(double value) { return value % 2 != 0; } public bool isprime(int n) { if (n > 1) { return enumerable.range(1, n).where(x => n % x == 0).sequenceequal(new[] { 1, n }); } return false; } public bool evennum(double value) { return value % 2 == 0; } public form1() { initializecomponent(); } private void btngo_click(object sender, eventargs e) { double start = double.parse(txtstart.text); double end = double.parse(txtend.text); double oddcount = 0; double evencount = 0; double evensum = 0; double oddsum = 0; double totalsum = 0; double avrgnum = 0; double avrgsum = 0; double productnum = 1; while (start <= end) { if (oddnum(start)) { oddsum += start; oddcount++; } else { evensum += start; evencount++; } totalsum += start; productnum *= start; avrgnum = totalsum / end; avrgsum = avrgnum / totalsum; start++; } string info = ("number of odd numbers: " + oddcount.tostring() + "\n" + "number of numbers: " + evencount.tostring() + "\n" + "sum of odd numbers: " + oddsum.tostring() + "\n" + "sum of numbers: " + evensum.tostring() + "\n" + "sum of numbers: " + totalsum.tostring() + "\n" + "product of numbers: " + productnum.tostring() + "\n" + "average: " + avrgnum.tostring("f2") + "\n" + "average / sum of numbers: " + avrgsum.tostring("f2")); lbloutput.text = info; } private void btnprime_click(object sender, eventargs e) { int start = convert.toint32(txtstart.text); if (isprime(start)) { messagebox.show("the start number prime."); } else { messagebox.show("the start number not prime."); } } }
}
____________
"create windows forms application performs while loops, if-else statements , double type calculations."
while looping through while loop, should checking if number odd or even, calling 1 of methods (don't need both). this:
while (startingnumber != endingnumber) { if (oddnum(startingnumber)) oddnumberslabel.text += " " + startingnumber; else evennumberslabel.text += " " + startingnumber; startingnumber += incrementvalue; }
Comments
Post a Comment