• 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.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main()
  5. {
  6.     int n, i;
  7.     unsigned long long factorial = 1;
  8.  
  9.     printf("Enter a number to find factorial: ");
  10.     scanf("%d",&n);
  11.  
  12.     // show error if the user enters a negative integer
  13.     if (n < 0)
  14.         printf("Error! Please enter any positive integer number");
  15.  
  16.     else
  17.     {
  18.         for(i=1; i<=n; ++i)
  19.         {
  20.             factorial *= i;              // factorial = factorial*i;
  21.         }
  22.         printf("Factorial of Number %d = %llu", n, factorial);
  23.     }
  24.  
  25.     getch();
  26. }

Output:

factorial number in c program without recursion

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu