- Factorial of n= n*n-1*.....1
- To find factorial of a number in c programming language we need to use for loop and iterate from n to 1
- in side loop we need to write a logic to multiply the result.
- factorial *= i;
- Finally in factorial we will have the result of 1 *2 *.....n
- Let us see an example c program on finding factorial of a number without using recursion.
Program #1: Write a c program to print factorial of a number without using recursion.
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- int n, i;
- unsigned long long factorial = 1;
- printf("Enter a number to find factorial: ");
- scanf("%d",&n);
- // show error if the user enters a negative integer
- if (n < 0)
- printf("Error! Please enter any positive integer number");
- else
- {
- for(i=1; i<=n; ++i)
- {
- factorial *= i; // factorial = factorial*i;
- }
- printf("Factorial of Number %d = %llu", n, factorial);
- }
- getch();
- }
Output:
Good instructions
ReplyDelete-Thanks