Write a simple program for finding factorial in C++ ?

Showing Answers 1 - 5 of 5 Answers

Sainath Rao

  • Dec 6th, 2011
 

Code
  1. int calfactorial (int x)

  2. {

  3.  int finalx = 1;

  4.  if (x==1) return 1;

  5.  for (int i = 1; i <= x; ++i) finalx = finalx * i;

  6.  return finalx;

  7. }

Paul_Singh

  • Feb 7th, 2012
 

Should use the fact that in n!, n is only a positive integer. As the number increase fairly rapidly, maybe a double return is better than a signed int.

Code
  1. double factorial( unsigned int n )

  2. {

  3.     double result = 1;

  4.     for(unsigned int i = 1; i <=n; i++ )

  5.     {

  6.         result*=i;

  7.     }

  8.     return result;

  9. }

FHGCVJ

  • Apr 14th, 2012
 

..

Code
  1. int calfactorial (int x)

  2. {

  3.  int finalx = 1;

  4.  if (x==1||x==0) return 1;

  5.  for (int i = 1; i <= x; ++i) finalx = finalx * i;

  6.  return finalx;

  7. }

  Was this answer useful?  Yes

Anugya Singh

  • Sep 4th, 2012
 

Code
  1. #include<iostream.h>

  2. #include<conio.h>

  3. void main()

  4. {

  5. clrscr();

  6. int a,i,f=1;

  7. cout<<"Enter the Number whose factorial value is to be find

  8. ";

  9. cin>>a;

  10. for(i=a;i>0;i--)

  11. {

  12. f=f*i;

  13. }

  14. cout<<"Fctorial is "<<f;

  15. getch();

  16. }


  Was this answer useful?  Yes

sakshi

  • Oct 4th, 2012
 

Code
  1. class factorial

  2. {

  3. public static void main(String args[])

  4. int f=1,i,n;

  5. n=Convert.ToInt32(Console.ReadLine());

  6. for(i=1;i<=n;i++)

  7. {

  8. f=f*i;

  9. }

  10. Console.WriteLine("factorial of the no :"+f);

  11.  

  12. Console.ReadKey();

  13. }


  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions