#include<iostream>
using namespace std;
#define MIN 1
#define MAX 1000000
int main()
{
int num;
char c;
do
{
cout << "Enter a number from " << MIN << "-" << MAX << " (0 to quit): ";
cin >> num;
while(cin.fail() num < 0 num > MAX)
{
if( cin.fail() )
{
cin.clear();
cin >> c;
}
cout << "Invalid number, try again: ";
cin >> num;
}
if(num)
{
cout << "\nFibbonacci series for n, where 1 <= n <= " << num << ":\n\n";
int n = 1, x = 0, y = 0;
do
{
cout << n;
x = y;
y = n;
n = x + y;
if( n < num )
cout << ", ";
}while( n < num );
cout << endl << endl;
}
}while( num );
return( 0 );
}
Before jumping to program we will look how Fibonacci series are generated.
Fibonacci series is linear sequence of integers, there first one is 0 and second one is 1. Now all other elements are generated using this rule: Fn+1 = Fn + Fn-1.
0 1 1 2 3 5 8 13 21 34 55 89 ...
...
34 = 21 + 13
55 = 34 + 21
89 = 55 + 34
...
Here is small program that does the same and it uses do-while loop:
#include
int main() {
int num = 0;
int a = 0, b = 1;
int count = 0;
printf("How many numbers you want? (3 mininmum) ");
scanf("%d", &num);
printf("0 1 ");
do {
b = a + b;
a = b - a;
b = a + b - a;
printf("%d ", b);
count++;
} while (count <= num - 3);
printf("\n");
return 0;
}
Testing:
How many numbers you want? (3 mininmum) 20
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
Yes....Here is the code....
#include <stdio.h>
public int main() {
int num1 = 0;
int num2 = 0;
int num3;
int maxlimit;
printf("Enter the maximum limit of Fibonacci series : ");
scanf(%d",&maxlimit);
printf("%d\t", num1,num2);
num3 = num1 + num2;
if (maxlimit > 0) {
while ( num3 < maxlimit) {
printf("%d",num3);
num3 = num1 + num2;
num1 = num2;
num2 = num3;
}
}
else
printf("Invalid limit !");
}
public class fib{
public static void main(String[] args){
int count=0;
int i=0;
int j=1;
do{
System.out.println(j+" ");
int k= i+j;
i=j;
j=k;
}
while (++count<15);
}
write a program to print the series 1/12+1/22+.........+1/n2 ?
Just generate the Fibonacci numbers one by one, and print each number's last digit ie number%10.
The Fibonacci sequence uses recursion to derive answers. It is defined as: F0 = 0 F1 = 1 Fn = F(n - 1) + F(n -2) To have this sequence printed by a php script use the following: function fibonacci($n) { if($n 1) return 1; //F1 else return fibonacci($n - 1) + fibonacci($n - 2); //Fn } This recursive function will print out the Fibonacci number for the integer n. To make it print out all the numbers in a particular set add this to your script. for($i = 0; $i < 15; $i++) { echo fibonacci($i) . "<br />"; } So your final result would look like. <?php function fibonacci($n) { if($n 1) return 1; else return fibonacci($n - 1) + fibonacci($n - 2); } for($i = 0; $i < 15; $i++) { echo fibonacci($i) . "<br />"; } ?>
#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; }
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).
write a program to print the series 1/12+1/22+.........+1/n2 ?
Just generate the Fibonacci numbers one by one, and print each number's last digit ie number%10.
The Fibonacci sequence uses recursion to derive answers. It is defined as: F0 = 0 F1 = 1 Fn = F(n - 1) + F(n -2) To have this sequence printed by a php script use the following: function fibonacci($n) { if($n 1) return 1; //F1 else return fibonacci($n - 1) + fibonacci($n - 2); //Fn } This recursive function will print out the Fibonacci number for the integer n. To make it print out all the numbers in a particular set add this to your script. for($i = 0; $i < 15; $i++) { echo fibonacci($i) . "<br />"; } So your final result would look like. <?php function fibonacci($n) { if($n 1) return 1; else return fibonacci($n - 1) + fibonacci($n - 2); } for($i = 0; $i < 15; $i++) { echo fibonacci($i) . "<br />"; } ?>
#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; }
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).
write a program to print A to Z on screen in c?
CLS a = 0 b = 0 FOR i = 1 TO 10 IF a = 0 THEN f = 1 END IF PRINT f b = a a = f f = a + b NEXT i END from : Parth jani parthjani7@yahoo.com
echo 'print a pattern'
Q.1 Write a program to print first ten odd natural numbers. Q.2 Write a program to input a number. Print their table. Q.3 Write a function to print a factorial value.
to print the Fibonacci series until 100 you can take the input in d to make it run for whatever value you want void main(){ int a,b,c,d; a=0; b=1; c=1; d=100; while(c<d) { printf("%d\n",c); c=a+b; a=b; b=c; } }
10 print "That is not a question." 20 print "That is a command." 30 end
//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