Factorial Program In C Using While Loop With Example

Factorial Program In C Using While Loop With Example. If you are looking for a factorial program in C with while loop example, this C programming tutorial will help you to learn how to find the factorial of a number. Just go through this C program to calculate factorial of a number, you will be able to write a factorial C program using while loop.

Factorial Program in C – Table of Contents

 Factorial Program in C Using While Loop

 Learn how to write a  C program for factorial. Writing a factorial program in C to find factorial can be done using various techniques like using for loop, while loop, pointers, recursion function but here in this program, we show how to write a factorial program using while loop in a proper way.

Factorial Program in C Using While Loop Source Code

Copy the below program to find the factorial of a number using a while loop or write your own logic by using this program as a reference. Paste the factorial program into C compilers and run the program to see the result.
/* C PROGRAM FOR FACTORIAL USING WHILE LOOP - FACTORIAL.C */

#include<stdio.h>
#include<conio.h>

void main() //main method

{
   //variable declaration
   long int calcfact =1;
   int i=1,n;

   clrscr();

   printf("Enter a a positive number : "); //asking user to enter a num
   scanf("%d",&n); //reading entered number by user

   while(i<=n) //while loop logic for factorial

   {

     calcfact*=i; //calcfact = calcfact*i;

     i++;

   }

printf("factorial of %d is = %ld  \n", n, calcfact); 

   getch();

}

FACTORIAL PROGRAM IN C USING While Loop OUTPUT

After you compile and run the above factorial program in c to find the factorial of a number using while loop, your C compiler asks you to enter a number to find factorial. After you enter your number, the program will be executed and give output like below expected output.

Output – 1

Enter a positive number: 6
Factorial of 6 is = 720

FACTORIAL Program in C using Recursion with Explanation
  • The program execution starts from main() function.
  • Define three variable n, i, calcfact.
  • Asks the user to enter a positive number to find factorial using printf().
  • Gets the value from the user using scanf() function.
  • Let’s take a variable ‘i’ with an initial value of 1 to compare with user entered value ‘n’.
  • Now while loop will be executed until the statement while(i<=n) is false.
  • In each iteration in while loop, ‘i’ value will be increased (i++) and checked with ‘n’ (i<=n).
  • The ‘i’ value is multiplied with ‘calcfact’ variable and the new value will be stored in ‘calcfact’ variable. calcfact*=i; or calcfact = calcfact*i;
  • When while statement gets false, the compiler gets out of the while loop and prints the factorial of the entered number.
  • 1*2*3*4*5*6 = 720

C PROGRAMMING EXAMPLES

  1. C Program To Reverse a String Using Pointers
  2. C Program To Swap Two Numbers Using Two Variables
  3. C Program To Swap Two Numbers Using Three Variables
  4. C Program For Prime Numbers – Check  a Number is Prime or Not
  5. C Program To Reverse a String with Using Function
  6. C Program to Reverse a String without Using Function
  7. C Program to Reverse a Sting Using Recursion

Leave a Comment