C program to display the factorial of an input number using function.
Introduction
C programming is one of the most popular and widely-used programming languages today. It is known for its simplicity, efficiency, and powerful capabilities. One of the most important concepts in C programming is the use of functions to carry out various operations. In this article, we will focus on the C program to display the factorial of an input number using function.
Factorial is a mathematical operation that involves multiplying a number by all the numbers that come before it. For example, the factorial of 5 (written as 5!) is 5 x 4 x 3 x 2 x 1 = 120. Calculating factorials is an important task in programming and has a wide range of applications. In this article, we will learn how to write C program to display the factorial of an input number using function.
The C Program to Display the Factorial of an Input Number Using Function
To calculate the factorial of an input number using a function in C programming, we need to follow a few simple steps.
Step 1: Include the necessary header files We need to include the standard input/output header file "stdio.h" and the header file for mathematical operations "math.h" in our program.
Step 2: Define the function to calculate the factorial We need to define a function that will take an input number and calculate its factorial. We can call this function "factorial".
Step 3: Write the code to calculate the factorial using the defined function We need to write the code to take the input number from the user and pass it to the factorial function. The factorial function will then calculate the factorial and return the result.
Coding:
Here is the complete code for the C program to display the factorial of an input number using a function.
#include <stdio.h>
int facto(int);
main()
{
int
no,fa;
printf("Enter
any number\n");
scanf("%d",&no);
fa=facto(no);
printf("Factorial of the num %d is = %d\n",no,fa);
}
int facto(int n)
{
int
x,f=1;
for(x=1;x<=n;x++)
{
f=f*x;
}
return f;
}
Explanation of the program:
- The above C program finds the factorial of a given number using a function called "facto".
- The program first prompts the user to enter a number, which is then read by the "scanf" function and stored in the variable "no".
- Then, the "facto" function is called with the value of "no" as its argument. The "facto" function calculates the factorial of the given number using a loop and returns the final result to the calling function.
- The main function then assigns the returned value to the variable "fa" and prints out the factorial using the "printf" function.
- The "facto" function uses a variable "x" to loop through all the numbers from 1 to the given number "n". Inside the loop, the value of "f" (which is initially set to 1) is multiplied by the value of "x" in each iteration. After the loop, the final value of "f" (which is the factorial of the given number) is returned.
Output:
FAQs
Q1. What is the maximum number that can be calculated using this program?
A1. The maximum number that can be calculated using this program depends on the size of the data type used to store the result. In this program, we have used the data type "int" to store the result. The maximum value that can be stored in an "int" variable is 2,147,483,647. Therefore, this program can calculate the factorial of any number less than or equal to 2,147,483,647.
Q2. What happens if I enter a negative number as input?
A2. If you enter a negative number as input, the program will keep recursively multiplying the number by negative integers until it reaches 0. This will result in an infinite loop, causing the program to crash. Therefore, it is recommended to only enter non-negative integers as input.
Q3. Can I modify this program to calculate the factorial of a decimal or a floating-point number?
A3. No, this program is designed to calculate the factorial of an integer only. If you want to calculate the factorial of a decimal or a floating-point number, you will need to modify the program to use a different data type and a different formula for factorial calculation.
Conclusion
Calculating factorials is an important task in programming and has a wide range of applications. In this article, we have learned how to write C program to display the factorial of an input number using function. We have also discussed some common questions and concerns related to this program.
By mastering this program, you can improve your C programming skills and impress your peers with your coding abilities. So, go ahead and try out the C program to display the factorial of an input number using function and see the results for yourself!
No comments:
Post a Comment