A factor of a positive integer (number) is the sum of the multiplications of all integers less than this positive integer. For example, a factorial of 5 is 5 * 4 * 3 * 2 * 1, which is equal to 120.

The factor is denoted by “!”. So let’s say you want to find the factorial of the number n, then n! = n * (n-1) * (n-2) * (n-3)… *.

Now, let’s see how to write a C program for factorial number?

Postgraduate program: Full Stack Web Development

in collaboration with Caltech CTMESign up now

C program factor algorithm

The algorithm of the C program for finding the factorial of a number is:

  • Start the program
  • Ask the user to enter an integer to find the factorial
  • Read the integer and assign it to a variable
  • From the value of the integer to 1, multiply each digit and update the final value
  • The final value at the end of the whole multiplication to 1 is the factorial
  • End of the program

C program factor pseudocode

Using the above algorithm, we can create a pseudocode for program C to find the factorial of a number, such as:

fact of the procedure (number)

to number = 1

fact = fact * (number-1)

Print a fact

end of the procedure

Now that we know the basic algorithm and pseudocode for writing a C factorial program, let’s start applying it using different methods.

Now let’s start applying it using different methods.

C Factor finding program using For Loop

We will start by using the for loop to write a C program for the factorial of a number. The program will have an integer variable with a value of 1. The for loop will continue to increase the value by 1 with each iteration until the number is equal to the number entered by the user. The final value in the actual variable will be the factorial of the number entered by the user.

example:

#include

int main () {

int x, fact = 1, n;

printf (“Enter a number to find the factorial:”);

scanf (“% d”, & n);

for (x = 1; x <= n; x ++)

fact = fact * x;

printf (“Factorial of% d is:% d”, n, fact);

return to 0;

}

Exit:

C_Program_for_Factorial_1.

C Program for finding a factor using the While loop

In this example, we will apply the algorithm using the while loop and find the factorial of a number.

#include

int main () {

int x = 1, fact = 1, n;

printf (“Enter a number to find the factorial:”);

scanf (“% d”, & n);

while (x <= n) {

fact = fact * x;

x ++;

}

printf (“Factorial of% d is:% d”, n, fact);

return to 0;

}

Exit:

C_Program_for_Factorial_2

New course: Development of a full stack for beginners

Learn Git Command, Angular, NodeJS, Maven and moreSign up now

New course: Development of a full stack for beginners

C program for finding factor using recursion

We will now write a factorial program using a recursive function. The recursive function will be called until the value is 0.

We will now write a C program for a factorial using a recursive function. The recursive function will be called until the value is 0.

example:

#include

int main () {

int x = 7;

printf (“The factorial of the number is% d”, fact (x));

return to 0;

}

// Recursive function for finding factorial

int fact (int y) {

if (y == 0)

return 1;

return y * fact (y – 1);

}

Exit:

C_Program_for_Factorial_3

C Factor finding program using a ternary operator

The ternary operator is like a shorthand for the if … else operator. It provides two conditions and the operators that must be met on the basis of the conditions. Here is C’s factorial program using a ternary operator.

Here is the factor program using a ternary operator.

#include

int main () {

int n = 4;

printf (“% d factor is% d”),

n, fact (n));

return to 0;

}

int fact (int x) {

// Using a ternary operator

return (x == 1 || x == 0)

? 1

: x * fact (x – 1);

}

Exit:

C_Program_for_Factorial_4.

C Program for finding a factor using the tgamma () method.

The tgamma () function is a library function defined in the math.h library in C programming. It calculates the gamma function of the passed argument.

Let’s use this function to write a C factorial program.

#include

#include

int main () {

int x = 5;

x = tgamma (x + 1);

printf (“% d”, x);

return to 0;

}

Exit:

C_Program_for_Factorial_5.

Note: The tgamma () function only works for small integers, as C cannot store large values. Also, for integers above 5, you will need to add 1 to the final output to get the factorial.

C Program for finding a factor function

You can also create your own function to find the factorial of a number. The code below demonstrates the use of factorial finding functions.

example:

#include

int findFact (int);

int main () {

int x, fact, n;

printf (“Enter a number to get a factorial:”);

scanf (“% d”, & n);

fact = findFact (n);

printf (“The factorial of% d is:% d”, n, fact);

return to 0;

}

int findFact (int n) {

int x, fact = 1;

for (x = 1; x <= n; x ++)

fact = fact * x;

return of the fact;

}

Exit:

C_Program_for_Factorial_6

Full Stack course for web developers

To become an expert in MEAN StackSee the course

Full Stack course for web developers

C program for finding factor using pointers

For this example, we will create a C program to find the factorial of a number using C pointers. Below is the code for using pointers to find factorials.

#include

void findFact (int, int *);

int main () {

int x, fact, n;

printf (“Enter a number to get a factorial:”);

scanf (“% d”, & n);

findFact (n, & fact);

printf (“The factorial of% d is:% d”, n, fact);

return to 0;

}

void findFact (int n, int * fact) {

int x;

* fact = 1;

for (x = 1; x <= n; x ++)

* fact = * fact * x;

}

Exit:

C_Program_for_Factorial_7

C program for finding factor series

In addition to just finding a factorial for a single number, we can also write a C program to find the factorials of multiple numbers in a series. The code below shows how to find a factorial series of numbers in a range.

#include

int main () {

long fact = 1;

int x, n, s_range, e_range;

printf (“Enter the starting range:”);

scanf (“% d”, & s_range);

printf (“Enter the end range:”);

scanf (“% d”, & e_range);

printf (“Factorial series of the given range is:”);

for (n = s_range; n <= e_range; n ++) {

fact = 1;

for (x = 1; x <= n; x ++) {

fact = fact * x;

}

printf (“% ld”, fact);

}

return to 0;

}

Exit:

C_Program_for_Factorial_8.

Enhance your career as a MEAN stack developer with Full Stack Web Developer – the MEAN Stack master’s program. Sign up now!

Conclusion

In this article you learned how to write C programs to find the factorial of a number in different ways. The factor program of C is one of the main programs in C, which is used as an introduction to various concepts such as loops, operators and functions. You can now use the programs to find the factorial of any number.

If you want to improve your C programming skills, you can register on our SkillUp platform. The platform offers a number of free courses to help you gain a clear understanding of the basics of multiple programming languages, including C. However, if you want to excel in software development, it is crucial to learn several languages ​​instead of stick to one. Our postgraduate program in Full Stack Web Development provides application-based learning materials to help you learn about different programming concepts and tools to become a great software developer.

https://www.simplilearn.com/tutorials/c-tutorial/c-program-for-factorial

Previous article5-те най-добри охлаждащи подложки за лаптоп за 2022 г
Next articleLos Gatos imposes an “alcohol management strategy” for Promenades