Need a project done?

C++ Programming Developer

Search This Blog

Showing posts with label recursive. Show all posts
Showing posts with label recursive. Show all posts

Printing Linked List Recursively in C++

Printing Linked List Recursively in C++ Program is given below:


// Printing Linked List Recursively in C++
// Linked List is created by inserting at head and then printing recursively.
#include <iostream>
using namespace std;

Printing Linked List in Reverse Order Recursively in C++


Printing Linked List in Reverse Order Recursively in C++ Program is given below:

// Printing Linked List in Reverse Order Recursively in C++
// Insertion at head is done in the following program
#include <iostream>
using namespace std;

////////////////////////////////////////////////////////////////
struct link                           //one element of list
{
int data;                          //data item
link* next;                        //pointer to next link
};
////////////////////////////////////////////////////////////////
class linklist                        //a list of links
{
private:
link* first;                    //pointer to first link also known as head.
public:
linklist()                      //no-argument constructor
{ first = NULL; }            //no first link
link* getHead(){ return first; }
void additem(int d);            //add data item (one link)
void displayRecursivelyReversed(link* current);                 //display all links in REVERSE ORDER.
};
//-------------------------------------------------------------void
void linklist::additem(int d)         //add data item
{
link *newLink = new link;
newLink->data = d;
//insert at head
newLink->next = first;
first = newLink;
}
//-------------------------------------------------------------void
void linklist::displayRecursivelyReversed(link* current)              //display all links
{
if (current != NULL){
//move to next node.
displayRecursively(current->next);
cout << current->data << endl;
}

}
////////////////////////////////////////////////////////////////
int main()
{
linklist li;       //make linked list
li.additem(25);    //add four items to list
li.additem(36);
li.additem(49);
li.additem(64);
li.displayRecursivelyReversed(li.getHead());      //display entire list
return 0;
}

Pi Recursion C++

The value of π can be determined by the series equation
π=4∗(1-1/3+1/5−1/7+1/9−1/11+1/13…)
Write a program that prompts the user to enter a positive odd number n and uses recursion to approximate the value of π using the given formula including term up through 1/n.
Solution:

Recursive function to search through array

Recursive function to search through array.

Write a program that asks the user to enter 10 integers, stores these numbers in an array. The program should ask if the user would like to search an element in the array, in which case the program should then prompt the user to enter the number to look for. Use a recursive function find( int index, int target, int array[] ) that does sequential search for a target in this array. Display an appropriate message to the user after searching the element.

Solution:

Palindrome Recursion C++

Write a recursive function of determining whether a string is palindrome or not.
Note: A palindrome is a string that can be read the same both forward and backwards such as racecar, mom etc.

Solution:

Recursion Smallest element of array C++

Write a recursive function that finds and returns the minimum or smallest elemnt in an array, where the array and its size are given as parameters.

Solution:

Recursion Question

What will be the output?
#include <iostream>
using namespace std;
void f(int);
int main()
{
 f(2);
 return 0;
}
void f(int n)
{
 if (n == 0)
  cout << n << ' ';
 else
  f(n-1);
}
Reply in comments.

Recursive Power Function C++

Write a function int Power(int a, int b) that calculates the power a^b. You are allowed to use multiplication operator *.
Solution:

Recursive Multiplication using addition

Write a function int Mul(int a, int b) that computes the product of two integers a and b. The only arithmetic operation that you are allowed to use is addition '+'.
Solution:

Recursive function Sum of elements of array

Write a recursive function that computes and returns the sum of all elements in an array, where the array will be created dynamically by getting the size from the user. Array and its size are given as parameters.
Solution:

Recursive function to calculate Sum of all numbers from 1 to n in C++

Write a recursive function in C++ that computes the sum of all numbers from 1 to n, where n is given as parameters.
Solution:

Program to produce Fibonacci sequence (first 25 terms) | Recursive function to find any Fibonacci term

Write a function to obtain the first 25 numbers of a Fibonacci sequence. In a Fibonacci sequence the sum of two successive terms gives the third term. Following are the first few terms of the Fibonacci sequence: 1 1 2 3 5 8 13 21 34 55 89...

#include<iostream>
void fibonaci_sequence();
using namespace std;

void main()
{
            fibonaci_sequence();//Prints first 25 terms of Fibonaci Sequence.
}

void fibonaci_sequence()
{
            int term[27];
            term[0] = term[1] = 1;
            cout<<term[0]<<"\t "<<term[1]<<" \t";
           
            for (int i = 2; i<25; i++)
            {
                        term[i] = term[i-2]+term[i-1];
                        cout<<term[i]<<" \t";
            }
            cout<<endl;
}

//==========================================================================

Recursive Function to find any fibanacci term.

#include <iostream>
using namespace std;

int fib(int);

int main()
{
int x;
cout << "X: ";
cin >> x;

cout << fib(x) << endl;

return 0;
}


int fib(int term)
{
if (term == 1 || term == 2)
return 1;
else
return fib(term-1) + fib(term-2);
}

"Don't let anyone ever make you feel like you don't deserve what you want."