C++ 19: Recursive Functions
if a function contains call to itself then such function is called as Recursive function.
Note that the statement which containes call to the same
function must be specified in a condition therwise the function
will call itself infinite times.
#include <iostream>
using namespace std;void hello(){ cout << "Hello" << endl;}
void hi(){ hello(); cout << "Hi" << endl;}
int main(){ hi(); return 0;}Output:
Hello HiExample 2
#include <iostream>
using namespace std;fact(int n){ if (n == 1) { return n; } else { return (n * fact(n - 1)); }}main(){ int n, z; cout << "Enter a number : "; cin >> n;
z = fact(n); cout << "Factorial is " << z << endl;}Output:
Enter a number : 5
Factorial is 120WAP
#include <iostream>
using namespace std;int area(int l,int b){ int a = l*b; return a;
}int main(){ int L1,B1; int L2,B2; int A1,A2;
cout<<"Enter the length and breadth of inner Rectangle: "; cin>>L1>>B1; cout<<"Enter the length and breadth of outer Rectangle: "; cin>>L2>>B2;
A1=area(L1,B1); cout<<"Area of inner rectangle is "<<A1<<endl; A2=area(L2,B2); cout<<"Area outer rectangle is "<<A2<<endl;
int LA = A2-A1; cout<<"Area of shaded(lined) part is "<<LA<<endl;
return 0;}Output:
Enter the length and breadth of inner Rectangle: 5 8 12
Enter the length and breadth of outer Rectangle: 10 12 20
Area of inner rectangle is 40
Area outer rectangle is 120
Area of shaded(lined) part is 80WAP
#include <iostream>
using namespace std;int payment(int W, int WD){ int P = W*WD; return P;}main(){ char N1[20],N2[20]; int W1,WD1; int W2,WD2;
cout<<"Enter the Name, Wage, Working-days of 1st Worker: "; cin>>N1>>W1>>WD1; cout<<"Enter the Name, Wage, Working-days of 2st Worker: "; cin>>N2>>W2>>WD2;
int P1 =payment(W1,WD1); cout<<"Payment of "<<N1<<" = "<<P1<<"Rs."<<endl; int P2 =payment(W2,WD2); cout<<"Payment of "<<N2<<" = "<<P2<<"Rs."<<endl;
int T = P1+P2; cout<<"Total payment is "<<T<<" Rs."<<endl; }Output:
Enter the Name, Wage, Working-days of 1st Worker: Ramu
500 30
Enter the Name, Wage, Working-days of 2st Worker: Hari 700 30
Payment of Ramu = 15000Rs.
Payment of Hari = 21000Rs.
Total payment is 36000 Rs.
Comments
Post a Comment