Showing posts with label factorial. Show all posts
Showing posts with label factorial. Show all posts

Wednesday, June 13, 2012

Factorial of a number using class in cpp

//This program is compiled using GCC

#include<iostream>
using namespace std;

class factorial
{
    int num;
   
    public:
        void getinfo()                 //1st member function of class factorial
        {
            cout << "\n Enter the number for which the factorial is to be found: ";
            cin >> num;
        }
       
        void facto()          
//2nd member function of class factorial        
        {
            int i, fact=1;
           
            if(num == 0 || num == 1)
            {
                cout << "\n factorial of "<< num << " is 1";
            }
           
            else
            {
                for(i=1; i<=num; i++)
                {
                    fact = fact*i;
                }
                cout << "\n factorial of " << num << " is: " << fact;
            }
        }
};

int main()
{
    factorial f1;                //creating the object of class 'factorial'
    f1.getinfo();
    f1.facto();
   
    cin.ignore();      //these 2 fun are use
    cin.get();          //for holding the output screen
}

Thursday, March 22, 2012

Program to find factorial using recursion in c

//1st way
#include <stdio.h>
#include <conio.h>

int fact(int);

int main()
{
    int num, f;
    printf ("\n Enter a number: ");
    scanf ("%d", &num);
    f = fact(num);
    printf (" \n Factorial of %d is: %d ", num, f);
}

int fact(int n)
{
    if(n == 1)
    {
        return 1;
    }
   
    else
    {
        return (n*fact(n-1));
    }

}

____________________________________________________________________
//2nd way
#include <stdio.h>
#include <conio.h>

int getFactorial(int n)
{
    int local;
    if (n > 1)
    {
        local = n * getFactorial(n-1);
        return local;
    }

    else
        return 1;
}

int main()
{
    int num, f;
    printf ( "\n Enter a number: " );
    scanf ("%d", &num);
    f = getFactorial(num);
    printf ( "\n Factorial of %d is: %d ", num, f);
}

Thursday, March 8, 2012

A simple program to find factorial of a number

 // A simple program to find factorial of a number
#include<iostream>
using namespace std;

int main()
{
    int  a;
    cout << "\n Enter the no. to find factorial: ";
    cin >> a;


        int fact=1;
        for(int i=1;i<=a;i++)
        {
            fact=i*fact;
        }
   
    cout << "\n\n FACTORIAL= " << fact;
}

____________________________________________________________________________________
// program to find factorial of a number using functions
#include<iostream>
using namespace std;

int factorial(int);                //declaration of a function named factorial

int main()
{
    int fact, a;
    cout << "Enter the no.  to find factorial: ";
    cin >> a;

    fact = factorial(a);
    cout << "\n\n FACTORIAL= " << fact;
   
}

int factorial(int x)               //definition above declared function is given here
    {
        int n=1;
        for(int i=1;i<=x;i++)
        {
            n=i*n;
        }
    return (n);
}

___________________________________________________________________________________
// factorial of 1 to 10 (first 10) natural number
#include<iostream>
using namespace std;

int main()
{
    int  b;
   
    for(b=1; b<=10; b++)
    {
        long int fact = 1;
        for(int i=1;  i<=b;  i++)
        {
            fact=i*fact;
        }
        cout<< "\n\nFACTORIAL of: " << b << " is " << fact;
    }   
}