New Posts  All Forums:Forum Nav:

A little recursion c++ app.

post #1 of 6
Thread Starter 
I wrote a recursion program in c++. It's a basic program to show how recursion works, all thought it could be commented a little better.

It prints a number and its factorial and then all the numbers that add up to its factorial as well.

Code:

//recursion

#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>

void instruct(void); //instructions function

unsigned long factorial ( unsigned long number);
unsigned long f_out ( unsigned long numb);

int
main(void)
{

instruct();
for ( int lol = 0; lol <= 50; lol++){
cout << setw ( 2 ) << lol << "! = " << factorial ( lol ) << setw( 5 ) <<  " = ";
f_out(lol);
cout << "n";
}
return 0;
}

unsigned long factorial( unsigned long number)
{
if ( number <= 1) // base case
return 1;
else
return number * factorial( number - 1 );
}

unsigned long f_out( unsigned long numb ) {
if ( numb <= 1 ) {
cout << "1";
return 1;}

else {
cout << numb << " * ";
return ( numb * f_out( numb - 1));
}
}
void
instruct()
{
cout << "Modify a factorial function to print its local variable and recursive call parameter.nn";
}
HELENKELLER
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 @ 2.4 eVGA 780I EVGA 8800GTS 4 GB OCZ SLI RDY 
Hard DriveOSKeyboardPower
400GB WD/Seagate Internal / 500 WD External Vista X64 Logitech MX5000 Ultra X2 750watt 
CaseMouseMouse Pad
Ultra Aluminus Mid-Tower Logitech MX5000 GO Army 
  hide details  
Reply
HELENKELLER
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 @ 2.4 eVGA 780I EVGA 8800GTS 4 GB OCZ SLI RDY 
Hard DriveOSKeyboardPower
400GB WD/Seagate Internal / 500 WD External Vista X64 Logitech MX5000 Ultra X2 750watt 
CaseMouseMouse Pad
Ultra Aluminus Mid-Tower Logitech MX5000 GO Army 
  hide details  
Reply
post #2 of 6
Very nice, any chance you would do a C# version?
Lee XT
(17 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-6300 Asus M5A97 SAPPHIRE Radeon HD 7850 AMD 4GB DDR3 1333MHZ 
RAMRAMRAMHard Drive
AMD 4GB DDR3 1333MHZ AMD 4GB DDR3 1333MHZ AMD 4GB DDR3 1333MHZ OCZ Vertex 4 256GB 
CoolingOSMonitorKeyboard
Corsair H80 Windows 8.1 Pro MCE Dell P2414H WHXV7  Microsoft Generic 
PowerCaseMouseMouse Pad
Ultra 600W Limited Edition NZXT Black Steel Razer Deathadder Razer Goliath 
Audio
Realtek HD Audio 
  hide details  
Reply
Lee XT
(17 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-6300 Asus M5A97 SAPPHIRE Radeon HD 7850 AMD 4GB DDR3 1333MHZ 
RAMRAMRAMHard Drive
AMD 4GB DDR3 1333MHZ AMD 4GB DDR3 1333MHZ AMD 4GB DDR3 1333MHZ OCZ Vertex 4 256GB 
CoolingOSMonitorKeyboard
Corsair H80 Windows 8.1 Pro MCE Dell P2414H WHXV7  Microsoft Generic 
PowerCaseMouseMouse Pad
Ultra 600W Limited Edition NZXT Black Steel Razer Deathadder Razer Goliath 
Audio
Realtek HD Audio 
  hide details  
Reply
post #3 of 6
Thread Starter 
yeah ill try to get it up tomorrow.
HELENKELLER
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 @ 2.4 eVGA 780I EVGA 8800GTS 4 GB OCZ SLI RDY 
Hard DriveOSKeyboardPower
400GB WD/Seagate Internal / 500 WD External Vista X64 Logitech MX5000 Ultra X2 750watt 
CaseMouseMouse Pad
Ultra Aluminus Mid-Tower Logitech MX5000 GO Army 
  hide details  
Reply
HELENKELLER
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 @ 2.4 eVGA 780I EVGA 8800GTS 4 GB OCZ SLI RDY 
Hard DriveOSKeyboardPower
400GB WD/Seagate Internal / 500 WD External Vista X64 Logitech MX5000 Ultra X2 750watt 
CaseMouseMouse Pad
Ultra Aluminus Mid-Tower Logitech MX5000 GO Army 
  hide details  
Reply
post #4 of 6
Did you do that for a class or just for you? Recursion is powerful, but a factorial calculation is much better implemented in loop form

Quote:
Originally Posted by Licht View Post
Very nice, any chance you would do a C# version?
Just out of curiosity, why?
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
post #5 of 6
Quote:
Originally Posted by rabidgnome229 View Post
Did you do that for a class or just for you? Recursion is powerful, but a factorial calculation is much better implemented in loop form



Just out of curiosity, why?
I can do C++ but its a chore. Saves time for me to have a code in native C#.
Lee XT
(17 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-6300 Asus M5A97 SAPPHIRE Radeon HD 7850 AMD 4GB DDR3 1333MHZ 
RAMRAMRAMHard Drive
AMD 4GB DDR3 1333MHZ AMD 4GB DDR3 1333MHZ AMD 4GB DDR3 1333MHZ OCZ Vertex 4 256GB 
CoolingOSMonitorKeyboard
Corsair H80 Windows 8.1 Pro MCE Dell P2414H WHXV7  Microsoft Generic 
PowerCaseMouseMouse Pad
Ultra 600W Limited Edition NZXT Black Steel Razer Deathadder Razer Goliath 
Audio
Realtek HD Audio 
  hide details  
Reply
Lee XT
(17 items)
 
  
CPUMotherboardGraphicsRAM
AMD FX-6300 Asus M5A97 SAPPHIRE Radeon HD 7850 AMD 4GB DDR3 1333MHZ 
RAMRAMRAMHard Drive
AMD 4GB DDR3 1333MHZ AMD 4GB DDR3 1333MHZ AMD 4GB DDR3 1333MHZ OCZ Vertex 4 256GB 
CoolingOSMonitorKeyboard
Corsair H80 Windows 8.1 Pro MCE Dell P2414H WHXV7  Microsoft Generic 
PowerCaseMouseMouse Pad
Ultra 600W Limited Edition NZXT Black Steel Razer Deathadder Razer Goliath 
Audio
Realtek HD Audio 
  hide details  
Reply
post #6 of 6
Thread Starter 
Quote:
Originally Posted by rabidgnome229 View Post
Did you do that for a class or just for you? Recursion is powerful, but a factorial calculation is much better implemented in loop form
did it for a class a while ago. thought i'd post it so people who don't know recursion. maybe it will help them understand better
HELENKELLER
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 @ 2.4 eVGA 780I EVGA 8800GTS 4 GB OCZ SLI RDY 
Hard DriveOSKeyboardPower
400GB WD/Seagate Internal / 500 WD External Vista X64 Logitech MX5000 Ultra X2 750watt 
CaseMouseMouse Pad
Ultra Aluminus Mid-Tower Logitech MX5000 GO Army 
  hide details  
Reply
HELENKELLER
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 @ 2.4 eVGA 780I EVGA 8800GTS 4 GB OCZ SLI RDY 
Hard DriveOSKeyboardPower
400GB WD/Seagate Internal / 500 WD External Vista X64 Logitech MX5000 Ultra X2 750watt 
CaseMouseMouse Pad
Ultra Aluminus Mid-Tower Logitech MX5000 GO Army 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming