How To Write a C Program To Find Factorial of Number without using function in C Programming Language ?
Solution For C Program To Find Factorial of Number without using function:
#include<stdio.h> #include<conio.h> void main() { int i,number,factorial; printf("Enter the number : "); scanf("%d",&n); factorial = 1; for(i=1;i<=n;i++) factorial = factorial * i; printf("Factorial of %d is %d",n,factorial ); getch(); }Output :Enter the number : 5 Factorial of 5 is 120
Explanation of Program :
Before Explaining the program let us see how factorial is calculated -
Factorial of 5 = 5! = 5 * 4! = 5 * 4 * 3! = 5 * 4 * 3 * 2! = 5 * 4 * 3 * 2 * 1! = 5 * 4 * 3 * 2 * 1 * 0! = 5 * 4 * 3 * 2 * 1 * 1 = 120
Firstly accept the number whose factorial is to be found.
We have to iterate from 1 to (n-1) using for/while/do-while loop. In each iteration we are going to multiply the current iteration number and result.printf("nEnter the number : "); scanf("%d",&n);
for(i=1;i<=n;i++) factorial = factorial * i;
Some Precautions to be taken :
Precaution 1 : Initialize ‘factorial’ variable
factorial = 1;
before going inside loop we must initialize factorial variable to 1 since by default each c variable have garbage value. If we forgot to initialize variable then garbage value will be multiplied and you will get garbage value as output.
Precaution 2 : For loop must start with 1
Suppose by mistake we write following statement -
for(i=0;i<=n;i++) factorial = factorial * i;
then in the very first iteration we will get factorial = 0 and in all successive iteration we will get result as 0 since anything multiplied by Zero is Zero
Precaution 3 : Try to accept lower value to calculate factorial
We have 2 bytes to store the integer in Borland C++ compiler, so we can have maximum limit upto certain thousand. Whenever we try to accept value greater than 20 we will get factorial overflow.
Tags: C Program To Find Factorial of Number without using function, c program to find factorial of a given number using functions, fact function in c, factorial program in c using for loop, c program to find factorial of a number using user defined function, c program to find factorial of a number using recursive function, write ac program to find factorial of a number using function, c program to find factorial of a number using while loop, c program to find factorial of a number using non recursion.
Learn More :
C Program
- Using Bash to input stuff into c program
- Difficult C Programming Questions
- Write a c program to find largest among three numbers using binary minus operator three numbers using binary minus operator
- PRINTING ASCII VALUE USING C PROGRAM
- MULTIPLICATION OF TWO MATRICES USING C PROGRAM
- FIND OUT SUM OF DIAGONAL ELEMENTS OF A MATRIX USING
- Write A C Program To Find Out Transport Of A Matrix
- Factorial of 100 in C Program
- Multiplication of large numbers in c
- Division of Large Numbers in C Program
- BINARY SEARCH USING C PROGRAM
- BINARY SEARCH THROUGH RECURSION USING C PROGRAM
- FIND FACTORIAL OF A NUMBER USING RECURSION IN C PROGRAM
- FIND GCD OF A NUMBER USING RECURSION IN C PROGRAM
- FIND SUM OF DIGITS OF A NUMBER USING RECURSION USING C PROGRAM
- FIND POWER OF A NUMBER USING RECURSION USING C PROGRAM
- REVERSE A NUMBER USING RECURSION IN C PROGRAM
- SWAP TWO VARIABLES WITHOUT USING THIRD USING C PROGRAM VARIABLE
- Write A C Program For Swapping Of Two Arrays
- SWAPPING OF STRINGS USING C PROGRAM
- CONVERSION FROM DECIMAL TO OCTAL USING C PROGRAM
- CONVERSION FROM DECIMAL TO OCTAL USING C PROGRAM
- CONVERSION OF DECIMAL TO BINARY USING C PROGRAM
- CONVERSION OF FAHRENHEIT TO CENTIGRADE USING C PROGRAM
- C or C++ Program To Find Bonus Amount