PROGRAM TO CALCULATE THE FACTORIAL OF A GIVEN NUMBER

C, C++, etc Programing examples. Post as many you can. Test the programs and discuss about the output of the programes.
Forum rules
Please Discuss & post only about the programes and its Output

PROGRAM TO CALCULATE THE FACTORIAL OF A GIVEN NUMBER

Postby rohit.wadhwa » Wed Mar 18, 2009 4:38 pm

Code: Select all
  /* PROGRAM TO CALCULATE THE FACTORIAL OF A GIVEN NUMBER */
  /*author-Rohit Wadhwa*/
   #include<stdio.h>
   #include<conio.h>
   void  main()
    {
    int n;
    int fact=1;
    int val;
    printf("Enter a number for Calculate Factorial:\n");
    scanf("%d",&n);
    val=n;
    while(n>1)
    {
    fact*=n;/*fact=fact*n*/
    n--;
    }
    printf("Factorial of %d are: %d\n",val,fact);
    getch();
    }
Thanks
Rohit Wadhwa
Site : Study Site
User avatar
rohit.wadhwa
Moderator
 
Posts: 55
Joined: Mon Mar 16, 2009 4:00 pm
Location: Hisar(Haryana)
membership: Senior Technician of The Institution of Engineers (India)- ST

ad
 

Re: PROGRAM TO CALCULATE THE FACTORIAL OF A GIVEN NUMBER

Postby irfanmasiullah » Wed Apr 14, 2010 6:09 am

Hello Mr. Rohit,
Thanks for the program, but I have some comments after execution.
This program is not calculating correct answer after 10 because value is getting hence I use "double" variable instead of int for factorial.
Find below my program in C++ , I appreciate comments....

/* Program to fing Factorial number*/

#include <iostream>
#include <cmath>
int main ()
{
int n; /* n= Given number for which we want factorial */
double f=1; /* f= factorial number */
using namespace std;
cout << "Please Enter the Number for which you want Factorial ?? " << endl;
cin >> n;
while (n!=0)
{
f=f*n;
n=n-1;}
cout << "Factorial is "<< f << endl;
system ("pause");
return 0;
}
Thanks & Best Regards -
Irfan M Ansari - Section B - Civil
MOB- 00966 561 770 321 - Dammam - Saudi Arabia
Email :irfanmasiullah@gmail.com
IM: irfanmasiullah@yahoo.com : irfanmasiullah@gmail.com
User avatar
irfanmasiullah
Super Member
Super Member
 
Posts: 77
Joined: Thu Jul 30, 2009 7:59 pm
Location: Mumbai / Dammam (Saudi Arabia)
membership: Senior Technician of The Institution of Engineers (India)- ST

Re: PROGRAM TO CALCULATE THE FACTORIAL OF A GIVEN NUMBER

Postby rohit.wadhwa » Thu Apr 15, 2010 11:26 am

Hello Mr Irfanmasiullah,

you r right,this program is only give right result up to 10.Nice work keep it up.
you can also used long type for factorial result.
Thanks
Rohit Wadhwa
Site : Study Site
User avatar
rohit.wadhwa
Moderator
 
Posts: 55
Joined: Mon Mar 16, 2009 4:00 pm
Location: Hisar(Haryana)
membership: Senior Technician of The Institution of Engineers (India)- ST

Re: PROGRAM TO CALCULATE THE FACTORIAL OF A GIVEN NUMBER

Postby ram1ramji » Wed Mar 23, 2011 5:37 pm

Thank you...


Regards
Ramji R
Regards
Ramji R
ram1ramji
Registered User
 
Posts: 20
Joined: Mon Sep 27, 2010 6:02 pm
membership: NA


Return to Computer Notes : Some Sample Programs

Who is online

Users browsing this forum: No registered users and 0 guests