46. Write a Program for Fibonacci Series.
Answer :
#include<iostream.h>
#include<conio.h>
Void main()
{
int f=0, s=1, t, i, n;
cout<<"Enter n;
cin>>n;
cout<<f<<" "<<s;
for(i=3; i<=n; i++)
{
t=f+s;
cout<<" "<<t;
f=s;
s=t;
}
getch();
}
...................................................................................................................................................................
47. Write a Program for check that the Number is Prime or not.
Answer :
#include<iostream.h>
#include<conio.h>
Void main()
{
int i, n, prime=1;
cout<<"Enter n";
cin>>n;
for(i=2, i<n; i++)
{
if(n%1 == 0)
{
prime = 0;
break;
}
}
if(prime==1)
{
cout<<#include<iostream.h>
#include<conio.h>
Void main()
{
cout<<"Prime No";
}
else
{
cout<<"Not a Prime No";
}
}
...................................................................................................................................................................
48. Write a Program for check the Factor Number.
Answer :
#include<iostream.h>
#include<conio.h>
Void main()
{
int i,n;
cout<<"\n Enter n";
cin>>n;
for(i=n; i<=n; i++)
{
if(n%1 == 0)
{
cout<<i<<" ";
}
}
getch();
}
...................................................................................................................................................................
49. Example for Function Template.
Answer :
#include<iostream.h>
#include<conio.h>
template <class T>
Void sum(Ta, Tb)
{
T c;
c=a+b;
return T;
}
Void main()
{
int a=10, b=20;
cout<<"The sum of "<<a<<"and"<<b<<"is"<<sum(a,b);
double x=10.5, y=20.6;
cout<<"The Sum of<<x<<"and"<<y<<"is"<<sum(x,y);
getch();
}
Answer :
#include<iostream.h>
#include<conio.h>
Void main()
{
int f=0, s=1, t, i, n;
cout<<"Enter n;
cin>>n;
cout<<f<<" "<<s;
for(i=3; i<=n; i++)
{
t=f+s;
cout<<" "<<t;
f=s;
s=t;
}
getch();
}
...................................................................................................................................................................
47. Write a Program for check that the Number is Prime or not.
Answer :
#include<iostream.h>
#include<conio.h>
Void main()
{
int i, n, prime=1;
cout<<"Enter n";
cin>>n;
for(i=2, i<n; i++)
{
if(n%1 == 0)
{
prime = 0;
break;
}
}
if(prime==1)
{
cout<<#include<iostream.h>
#include<conio.h>
Void main()
{
cout<<"Prime No";
}
else
{
cout<<"Not a Prime No";
}
}
...................................................................................................................................................................
48. Write a Program for check the Factor Number.
Answer :
#include<iostream.h>
#include<conio.h>
Void main()
{
int i,n;
cout<<"\n Enter n";
cin>>n;
for(i=n; i<=n; i++)
{
if(n%1 == 0)
{
cout<<i<<" ";
}
}
getch();
}
...................................................................................................................................................................
49. Example for Function Template.
Answer :
#include<iostream.h>
#include<conio.h>
template <class T>
Void sum(Ta, Tb)
{
T c;
c=a+b;
return T;
}
Void main()
{
int a=10, b=20;
cout<<"The sum of "<<a<<"and"<<b<<"is"<<sum(a,b);
double x=10.5, y=20.6;
cout<<"The Sum of<<x<<"and"<<y<<"is"<<sum(x,y);
getch();
}
...................................................................................................................................................................
50. Example of Class Template.
Answer :
#include<iostream.h>
#include<conio.h>
template <class T>
class vector
{
T*V;
int size;
Public:
vector (int m);
vector (T*a);
T operator *(vector &y);
};
template <class T>
vector<T> :: vector (int m)
{
v = new T[size = m];
for(int i=0; i<size; i++)
v[i]=0;
}
template <class T>
vector <T> :: vector (T*a)
{
for(int i=0; i<size; i++)
v[i] = a[i];
}
template <class T>
Tvector <T> :: operator*(vector &y)
{
T sum=0;
for(int i=0;i<size;i++)
sum+=this -> v[i] * y.v[i];
return sum;
}
Void main()
{
vector <int> T,(5);
T1 = {10,20,30,40,50};
vector <int> T2()5;
T2 = {80,90,60,70,15};
vector <int> T3 = T1 * T2;
for(int i=0; i<5; i++)
cout<<T3[i];
getch();
}
*********************************************************************************
0 comments:
Post a Comment