Factorial form

Factorial form of a number - C Programming Example

In this C programming example the desired number  will be given as input. Output will show the factorial form of  the desired number.

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 ! = 5 × 4 × 3 × 2 × 1 = 120.
The value of 0! is 1, according to the convention for an empty product.
The factorial operation is encountered in many areas of mathematics, notably in combinatorics, algebra, and mathematical analysis. Its most basic occurrence is the fact that there are n! ways to arrange n distinct objects into a sequence (i.e., permutations of the set of objects). This fact was known at least as early as the 12th century, to Indian scholars. Fabian Stedman, in 1677, described factorials as applied to change ringing. After describing a recursive approach, Stedman gives a statement of a factorial (using the language of the original):
Now the nature of these methods is such, that the changes on one number comprehends [includes] the changes on all lesser numbers, ... insomuch that a compleat Peal of changes on one number seemeth to be formed by uniting of the compleat Peals on all lesser numbers into one entire body.
The notation n! was introduced by the French mathematician Christian Kramp in 1808.

C Programming Code

    int fact=1, i, n;
    printf("Input the number \n");
    for (i=1;i<=n;i++)
        fact= i*fact;

        printf("The factorial form of the desired number is %d",fact);


Output of the C program code

C Programming Example
Factorial of 5

Explanation of the code

·     #include<stdio.h>
Ø This are called header file.A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source filesThe C programming language provides many standard library functions for file input and output. These functions make up the bulk of the C standard library header<stdio.h>.In the other hand<conio.h> is a C header file used mostly by MS-DOS compilers to provide console input/output. It is not part of the C standard library or ISO C.This header declares several useful library functions for performing "console input and output" from a program.
·       main()
Ø In C, program execution starts from the main() function. The main function can in-turn call other functions. When main calls a function, it passes the execution control to that function. The function returns control to main when a return statement is executed or when end of function is reached.
·       int fact, i, n;

Ø An integer is a  whole number (not a fraction) that can be positive, negative or zero. Therefore, the numbers 10,0, 25 and 5.148 are all intergers .Unlike floating point numbers, integers cannot have decimal place. Here we declare “n” “fact”and  “i”variable in  integer type. So this two variable cannot  take values containing decimal number.
·       printf() function
Ø C uses printf() function to write from the input devices. This function has been declared in the header file called stdio.h. Any text written within the pair of quotes ("") is displayed as such by printf() function on the screen. 
·       scanf() function
Ø This function is used to get input from the user of the program.scanf(“%d”,&n”)is used here. So, this program will read in a integer value for “a” variable that the user enters on the keyboard(%d is for float values.
·       printf; (“the factorial form of the number is %d”, fact)
Ø %d is used to print the main result.
·       getch() function
Ø getch() is used to hold the console(output) window on the screen after the whole program run is completed till the user enters a key from keyboard. This function is present in the header file called conio.h.

This program will show you the factorial form of any number. What you need to do is, just input the number. 

No comments

Powered by Blogger.