C
C#2y ago
MojoNoot

❔ Trying to make a bit of code to display all values of fibonacci until the nth term input

Have got this so far (i am a beginner) int n = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(Fibonacci(n)); public static int Fibonacci(int x) { int first = 1; int second = 2; int next = 3;
for (int i = 3; i <= x; i++){ next = first + second; Console.WriteLine("{0}", next); first = second; second = next; }
}
28 Replies
mtreit
mtreit2y ago
What part are you having trouble with?
MojoNoot
MojoNootOP2y ago
it says not all code paths return a value and im just confused
mtreit
mtreit2y ago
So, your function is defined to return an int:
public static int Fibonacci(int x)
public static int Fibonacci(int x)
Do you ever return an int?
MojoNoot
MojoNootOP2y ago
i want to return the series of fibonacci numbers so if i put 3 in i want it to return 1,1,2
mtreit
mtreit2y ago
If you just want to print and not return a value, change the return type from int to void There is a difference between "returning" and "printing"
MojoNoot
MojoNootOP2y ago
would you mind explaining that for me sorry i am knew to coding
mtreit
mtreit2y ago
If you don't know what a return value from a method is I would suggest going through a basic C# tutorial, such as $helloworld er $helloworld
MojoNoot
MojoNootOP2y ago
okay ill check that out thank you very much i might be back later to ask smth else 😆 🙂
mtreit
mtreit2y ago
Sure, good luck
MojoNoot
MojoNootOP2y ago
once i converted it to void it now gives me the error cannot convert bool to void\ but i havent used a bool anywhere lmao
mtreit
mtreit2y ago
Show the current code $code
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
mtreit
mtreit2y ago
$codegif
MojoNoot
MojoNootOP2y ago

int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(Fibonacci(n));
public static void Fibonacci(int x) {
int first = 1;
int second = 2;
int next = 3;

for (int i = 3; i <= x; i++){
next = first + second;
Console.WriteLine("{0}", next);
first = second;
second = next;
}

}

int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(Fibonacci(n));
public static void Fibonacci(int x) {
int first = 1;
int second = 2;
int next = 3;

for (int i = 3; i <= x; i++){
next = first + second;
Console.WriteLine("{0}", next);
first = second;
second = next;
}

}
mtreit
mtreit2y ago
Console.WriteLine(Fibonacci(n));
Console.WriteLine(Fibonacci(n));
Remove the Console.WriteLine there You aren't returning anything so there is nothing to pass to Console.WriteLine
MojoNoot
MojoNootOP2y ago
should i just put fibonacci(n)
mtreit
mtreit2y ago
Yeah
MojoNoot
MojoNootOP2y ago
oki dokiee
mtreit
mtreit2y ago
Since you are printing the values inside your Fibonacci method
MojoNoot
MojoNootOP2y ago
it also wont return first and second didgets but i cant figure out where to put that in my code like i know what to write idk where to put it
mtreit
mtreit2y ago
You could just special case those:
Console.WriteLine(1);
Console.WriteLine(1);
Console.WriteLine(2);

for (int i = 3; i <= x; i++)
Console.WriteLine(1);
Console.WriteLine(1);
Console.WriteLine(2);

for (int i = 3; i <= x; i++)
Or, rewrite your code to not start the loop at 3 and have it also print the initial values.
MojoNoot
MojoNootOP2y ago
oh okay ill give it ago thank you very much 🙂 u have saved me can i ask one question i got this part from a friend but i dont fully understand how it works
Console.WriteLine("{0}", next);
Console.WriteLine("{0}", next);
mtreit
mtreit2y ago
That's called a format string. The {0} part is placeholder where the thing after the comma will be inserted. If you had two things to insert it might look like this:
Console.WriteLine("next is {0} and other is {1}", next, other);
Console.WriteLine("next is {0} and other is {1}", next, other);
However, you can do the same thing with something called string interpolation, which is usually the preferred way these days:
Console.WriteLine($"{next}");
Console.WriteLine($"{next}");
MojoNoot
MojoNootOP2y ago
i think ive seen that before but writen in a different way yeah thats it thank you very mcuh u have been super helpful
mtreit
mtreit2y ago
Happy to help
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server