Skip to content

Commit

Permalink
Merged pull request #91 from imoizza/imoizza
Browse files Browse the repository at this point in the history
Two Recursion Files
  • Loading branch information
Rajspeaks authored Oct 26, 2021
2 parents 2f67b35 + d7cbb9d commit c4a6e70
Show file tree
Hide file tree
Showing 5 changed files with 323 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Contributors-2021.txt
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,16 @@ Twitter profile url: N/A
Country: India
Bio: Student

*Name: Moizza Azhar
*Github username: @imoizza
How many pull requests have you done here: 1

LinekdIn profile url: https://www.linkedin.com/in/moizza-azhar-3b7170200/
Twitter profile url: N/A

Country: Pakistan
Bio: Student


*Name: Aravindha Hariharan
*Github username: aravindha1234u
Expand Down
232 changes: 232 additions & 0 deletions Moizza Azhar/recursion part 1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
// ConsoleApplication63.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
using namespace std;

void odd(int n);
void even(int n);

int fun1(int n)
{
if (n == 1)
return 1;
else
return (1 + fun1(n - 1));
}

int fun2(int n)
{
if (n == 0)
return 1;
else
return (7 + fun2(n - 2));
}

int fact(int n)
{
if (n == 1)
return 1;
else
return n * fact(n - 1);
}

int fact2(int n)
{
int a = 1;
while (n != 0)
{
a = a * n;
n--;
}
return a;

}

void even(int n)
{
if (n <= 10)
{
cout << n - 1 << " ";
n++;
odd(n);
}
return;
}

void odd(int n)
{
if (n <= 10)
{
cout << n + 1 << " ";
n++;
even(n);
}
return;
}

void fun3(int n)
{
if (n == 0)
return;
else
{
cout << n << " ";
return fun3(n - 1);
}
}

void fun4(int n)
{
if (n == 0)
return;
else
{

fun4(n - 1);
cout << n << " ";
}
}

int fun5(int n)
{
if (n == 0)
return 0;
else
{
return 1 + fun5(n / 2);
}
}

void get(int n)
{
//int b=0;
if (n < 1)
return;
get(n - 1);
get(n - 3);
//b = n + b;
cout << n << " ";
}

void get2(int n)
{
int i = 0;
if (n > 1)
{
get2(n - 1);
}
for (int i = 0; i < n; i++)
{
cout << "*";
}
}

int get3(int n)
{
static int i = 50;
int k;
if (i == n)
{
cout << "something !";
k = get3(i);
return 0;
}
else return 0;

}

int get4(int n)
{
int x = 1, k;
if (n == 1)
return x;
for (int k = 1; k < n; ++k)
{
x = x + get4(k) * get4(n - k);
}
return x;
}

void get5(int n)
{
static int d = 1;
cout << n << " ";
cout << d << " ";
d++;
if (n > 1)
get5(n - 1);
cout << d << " ";
}


int main()
{
int num;
int a = 1;
int n = 3;
cout << "\n\t Direct Recursion\n" << endl;
cout << fun1(n);
cout << endl;
cout << fun2(4);
cout << endl;

cout << "\n\t Indirect Recursion \n" << endl;

cout << "Wap the function from 1 to 10 ,if number is odd add 1 and number is even substract 1. \n";
odd(a);

cout << "\n\t Tail recursion\n" << endl;
fun3(4);
cout << endl;

cout << "\n\t non-Tail recursion\n" << endl;
fun4(4);
cout << endl;
cout << fun5(8);
cout << endl;

cout << "\n\tRecursive VS Iterative\n" << endl;
cout << "Now Calculate the Factorial of the number " << endl;
cout << "Enter the number you want to caculate the factorial : ";
cin >> num;
cout << endl;
cout << "Factorial : " << fact(num) << endl;
cout << "Factorial : " << fact2(num) << endl;

cout << "\n\t EXAMPLES \n" << endl;

cout << "EXAMPLE # 1 : ";
get(6);
cout << endl;

cout << "EXAMPLE # 2 : ";
get2(3);
cout << endl;

cout << "EXAMPLE # 3 : ";
cout<<get3(5);
cout << "\n(d) The funvtion will exhaust the runtime stack or run into infinite loop when j=50 .";
cout << endl;

cout << "EXAMPLE # 4 : ";
cout << get4(5);
cout << endl;

cout << "EXAMPLE # 5 : ";
get5(3);
cout << endl;

return 0;
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
Binary file added Moizza Azhar/recursion part 1.exe
Binary file not shown.
81 changes: 81 additions & 0 deletions Moizza Azhar/recursion part 2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// ConsoleApplication64.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
using namespace std;

int fib(int n)
{
if (n <= 2)
return 1;
else
return fib(n - 1) + fib(n - 2);
}

void foo(int n)
{
if (n <= 0)
return;
else
cout << n << " ";
foo(n - 1);
}

void bar(int n)
{
if (n <= 0)
return;
else
cout << n << " ";
bar(n - 2);
}

void dib(int n)
{
if (n <= 0)
return;

dib(n - 1);
cout << n << " ";
dib(n - 1);
cout << n << " ";
}

void lib(int n)
{
if (n <= 0)
return;

lib(n - 2);
cout << n << " ";
lib(n - 2);
cout << n << " ";
}

int main()
{
int n;
cout << fib(7) << endl;
foo(5);
cout << endl;
bar(8);
cout << endl;
dib(5);
cout << endl;
cout << endl;
lib(8);
cout << endl;

return 0;
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
Binary file added Moizza Azhar/recursion part 2.exe
Binary file not shown.

0 comments on commit c4a6e70

Please sign in to comment.