Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

#include <iostream>

#include <conio.h>
#include <iomanip>
using namespace std;
void reverse(int); // a recursive function
int noDigits(long int); // count no of digits
int sumDigit(long int); // sum the digits
void main()
{
int number;
cout << "Enter a positive integer value ";
cin >> number;
reverse(number); // function call
cout << "number of digits is" <<
noDigits(number) << endl;
cout << "sum of digits is " <<
sumDigit(number) << endl;
_getch();
}
void reverse(int n)
{
cout << n % 10;
if ((n / 10) != 0) // more digits
reverse(n / 10); // recursive call
}

int noDigits(long int n)


{
if (n < 10)
return 1;
else
return 1 + noDigits(n / 10);
}
int sumDigit(long int n)
{
if (n < 10)
return n;
else
return (n % 10) + sumDigit(n / 10);

}
#include <iostream>
#include <conio.h>
using namespace std;
void Tobase2(int n, int base2);
void main()
{
Tobase2(2, 2);
_getch();
}
void Tobase2(int n, int base2)
{
if ((n / base2) == 0)
// terminating point
cout << n % base2;
else
{
Tobase2(n / base2, base2);
cout << n % base2;
}
}
#include <iostream>

#include <conio.h>

using namespace std;

void move(int, char, char, char, int&);

// function prototype

void main()

int height;

int counter = 0;

cout << "How many disks are you going to start with? ";

cin >> height;

cout << endl;

// use a loop to do this for height from 3 to 31.

move( height, 'A', 'C', 'B', counter);

// recursive call

count << “Total number of moves is : “

counter << endl;

_getch();

// Recursive procedure for determining moves

// keep this order -- from, to, using -- in mind

// when you read the recursive calls.

void move(int h, char frompeg, char topeg,

char usingpeg, int&

counter)

{ // only value parameters are used

if (h == 1) // terminating point

{
cout << " Move a disk from ";

cout << frompeg;

cout << " To ";

cout << topeg << endl;

counter++;

else

{ move(h - 1, frompeg, usingpeg,

topeg,counter);

cout << " Move a disk from ";

cout << frompeg;

cout << " To ";

cout << topeg << endl ;

counter++;

move(h - 1, usingpeg, topeg, frompeg,counter);

You might also like