In: Computer Science
C#
Assignment4A: Fibo-what? If you’ve never heard of the Fibonacci series, it’s an infinite series that occurs everywhere in nature. It starts off with two digits – 0 and 1. To get the next value in the series, you add the previous two values. In this case, the third value is 0+1 = 1. The fourth value is 1+1 = 2, the fifth 1+2=3 and so on, to give us a series like:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34,...
I always wondered what it would look like if you changed the starting values from 0 and 1 to some arbitrary (and if it would matter). We’re going to do that here. Ask the user for a pair of starting values as well as a number of times to iterate the series, then produce the correct output. We recommend you use a FOR loop and use a sum variable.
For part 1 of this assignment, design pseudocode to solve this problem. When working on the source code, call the file name Assignment4A(.java, .cs, .cpp) and the class name Assignment4A. When printing the next value in the series, print the comma first. The first two values are special cases for printing.
Sample Output #1: Enter seed 1:
5
Enter seed 2:
8
Number of iterations:
10
5,8,13,21,34,55,89,144,233,377,610,987
Sample Output #2: Enter seed 1:
56
Enter seed 2:
78
Number of iterations:
6
56,78,134,212,346,558,904,1462
SOLUTION-
I have solve the problem in C# code with comments and screenshot
for easy understanding :)
CODE-
using
System;
public class Fibonacci
{
public static void
Main(string[] args)
{
int n1, n2, sum, i, num;
Console.Write("Enter seed 1: ");
n1 =
int.Parse(Console.ReadLine());
// taking input of the first number
Console.Write("Enter seed 2: ");
n2 =
int.Parse(Console.ReadLine());
// taking input of the second number
Console.Write("Number of iterations: ");
num =
int.Parse(Console.ReadLine());
// taking input of the number of items other than first two
Console.Write(n1+","+n2+",");
// printing the first two number
for(i=2; i<num+2;
++i)
// iterating the loop
{
sum =
n1+n2;
// calculating the next item which is the sum of previous two
Console.Write(sum+",");
// printing the next number
n1 =
n2;
// storing the numbers for the next iterations
n2 = sum;
}
}
}
SCREENSHOT-
IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL
SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK
YOU!!!!!!!!----------