22.3.21

Hw-6: Write a c++ OOP to find factorial using loop in C++

In this program, we will learn how to find factorial of a given number using C++ program? Here, we will implement this program with and without using user define function.

Logic:

  1. Input a number
  2. Initialize the factorial variable with 1
  3. Initialize the loop counter with N (Run loop from number (N) to 1)
  4. Multiply the loop counter's value with factorial variable

Program to find factorial using loop in C++


Enter an integer number: 6
Factorial of 6 is = 720    
What is factorial?
Answer: 

Factorial !

Example: 4! is shorthand for 4 × 3 × 2 × 1

Factorial Symbol

The factorial function (symbol: !) says to multiply all whole numbers from our chosen number down to 1.

Examples:

  • 4! = 4 × 3 × 2 × 1 = 24
  • 7! = 7 × 6 × 5 × 4 × 3 × 2 × 1 = 5040
  • 1! = 1

We usually say (for example) 4! as "4 factorial", but some people say "4 shriek" or "4 bang"

Calculating From the Previous Value

We can easily calculate a factorial from the previous one:

factorial multiply

As a table:

nn!  
1111
22 × 1= 2 × 1!= 2
33 × 2 × 1= 3 × 2!= 6
44 × 3 × 2 × 1= 4 × 3!= 24
55 × 4 × 3 × 2 × 1= 5 × 4!= 120
6etcetc 

  • To work out 6!, multiply 120 by 6 to get 720
  • To work out 7!, multiply 720 by 7 to get 5040
  • And so on

Example: 9! equals 362,880. Try to calculate 10!

10! = 10 × 9!

10! = 10 × 362,880 = 3,628,800

So the rule is:

n! = n × (n−1)!

Which says

"the factorial of any number is that number times the factorial of (that number minus 1)"

So 10! = 10 × 9!, ... and 125! = 125 × 124!, etc.

What About "0!"

Zero Factorial is interesting ... it is generally agreed that 0! = 1.

It may seem funny that multiplying no numbers together results in 1, but let's follow the pattern backwards from, say, 4! like this:

24/4=6, 6/3=2, 2/2=1, 1/1=1

And in many equations using 0! = 1 just makes sense.


Example: how many ways can we arrange letters (without repeating)?

  • For 1 letter "a" there is only 1 way: a
  • For 2 letters "ab" there are 1×2=2 ways: ab, ba
  • For 3 letters "abc" there are 1×2×3=6 ways: abc acb cab bac bca cba
  • For 4 letters "abcd" there are 1×2×3×4=24 ways: (try it yourself!)
  • etc

The formula is simply n!

Now ... how many ways can we arrange no letters? Just one way, an empty space:


So 0! =
1


6 comments:

  1. //Faizah Akter
    //202010061
    #include
    using namespace std;
    class B
    {
    public:
    int check()
    {
    int num,factorial=1;
    cout<<" Enter Number To Find Its Factorial: ";
    cin>>num;
    for (int a=1;a<=num;a++)
    {
    factorial=factorial*a;
    }
    cout<<"Factorial of Given Number is ="<<factorial<<endl;
    return 0;
    }

    };
    int main()
    {
    B obj;
    obj.check();
    }

    ReplyDelete
  2. Mahbubur rahman Limon
    Id:202030070

    #include
    using namespace std;
    class Factor
    {
    public:
    int count()
    {
    int counter, n, fact = 1;

    cout<<"Enter an integer Number :";
    cin>>n;
    for (int counter = 1; counter <= n; counter++)

    {
    fact = fact * counter;
    }
    cout<<"Factorial of value is = "<<fact;
    return 0;

    }
    };
    int main()
    {
    Factor obj;
    obj.count();
    }

    ReplyDelete
  3. //Faizah Akter
    //202010061
    #include
    using namespace std;
    class B
    {
    public:
    int check()
    {
    int num,factorial=1;
    cout<<" Enter Number To Find Its Factorial: ";
    cin>>num;
    for (int a=1;a<=num;a++)
    {
    factorial=factorial*a;
    }
    cout<<"Factorial of Given Number is ="<<factorial<<endl;
    return 0;
    }

    };
    int main()
    {
    B obj;
    obj.check();
    }

    ReplyDelete
  4. //Sumaiya Mariya
    //ID:201520785
    #include
    using namespace std;
    class B
    {
    public:
    int c()
    {
    int n,factorial=1;
    cout<<" Enter Number To Find Its Factorial: ";
    cin>>n;
    for (int i=1;i<=n;i++)
    {
    factorial=factorial*i;
    }
    cout<<"Factorial of Given Number is ="<<factorial<<endl;
    return 0;
    }

    };
    int main()
    {
    B obj;
    obj.c();
    }

    ReplyDelete
  5. //Robin Ahmed
    //201910109

    #include
    using namespace std;
    int main (){
    int number,factorial = 1;
    cout<< "enter the number "<>number;
    if(number < 0)
    cout << "can't find the factorial for negative number";
    else if (number<=1)
    cout << number << "!="<=2; counter --)
    {factorial=factorial*counter;}
    cout<< number << "!= "<<factorial;
    }



    return 0;

    }

    ReplyDelete
  6. //Monika Akter Ratna
    //202010275

    #include
    using namespace std;
    int main (){
    int num,fac = 1;
    cout<< "enter the number "<>num;
    if(num < 0)
    cout << "can't find the factorial ";
    else if (num<=1)
    cout << num << "! = "<=2; con --)
    {fac=fac*con;}
    cout<< num << "! = "<<fac;
    }



    return 0;

    }

    ReplyDelete

Comment Here