using System;
class Fibonacci
{
static void Main()
{
Console.WriteLine("Enter First No");
int num1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Second No");
int num2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter no of iteration you need");
int range = Convert.ToInt32(Console.ReadLine());
int result = 0;
Console.WriteLine(num1);
Console.WriteLine(num2);
for (int i = 0; i <= range; i++)
{
result = num1 + num2;
Console.WriteLine(result);
num1 = num2;
num2 = result;
}
Console.ReadLine();
}
}
Dry run:
DRY RUN | |||
NOI=10 | num1=1 | num2=2 | |
No Of Iteration | num1 | num2 | Result |
1 | 1 | 2 | 3 |
2 | 2 | 3 | 5 |
3 | 3 | 5 | 8 |
4 | 5 | 8 | 13 |
5 | 8 | 13 | 21 |
6 | 13 | 21 | 34 |
7 | 21 | 34 | 55 |
8 | 34 | 55 | 89 |
9 | 55 | 89 | 144 |
10 | 89 | 144 | 233 |
1
Enter Second No
2
Enter no of iteration you need
10
1
2
3
5
8
13
21
34
55
89
144
233
377
Loop: - Loop is mechanism which used to execute your
statement till the condition become false.
Further loop can categories into two. 1)Range Based and 2)Condition Based.
Range Based:- In this case user/developer must aware range
of iteration.
Iteration 0 to 10.in this scenario we will go for loop.
For Loop:- This loop repeatedly executes a group of
statements as long as a condition not become false.
Syntax:-for( int i=0;i<10;i++ or--)
{ Operation
}
For loop consist three importance parts.
1)
Initialization
2)
Condition
3)
Increment/ decrement.
Eg:-
( int i=0;i<10;i++
or--)
1 2 3
.
Please WAP Fibonacci Series using while loop.
ReplyDelete