Do While Fibonacci C++:: #Include #Include
Do While Fibonacci C++:: #Include #Include
#include <iostream>
int main()
{
//define first two fibonacci series numbers.
int fib1 = 0;
int fib2 = 1;
//declare the variable to store the next number of fibonacci series
int fib3;
//declare the variable to store how many numbers to be printed. Default is 2.
int numbers = 2;
//the counter to keep track how many numbers are printed.
int counter = 2;
//Ask user how many numbers of the fibonacci series need to be printed.
std::cout << "How many Fibonacci number you need ? : " ;
//Store the number.
std::cin >> numbers;
//If number entered is less than 3, exit the program.
if (numbers < 3) return 0;
//Print the first two element.
std::cout << fib1 << "\t" << fib2;
//do-while loop to calculate the new element of the series and printing the same.
do {
counter++;
fib3 = fib1 + fib2;
std::cout << "\t" << fib3;
fib1 = fib2;
fib2 = fib3;
} while (counter <= numbers);
std::cout << std::endl;
system("pause");
return 0;
}
USING RECURSION c++
#include <iostream.h>
#include <conio.h>
unsigned int fibonacci (unsigned int n)
{
if (n <= 2)
{
return 1;
}
else
{
return fibonacci(n-1) + fibonacci(n-2);
}
}
int main()
{
unsigned int i, j=0;
cout<<"\n Enter the fibonnaci number : ";
cin>>i;
for(j=1; j<=i; j++)
cout<<fibonacci(j)<<" ";
getch();
}
Using c Fibonacci
#include<stdio.h>
int main()
{
unsigned int i=0,j=0,sum=1,num;
printf("nEnter the limit for the series ");
scanf("%d",&num);
while(sum<num)
{
printf("%d ",sum);
i=j;
j=sum;
sum=i+j;
}
getch();
}
odd even
#include< stdio.h>
#include< conio.h>
void main()
{
int a;
clrscr();
printf("Enter A number : ");
scanf("%d",&a);
if(a%2 != 0)
{
printf("\nThe Number %d Is Odd",a);
}
else if(a%2 == 0)
{
printf("\nThe Number %d Is Even",a);
}
getch();
}
#include <iostream>
using namespace std;