Friday, April 11, 2014

Factorial of a Number – C Program

Problem Question


Write a program to find the factorial value of any number entered through the keyboard.

Explanation of Problem


We need to write a program which shall accept a whole number from the user, calculate its factorial, and print the result.

Code


#include <stdio.h>
/*Factorial.c*
*@Language: ANSI C*
*@Compiler: GNU GCC*
*@IDE: CodeBlocks 12.11*
*@Author: Toxifier*
*@URL: http://letsplaycoding.blogspot.com/*
*@Date: 11-04-2014**/
/*Note: This program works with integers only.
Carefully choose input such that the output doesn’t go out of the int range.*/
int main()
{
  int number, factorial = 1;
  printf("\n\nEnter a number: ");
  scanf("%d", &number);
  while(number >= 1)
  {
    factorial *= number--;
  }
  printf("\nFactorial: %d\n", factorial);
  system("pause");
  return 0;
}

Explanation of Code


#include <stdio.h> -> This is the step which occurs before compilation starts. The compiler calls the C Preprocessor to include the STDIO(Standard Input Output) header file into the program, thus letting the use of the standard input/output functions like printf() and scanf() which come from STDIO.H

int main() -> The entry point of the program where the execution starts. This function has to named main. As per the ANSI specification, the return type has to be int. If you use the traditional C, you may use void as the return type. Since the return type is specified as int in my program, I have to use a return statement at the end of my code. So I use return 0 since zero returned from a function, by convention, implies a correct execution of the program. The return values are used to debug the program.

printf() -> This is a standard output function used to print something on the screen. We have to pass a string to this function which will be displayed on user's terminal.

scanf() -> This is the scanf() function which waits for the user to enter certain value using his/her keyboard. We store the user input at the location in memory which is pointed to by the variable whose address is passed to this function.

while(number >= 1) {
factorial *= number--;
}
This is the code where we calculate the factorial of the number user entered. We decrement the variable ‘number’ at each iteration of the while loop because we want to calculate the factorial using the formula: n! = n X (n-1) X (n-2) X…..X 2 X 1
Thus the limits/condition of the while loop is self-explanatory.
factorial *= number--; multiplies the value of variable ‘factorial’ with the value of variable ‘number’, stores the result in the variable ‘factorial’ and then decrements the value of the variable ‘number’.

system("pause") -> This statement is used to pause the program, until user presses a key. This function is not necessary in your program, I use it to see my outputs paused. If you use cmd to run your programs, you might not need this. If you use linux/unix you might not need this. Depending on your compiler, this function may or may not work. Moreover, removing this line of code from this program, doesn't affect the functionality of the program.

Output(s)





No comments:

Post a Comment

Need help?