0% found this document useful (0 votes)
175 views3 pages

Recursive Function Programming

The document discusses a C++ program that uses recursion and random values. It defines a class called SuObjet that contains methods for reading random numbers N, A, and B. It includes a facto method that calculates factorials recursively and a SumObj method that calculates a sum using the random values and factorial results. The main method initializes a SuObjet object and loops to call the methods, displaying outputs and prompting for another iteration.

Uploaded by

Joel Ed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
175 views3 pages

Recursive Function Programming

The document discusses a C++ program that uses recursion and random values. It defines a class called SuObjet that contains methods for reading random numbers N, A, and B. It includes a facto method that calculates factorials recursively and a SumObj method that calculates a sum using the random values and factorial results. The main method initializes a SuObjet object and loops to call the methods, displaying outputs and prompting for another iteration.

Uploaded by

Joel Ed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

/*

//UNA FUNCION CON RECURSIVIDAD Y VALORES ALEATORIOS


#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#define mensaje "\nOtra Prueba[S][N] "
using namespace std;
void LeeN(long double &N){
N=rand()%16+1;
cout<<"N= "<<N;
}
void LeeAB(long &A,long &B){
A=rand()%21;
B=rand()%21;
cout<<"\nA "<<A<<endl
<<"B "<<B;
}
long double facto(long double N){
if(N==0||N==1)
return(1);
else
return(N*facto(N-1));
}
double Suma(long A,long B,long double N){
double S=0;
if(B==0){
cout<<"\nSin Solucion ";
system("pause");
exit(1);
}
else
for(long i=1;i<=N;i++)
S=S+powf(-1,i+1)*((2*i-1)*A-
powf(facto(2*i)*B,1.0/(2*i)))/(2*i*B);
return(S);
}
void main(){
for(;;){system("cls");
srand((unsigned)time (NULL));
long double M;long P,Q;char rpt;
LeeN(M);
LeeAB(P,Q);
cout<<"\nEl Factorial de N= "<<facto(M)<<endl
<<"\La Suma = "<<Suma(P,Q,M);
cout<<mensaje;
cin>>rpt;
if(rpt=='n' ||rpt=='N')break;
}
//system("pause");
}
*/

//Programando con Objetos


#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<math.h>

Ing. Daniel Osorio Maldonado Pgina 1


#define mensaje "\nOtra Prueba[S][N] "
using namespace std;

class SuObjet{
protected:
long double N;
long A,B;
public:
void LeeN(long double M);
void LeeAB(long P,long Q);
long double facto(long double N);
double SumObj(long A,long B,long double N);
};
//Implementacion
void SuObjet::LeeN(long double M)
{N=M;}

void SuObjet::LeeAB(long P,long Q)


{A=P;B=Q;}

long double SuObjet::facto(long double N){


if(N==0||N==1)
return(1);
else
return(N*facto(N-1));
}
double SuObjet::SumObj(long A,long B,long double N){
double S=0;
if(B==0){
cout<<"\nSin Solucion ";
system("pause");
exit(1);
}
else
for(long i=1;i<=N;i++)
S=S+powf(-1,i+1)*((2*i-1)*A-
powf(facto(2*i)*B,1.0/(2*i)))/(2*i*B);
return(S);
}

void main(){
SuObjet suma;
long double M;long R,S;char rpt;
do{system("cls");
cout<<"Ingrese el Valor de A= ";
cin>>R;
cout<<"Ingrese el Valor de B= ";
cin>>S;
suma.LeeAB(R,S);
cout<<"Ingrese el Numero de Terminos N= ";
cin>>M;
suma.LeeN(M);
cout<<"\nEl Factorial de N= "<<M<<" Es "<<suma.facto(M)<<endl
<<"\nLa Suma es "<<suma.SumObj(R,S,M)<<endl
<<mensaje;
cin>>rpt;
}while(rpt=='s'||rpt=='S');
//system("pause");

Ing. Daniel Osorio Maldonado Pgina 2


}

Ing. Daniel Osorio Maldonado Pgina 3

You might also like