47 : C Program to find factorial of a number

Check out the complete list of c-programs : C Program List    



What is factorial of a number ?

It is a product of an int and all the integers below it,

Example 5! = 5 x 4 x 3 x 2 x 1 ! = 120

Logic :


  for (i = 1; i <= number ; i++) {
   
      factorial = factorial * i;    
      
  }



Variables :

number : Datatype int : Holds value of year inputted by the user. %d is the Format specifier for int's

i : Datatype int : variable used in for loop

unsigned : Datatype long : To hold result. As factorial cannot be negative we set it as unsigned








*
 * 1000+ C programs + tutorials
 *
 * 
 * 47_Calculate_factorial_of_a_number_C_program.c
 *
 *
 *  Created on: Oct 20, 2014
 *  Author: Code2care.org
 */

#include 
//#include 



void main() {

int number;
int i;
unsigned long factorial = 1;
  

//clrscr();

    printf("C Program Calculate Factorial of entered number.");

    printf("\n\n Enter Number : ");
    scanf("%i", &number);

    

     
     
  //Logic factorial

  for (i = 1; i <= number ; i++) {
      factorial = factorial * i;    
  }
  
  printf("\n\n %d! = %lu",number,factorial);
    
   
  

//getch();



}









The best way to learn C programming is to practice more and more of programs . Code2care C Programming tutorials provide 1000+ programs in C that you can study and become an expert in the language. Programs are divided into categories depending upon type and complexity.

BSc. IT, BSc. Computer Science, BSc. IT, MSc. IT, MSc. Computer Science, MCA, BTech IT & BTech Engineers may find these programs very useful.