#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
void main (void)
{
clrscr();
int i;
int a[10];
a[0]=0;
a[1]=1;
printf("First 10 Fibonacci numbers are :\n");
printf("%d\n%d\n",a[0],a[1]);
for(i=2;i<10;i++)
{
a[i]=a[i-1]+a[i-2];
printf("%d\n",a[i]);
}
getch();
}
The same way it works in any other language; the nth term is the sum of terms n-1 and n-2, where the first two terms are 0 and 1.
To represent the sequence, use a fixed-size array to store the count of terms (t) that you require, then set the first two terms to 0 and 1. Then traverse the array from element [2] to [t-1], assigning the sum of the previous two elements to the current element.
The following example instantiates a static array of 100 terms that is initialised on first use:
#include<stdio.h>
const int max_fib_terms = 100;
const int* const fib() {
static int fib[max_fib_terms];
static bool init = false;
if (!init) {
init = !init;
fib[0]=0;
fib[1]=1;
int n;
for (n=2; n<max_fib_terms; ++n)
fib[n]=fib[n-1]+fib[n-2];
}
return &fib[0];
}
int main() {
for (int i=0; i<max_fib_terms; ++i)
printf("%u, ", fib()[i]);
printf("\b\b \n");
return 0;
}
program to Display Fibonacci series (0,1,1,2,3,5,8,13------)
112358
#include #include void main(void) { int i,j,k,n; clrscr(); i=0; j=1; printf("%d %d ",i,j); for(n=0;n<=5;n++) { k=i+j; i=j; j=k; printf("%d ",k); } getch(); }
Just generate the Fibonacci numbers one by one, and print each number's last digit ie number%10.
//WAP to print fibonacci series using do-while loop.? using System; class Fibonacci { public static void Main() { int a=1,b=1; int sum=0; Console.Write("Enter Limit:"); int n=Int32.Parse(Console.ReadLine()); Console.Write(a); Console.Write(b); do { sum=a+b; a=b; b=sum; Console.Write(sum); } while(sum<n); } } By-Vivek Kumar Keshari
class Fibonacci { public static void main(String args[]) { System.out.println("enter the number upto u want"); int number=Integer.parseInt(args[0]); int a=0,b=1,c=0; System.out.print(a+" "+b); for(int i=1;i<=number;i++) { c=a+b; System.out.print(c+" "); a=b; b=c; } } } the number are given to the run time where are print the series eg: 5 0 1 1 2 3 5 8 13................
If you have the series stored in an array, you loop through the array and print each array element in turn. Another possibility is to print out the numbers in the series as you generate them. In that case, you may not need to store anything (depending on the series, of course).
Exactly what do you mean by 'C program in Java'
#include #include void main(void) { int i,j,k,n; clrscr(); i=0; j=1; printf("%d %d ",i,j); for(n=0;n<=5;n++) { k=i+j; i=j; j=k; printf("%d ",k); } getch(); }
Just generate the Fibonacci numbers one by one, and print each number's last digit ie number%10.
//to generate Fibonacci series upto a range of 200....(in C).... #include<stdio.h> main() { int a,b,c,i; a=0; b=1; printf("\n FIBONACCI SERIES .....\t"); i=1; while(i<=(200-2)) { c=a+b; printf("\t%d",c); a=b; b=c; i++; } }
//WAP to print fibonacci series using do-while loop.? using System; class Fibonacci { public static void Main() { int a=1,b=1; int sum=0; Console.Write("Enter Limit:"); int n=Int32.Parse(Console.ReadLine()); Console.Write(a); Console.Write(b); do { sum=a+b; a=b; b=sum; Console.Write(sum); } while(sum<n); } } By-Vivek Kumar Keshari
write a c program to accept a number and generate a square root cube and exponential values
class Fibonacci { public static void main(String args[]) { System.out.println("enter the number upto u want"); int number=Integer.parseInt(args[0]); int a=0,b=1,c=0; System.out.print(a+" "+b); for(int i=1;i<=number;i++) { c=a+b; System.out.print(c+" "); a=b; b=c; } } } the number are given to the run time where are print the series eg: 5 0 1 1 2 3 5 8 13................
If you have the series stored in an array, you loop through the array and print each array element in turn. Another possibility is to print out the numbers in the series as you generate them. In that case, you may not need to store anything (depending on the series, of course).
#include<stdio.h> #include<conio.h> int fib(int a); main() { int a; clrscr(); scanf("%d",&a); for(int i=0;i<a;i++) printf("%d\n",fib(i)); } int fib(int a) { if(a==0) return 0; if(a==1) return 1; else return (fib(a-1)+fib(a-2)); }
#include<iostream> unsigned fib (unsigned term, unsigned a=0, unsigned b=1) { if (term<1) return a; return fib (--term, a+b, a); } int main() { std::cout << "Fibonacci (1000th term): " << fib (1000) << std::endl; }
write a program to print the series 1/12+1/22+.........+1/n2 ?
#include#includevoid fibonacci(int x){int f=0,m=-1,n=1,i=0;while(i