Tuesday 26 August 2014

C Program to find out factorial of a number



C Program to find out factorial of a number

This is a C program to find out the factorial of a given number. In this example, it takes the number as input to the program, calculates the factorial of the number and prints the result.
In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example:

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

See the source code of the program

/*
            Program to find out factorial of a number
            input   : a number
            output  : factorial
*/

# include <stdio.h>

void main () {
            long int n,factorial, i;

            clrscr();

            printf ("Input\n-----\n\n");
            printf ("Enter a number:");
            scanf ("%ld", &n);

            factorial = 1;

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

                        factorial  = factorial  * i;

            printf ("\n\nOutput\n------\n\n");
            printf("Factorial of %ld is %ld", n, factorial);

            printf("\n\nPress any key to continue...");
            getch();
}

Output



No comments:

Post a Comment