Doing Factorial in C++ for Console
Application
Factorial.h
#ifndef FACTORIAL_H
#define FACTORIAL_H
#pragma once
class Factorial
{
public:
Factorial();
int fact(int n);
~Factorial(void);
};
#endif
|
Factorial.cpp
#include "StdAfx.h"
#include "Factorial.h"
Factorial::Factorial() // constructor
{
}
int Factorial::fact(int n)
{
if (n == 0)
{
return 1;
}
else
{
return n * fact(n-1);
}
}
Factorial::~Factorial()
{
}
|
Recursion.cpp
// Recursion.cpp :
Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include "Factorial.h"
using namespace
std;
int _tmain(int
argc, _TCHAR* argv[])
{
Factorial
f; //created
object of Factorial
// calculate and print "3!"
cout
<< f.fact(3) << endl;
return 0;
}
|
Console
output
No comments:
Post a Comment