Factorial of a Number
This program calculates the the factorial of an integer number entered by the user. The function uses for loop to calculate the factorial and returns the number. Program terminates if a non integer number is entered. This C language program uses for loop in just a single statement to calculates the factorial of integer number.
/*******************************************************
* MYCPLUS Sample Code - http://www.mycplus.com *
* *
* This code is made available as a service to our *
* visitors and is provided strictly for the *
* purpose of illustration. *
* *
* Please direct all inquiries to saqib at mycplus.com *
*******************************************************/
#include <stdio.h>
int fact(int n);
int main(void) {
int current;
printf("Enter a positive integer [to terminate enter non-positive] > ");
scanf("%d", &t);
while (current > 0) {
printf("The factorial of %d is %d\n", current, fact(current));
printf("Enter a positive integer [to terminate enter non-positive] > ");
scanf("%d", &t);
}
}
/* n is a positive integer. The function returns its factorial */
int fact(int n) {
int lcv; /* loop control variable */
int p; /* set to the product of the first lcv positive integers */
for(p=1, lcv=2; lcv <= n; p=p*lcv, lcv++);
return p;
}
Tags: C Programming, Source Code
Like What you See?
Become one of the regulars by subscribing! You'll be the first to know when we add more great posts just like this. Join up by either RSS Feeds or Email Updates today!
There are 3 Comments to this post. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response or TrackBack from your own site.



































please see the program ….
#include <stdio.h> void main() { int a,fact,b; //fact 5! = 5*4*3*2*1 label: printf(" \n...... ..Factorial..............\n"); printf("Enter a number between 1 and 100\n\r"); scanf("%d",&a); b = a; if(a>100|| a = 1) { fact = fact * a; a = a-1; } printf("The factorial of %d is %d",b ,fact); printf("\n\rEnter an invalid number to exit"); goto label; return 0; }Yes, but on 13! standart C int is oferflowed. ;-)
#include
#include
void main()
{
int f=1,i,num;
clrscr();
printf(“Enter the value”);
scanf(“%d”,num);
for(i=1;i<=n;i++)
f=f*i;
printf("factorial of %d is %d",f);
getch();
}