C++ tutorial, C++ programs, C++ Exercises, C++ Example Programs

Showing posts with label Program Factorial by While Loop Logic. Show all posts
Showing posts with label Program Factorial by While Loop Logic. Show all posts

Friday, January 10, 2014

Program Factorial by While Loop Logic

Q: Write a program in C++ to input a number and display its factorial. Use while Loop.

Easyway How to Write a program in C++ to input a number and display its factorial

Easyway How to Write a program in C++ to input a number and display its factorial


Program factorial : How to understand mathematical  Logic behind factorial?

We know that factorial of a number is equal to the product of all numbers from one to the given number. For example
Factorial of  3 = 1 x 2 x 3   hence 6
Factorial of  4 = 1 x 2 x 3 x 4 , hence 24
Similarly Factorial of  N = 1 x 2 x 3 .......... x n
If  N = 5 then
Factorial = 1 x 2 x 3 x 4 x 5 , hence 125
Which Statements we Need For Factorial Program?
1. We need cout<< for displaying messages and results to the user.
2. We need cin>> for inputting a number.
3. We need assignment statement, for example fact = 1;
4. We need a while loop statement to multiply all numbers from 1 to N.
And above all we need some variable declarations as well.

Coding : Find Factorial of a Number by while Loop


/*
(c)EasyCppProgramming.Blogspot.Com
C++ is Simplified Here!
Target 1001 C++ Example Programs!
EasyCppProgramming.Blogspot.Com
*/

// Program To Input a Number and Find
// its Factorial by while Loop
#include<iostream>
#include<conio.h>


 int main()                  
 {
   int num;
   clrscr();
   cout<<"Enter a Number to Display\nits Factorial=";
   cin>>num;

   long i=1, fact=1;
   while(i<=num)
   {
   fact = fact * i;
   i++;
   }
   cout<<"Factorial of "<<num<<" = "<<fact<<endl;
   getch();
   return 0;  
 }

 A Sample Run on Paper of Program Factorial

1.  Let we input 3.
2. Now num = 3, we also initialize i and fact variables to 1.So that now i = 1 and fact =1
3. Condition of While loop is tested (i<=num) putting the values 1<=3 which is true.
4. So while loop starts

     First Iteration

      fact = fact * i
      before execution
      1   =  1    *   1
      fact =1 and i = 1
      after execution
      fact =1  and i =1
     The value of i is incremented by 1 so i=2

     

Second Iteration

Since i<=num that is 2<=3 is true so loop takes second iteration    
      fact = fact * i
      before execution
      1   =  1    *   2
      fact =1 and i = 2
      after execution
     fact =2  and i =2
     Note: First of all, Right hand side expression (1 * 2) is evaluated and the result is assigned to the variable fact on left hand side of  =.
The value of i is incremented by 1 so i=3


Third Iteration

Since i<=num that is 3<=3 is true so loop takes third iteration    
      fact = fact * i
      before execution
      2   =  2    *   3
      fact =2 and i = 3
      after execution
      fact =6  and i =3
     The value of i is incremented by 1 so i=4

 Since i<=num that is 4<=3 is false so loop ends now.

The control goes to next statement which is
  cout<<"Factorial of "<<num<<" = "<<fact<<endl;
So it will print Factorial of 3 = 6
Share:

EasyCPPprogramming.blogspotcom

Labels