0% found this document useful (0 votes)
41 views9 pages

Program To Generate Fibonacci Series

The document contains 5 code examples that generate Fibonacci series using iterative and object-oriented approaches, find the greatest common divisor (GCD) of two numbers iteratively and using a class, and find the maximum and minimum of an array of numbers.

Uploaded by

nisha02t
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views9 pages

Program To Generate Fibonacci Series

The document contains 5 code examples that generate Fibonacci series using iterative and object-oriented approaches, find the greatest common divisor (GCD) of two numbers iteratively and using a class, and find the maximum and minimum of an array of numbers.

Uploaded by

nisha02t
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

1. Program to generate Fibonacci series.

#include<iostream.h>
#include<conio.h>
void fib(int n);
void main()
{
int n;
clrscr();
cout<<"Enter the limit:";
cin>>n;
cout<<"The elements in the fibonocci series are\n";
fib(n);
getch();
}
void fib(int n)
{
int a=0,b=1,c,count=0;
while(a<n)
{
cout<<a<<endl;
c=a+b;
a=b;
b=c;
count++;
}
cout<<"Count of the limit "<<n <<" is "<<count;
}
OUTPUT:

Enter the limit : 4

The elements in the Fibonacci series are


0
1
1
2
3

Count of the limit 4 is 5


2. Program to generate Fibonacci series using class.

#include<iostream.h>
#include<conio.h>
class fib
{
int n;
public:
void read()
{
cout<<"Enter the limit:";
cin>>n;
cout<<"The elements in the fibonocci series are\n";
}
void display();
};
void fib::display()
{
int a=0,b=1,c,count=0;
while(a<n)
{
cout<<a<<endl;
c=a+b;
a=b;
b=c;
count++;
}
cout<<"Count is "<<count;
}
int main()
{
int n;
clrscr();
fib f1;
f1.read();
f1.display();
getch();
return(0);
}
OUTPUT:

Enter the limit: 6

The elements in the Fibonacci series are


0
1
1
2
3
5

Count is 6
3. Program to find the GCD of the given two numbers

#include<iostream.h>
#include<conio.h>
int gcd(int,int);
void main()
{
int a,b,x,p,q;
clrscr();
cout<<"Enter the two numbers:";
cin>>a>>b;
p=a;
q=b;
x=gcd(a,b);
cout<<"\nGCD of "<< p <<" and "<< q <<" is "<<x;
getch();
}
int gcd(int a,int b)
{
int r;
while(r!=0)
{
r=a%b;
a=b;
b=r;
}
return(a);
}

OUTPUT:

Enter the two numbers: 4 8

GCD of 4 and 8 is 4
4. Program to find the GCD of the given two numbers using class.

#include<iostream.h>
#include<conio.h>
class gcd
{
private:
int a,b,k,p,q;
public:
gcd(){}
~gcd(){}
void read();
void func();
void display();
};
void gcd::read()
{
cout<<"Enter the two numbers:";
cin>>a>>b;
p=a;
q=b;
}
void gcd::func()
{
int r;
while(r!=0)
{
r=a%b;
a=b;
b=r;
}
k=a;
}
void gcd::display()
{
cout<<"\nGCD of "<< p <<" and "<< q <<" is "<<k;
}
void main()
{
gcd g;
clrscr();
g.read();
g.func();
g.display();
getch();
}

OUTPUT:

Enter the two numbers: 1026 3256

GCD of 1026 and 3256 is 2


5. Program to find the maximum and minimum of N numbers

#include<iostream.h>
#include<conio.h>
void maxmin(int a[],int l);
int main()
{
int i,l,a[10];
clrscr();
cout<<"Enter the limit:";
cin>>l;
cout<<"\nEnter the numbers:";
for(i=0;i<l;i++)
cin>>a[i];
maxmin(a,l);
getch();
return(0);
}
void maxmin(int a[],int l)
{
int max=a[0],min=a[0];
for(int i=1;i<l;i++)
{
if(max<a[i])
max=a[i];
else
{
if(min>a[i])
min=a[i];
}
}
cout<<"\nMaximum number is "<<max<<endl;
cout<<"\nMinimum number is "<<min;
getch();
}
OUTPUT:

Enter the limit: 6

Enter the numbers: 01 20 54 17 03 20

Maximum number is 54

Minimum number is 01

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy