Factorial Program In C Using Pointers With Example

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

Factorial Program in C Using Pointers

Table of Contents

Factorial Program in C with Pointers

 Learn how to write a  C program for factorial using pointers. Writing a factorial program in C 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 pointers in a proper way.

Factorial Program in C using Pointers Source Code

Copy the below program to find the factorial of a number using pointers 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 POINTERS - FACTORIAL.C */

#include<stdio.h>
 
void findFactorial(int,int *); //function
int main(){
 int i,factorial,n;
 
 printf("Enter a number: ");
 scanf("%d",&n);
 
 findFactorial(n,&factorial);
 printf("Factorial of %d is: %d",n,*factorial);
 
 return 0;
}
 
void findFactorial(int n,int *factorial){
 int i;
 
 *factorial =1;
 
 for(i=1;i<=n;i++)
 *factorial=*factorial*i;
}

Factorial Program in C using Pointers Output

After you compile and run the above factorial program in c to find the factorial of a number using pointers, 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.

Enter a number: 7
Factorial of 7 is: 5040

C PROGRAMMING EXAMPLES

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

Factorial Program In C Using Recursion Function With Explanation

Factorial Program In C Using Recursion Function With Explanation. If you are looking for a factorial program in C with recursion function 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 recursion function.

Factorial Program in C – Table of Contents

What is Factorial in Mathematics?

In mathematics, Factorial is the product of a positive number (n) and all the numbers below of it (n-1). Example, the factorial of positive number n is ( n! ) is equal to 1*2*3*…*n.

Example: What is the value of 5!

5! = 1*2*3*4*5 => 120

The value of 5! is 120.

Factorial Program in C

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

Factorial Program in C using Recursion

This factorial program in c using recursion function is the 12th C programming example in the series, it helps newbies who started coding, programming students and B.Tech graduates in enhancing their C programming skills and get a job in software industry.

All the best guys in learning c programs with coding compiler website. We wish all the success in your career. Happy Learning.

C Programming Tutorials

  1. C Program For Factorial Using For Loop
  2. C Program For Palindrome String
  3. C Program For Palindrome Numbers
  4. C Program To Reverse a String with Using Function
  5. C Program To Reverse a String without Using Function
  6. C Program To Reverse a String Using Recursion

Factorial Program in C using Recursion Function

Copy the below source code to find the factorial of a number using recursive function program 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 RECURSION FUNCTION - FACTORIAL.C */

#include <stdio.h>
long int multiplyNumbers(int n);

int main()
{
 int n; //variable declaration
 
 printf("Enter a positive number: "); //asking user to enter number
 
 scanf("%d", &n); //reading number from user
 
 printf("Factorial of %d = %ld", n, multiplyNumbers(n));
 return 0;
}

//Recursive function for finding factorial

long int multiplyNumbers(int n)
{
 if (n >= 1)
 return n*multiplyNumbers(n-1);
 else
 return 1;
}

FACTORIAL program in c using recursion function OUTPUT

After you compile and run the above factorial program in c to find the factorial of a number using a recursive function, 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: 5
Factorial of 5 = 120

FACTORIAL Program in C using Recursion with Explanation

In the above output user entered number 5 to find the factorial. Program execution will start from the beginning of the main() function. The main function consists of multiplyNumbers() recursive function, this multiplyNumbers() function is called from main() function with user entered number 5 as an argument.

After passing number 5 to the multiplyNumbers() function will call multiplyNumbers() function (recursive call). In recursive call, the value of that passed argument ‘n’ is decreased by 1 until n value reaches less than 1.

Once n value is less than one, there is no recursive call and the factorial program will calculate and print output. Factorial of 5 as 120.

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

C Program For Factorial Of A Number Using For Loop

C Program For Factorial Of A Number Using For Loop.  If you are looking for a factorial program in C with an example, this factorial tutorial will help you to learn how to find the factorial of a number in C language. Just go through this C programming example for factorial, you will be able to write a C program for factorial using for loop.

C Program For Factorial – Table of Contents

What is Factorial?

Factorial is the product of a positive integer (n) and all the integers below of it (n-1). Example, the factorial of positive integer n is ( n! ) is equal to 1*2*3*…*n.

Example: What is the value of 4!

4! = 1*2*3*4 => 24

The value of 4! is 24.

C Program For Factorial

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

This is the eleventh C programming example in the series, it helps newbies coders, programming students and B.Tech graduates in enhancing their C programming skills and land on their dream job in software industry. All the best guys in learning c programs with coding compiler website. We wish all the success in your career. Happy Learning.

C Programming Tutorials

  1. C Program For Palindrome String
  2. C Program For Palindrome Numbers
  3. C Program To Reverse a String with Using Function
  4. C Program To Reverse a String without Using Function
  5. C Program To Reverse a String Using Recursion

C Program For Factorial Source Code

Copy the below C program to find the factorial of a number source code 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 - FACTORIAL.C */

#include <stdio.h>
int main()
{
 int n, i,factorial = 1; //varialbes declaration

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

// showing error message, if the entered number is a negative number
 if (n < 0)
 printf("Error! Factorial of a negative number doesn't exist.");

else
 {
   for(i=1; i<=n; ++i) //finding factorial using for loop
   {
      factorial=factorial*i; // calculating factorial of a number
   }
   printf("Factorial of %d = %d", n, factorial);
 }

return 0;
}

C Program For Factorial Output

After you compile and run the above c program for factorial of a number using for 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 number: 4
Factorial of 4 = 24

Output – 2

Enter a number: -4
Error! Factorial of a negative number doesn’t exist.

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