斐波那契数列,简单地说,起始两项为0和1,此后的项分别为它的前两项之后,下面给出10范围内的斐波那契数列代码(无递归)。
JAVA
public class A{
public staitc void main(String[] args){
int a=0,b=1,n=0;
while(b<10)
{
System.out.println(b);
n=b;
b=a+b;
a=n;
}
}
}
Python
a,b=0,1
while b<10:
print(b)
a,b=b,a+b
C++
#include<stdio.h>
#include<iostream.h>
using namespace std;
void main()
{
int a=0;
int b=1;
int n=0;
while(b<10)
{
cout<<b<<endl;
n=b;
b=a+b;
a=n;
}
}
C
#include<stdio.h>
int main()
{
int a=0;
int b=1;
int n=0;
while(b<10)
{
print(b);
n=b;
b=a+b;
a=n;
}
return 0;
}
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1 //1 1 2 3 5
{
class Program
{
static void Main(string[] args)
{
int a = 1, b = 1, c=0;
Console.WriteLine("Please input the number:");
string s = Console.ReadLine();
int number = int.Parse(s);
for (int i = 2; i < number; i++)
{
c = a + b;
a=b;
b = c;
}
Console.WriteLine("{0}",c);
Console.ReadLine();
}
}
}