cmmdc recursiv

Sa se afle cmmdc pentru 2 numere utilizand varianta recursiva

#include<iostream.h>
int a,b;

int cmmdc(int a,int b)
{
if(a==b) return a;
else
if (a>b) return cmmdc(a-b,b);
else return cmmdc(a,b-a);
}

void main()
{
cout<<“a=”;cin>>a;
cout<<“b=”;cin>>b;
cout<<“cmmdc: “<<cmmdc(a,b);
}

{module orizontal600}

cautare in vector

#include<iostream.h>
#include<conio.h>
typedef int vector[20];
int n;vector v;

int e(int n,int x)
{
if (n==0) return 0;
else
if (v[n]==x) return 1;
else return e(n-1,x);
}

void main()
{
int x,i;
cout<<“n=”;cin>>n;
cout<<“x=”;cin>>x;
for(i=1;i<=n;i++) {cout<<“v[“<<i<<“]=”;cin>>v[i];}
if (e(n,x)) cout<<“este”<<endl;
else cout<<“nu este”;
getch();
}

suma elementelor pare

//suma elementelor pare
#include<iostream.h>
#include<conio.h>
typedef int vector[20];
int n;vector v;

int suma(int n)
{ if (n==0) return 0;
else return suma(n-1) + (v[n]%2==0)*v[n];
}

void main()
{
cout<<“n=”;cin>>n;
for(int i=1;i<=n;i++)
{cout<<“v[“<<i<<“]=”;cin>>v[i];}
cout<<suma(n)<<endl;
getch();
}

{module orizontal600}

maximul a n componente

Maximul unui vector

#include<iostream.h>
#include<conio.h>
typedef int vector[20];
int n;vector v;

int maxim(int n)
{int max;
if (n==1) return v[1];
else
{
max=maxim(n-1);
if (max<v[n]) return v[n];
else return max;
}
}

void main()
{
cout<<“n=”;cin>>n;
for(int i=1;i<=n;i++)
{cout<<“v[“<<i<<“]=”;cin>>v[i];}
cout<<maxim(n)<<endl;
getch();
}

{module orizontal600}

suma n/((n+1)(n+2)

//1/(2*3)+2/(3*4)+…(n/((n+1)(n+2))
#include<iostream.h>
#include<conio.h>

float e(int k)
{
return (float)k/((k+1)*(k+2));
}

float s(int k)
{
if
(k==1) return (float)1/(2*3);
else return e(k)+s(k-1);
}

void main()
{
int n;
cout<<“n=”;cin>>n;
cout<<s(n)<<endl;
getch();
}

{module orizontal600}