c# - How to put a MessageBox to display my Fibonacci sequence vertically -
here's finished code, need little advice , how insert desired messagebox. needs display finished sequence vertically, thanks.
using system; using system.collections.generic; using system.linq; using system.text; using system.windows.forms; class program { public static double fibonacci(double n) { double = 0; double b = 1; (double = 0; < n; i++) { double sum = a; = b; b = sum + b; } return a; } static void main() { (double = 0; < 12; i++) { console.writeline(fibonacci(i)); } } }
if want show messagebox, try:
static void main() { list<double> nums = new list<double>(); (double = 0; < 12; i++) { nums.add(fibonacci(i)); } messagebox.show(string.join(system.environment.newline, nums)); }
a console application can display messagebox if you've included right libraries, , looks include system.windows.forms
, should set.
although honestly, method isn't efficient, everytime call it, has recalculate numbers term want. use yield return
, main
can 1 liner:
class program { public static ienumerable<long> fibonacci(int n) { long = 0; long b = 1; (int = 0; < n; i++) { yield return a; long sum = a; = b; b = sum + b; } } static void main() { messagebox.show(string.join(system.environment.newline, fibonacci(12))); } }
Comments
Post a Comment