10.04.2012

Factorial C program,Algorithm,Flowchart

Q. Write a C program to find the factorial value of a number. Also write the algorithm and draw flowchart.

Ans.

/*c program to find out factorial value of a number*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int n,i,fact=1;
 printf("Enter any number : ");
 scanf("%d", &n);
 for(i=1; i<=n; i++)
    fact = fact * i;
 printf("Factorial value of %d = %d",n,fact);
 return 0;
}

The output of above program would be:
Output of calculate factorial value  of a number C program
Screen shot for calculate factorial value
of a number C program



Algorithm for calculate factorial value of a number:

[algorithm to calculate the factorial of a number]
step 1. Start
step 2. Read the number n
step 3. [Initialize]
        i=1, fact=1
step 4. Repeat step 4 through 6 until i=n
step 5. fact=fact*i
step 6. i=i+1
step 7. Print fact
step 8. Stop
[process finish of calculate the factorial value of a number]

Flowchart for calculate factorial value of a number:

flowchart for calculate factorial value of a number
Figure: Flowchart for calculate factorial value of
a number C program

32 comments:

  1. it must be i-- else it will be infinite loop

    ReplyDelete
    Replies
    1. no i++ is absolutely correct
      progrm runs succesfully by taking i++.....

      Delete
    2. i++ is absolutely correct
      program runs succesfully by taking i++

      Delete
    3. Program is right
      But flow chart is wrong.
      True should not led to stop
      Hence arrow between
      fact=fact*I and END should be
      Erased.

      Delete
  2. can you tell me on which data structure this program is running>

    ReplyDelete
  3. Replies
    1. Because factorial start with 1*2*3*....*n that's why we take fact = 0 as initial value

      Delete
  4. fact=1 must be one becoz starts from 1 multyplying.. if we take 0 fact is 0 for any number..

    ReplyDelete
  5. i need same algorithm but through recurssion. Please, if anyone can help me out.

    ReplyDelete
    Replies
    1. Recursion is the worst way to do this ... reason is that it is the slowest and could result in stack overflow for very large numbers. Also if your factorial is only for integers and you need it to be the fastest then just compute them up front and table them ... then use the number to index into the table. Since a 32 bit number maxes out at around 4 billion and 13 factorial is beyond that then the table only needs to be 12 elements wide.

      Delete
  6. I need some of flowchart anybady can help with me

    ReplyDelete
  7. i think the two statement (i=i+1) and (fact=fact*i) will be interchanged....

    ReplyDelete
  8. I don't know how create a programs anyone can U teach me plz.......

    ReplyDelete
  9. Can anyone say where to get the flowchart and algorithm of EMPLOYEE DETAIL USING UNION program please?

    ReplyDelete
  10. I need some of flowchart anybody can help me

    ReplyDelete
  11. Please can you write me the pseudocode of the Factorial of any number as the flow chart was drawn above

    ReplyDelete
  12. plese give pseudocode for this

    ReplyDelete
  13. It is really helpful thanks ����

    ReplyDelete
  14. Flow chart is wrong

    True condition leads to stop , which is absolutely wrong,
    Program only ends when the condition becomes false.
    Therefore the downward arrow to stop from /fact = fact*i/ should be erased

    ReplyDelete
  15. Look in the diagram carefully , true will not lead to stop . After reaching fact it will return to the starting of the loop .
    Still The arrow from fact to stop is wrong .

    ReplyDelete