Professional Documents
Culture Documents
Week 12
Week 12
#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
}
}
#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>
// function prototype
void main()
int height;
int counter = 0;
cout << "How many disks are you going to start with? ";
// recursive call
_getch();
counter)
if (h == 1) // terminating point
{
cout << " Move a disk from ";
counter++;
else
topeg,counter);
counter++;