Wednesday 25 November 2015

Program to find the Factorial of a number.

//Program to find the Factorial of a number.
#include<stdio.h>
#include<conio.h>

void main()
{
      int number,i; // variable 'number' is used to take input from user 
      int factorial; // integer variable used to store the factorial of a number

// Memory allocated is depended on the type of compiler

      clrscr(); // clearing the console screen (part of 'conio.h' header file)

      printf("Enter the number: "); //asks for a integer value
      scanf("%d",&number); // stores the integer value in variable 'number'
      

// logic for factorial of a number

      factorial=1; // initalizes 'factorial' with 1
      for(i=number ; i>0 ; i--) /* here the loop starts from number because the factorial of a number is in the form : number*(number-1)*(number-2)........*/
      {
      factorial = factorial*i; // calculates the factorial of a number
      }// end of i loop

      printf("The factorial of  %d is:%d",number,factorial); // prints the factorial of the number      
     
     getch(); // holding the output screen
}

No comments:

Post a Comment