Utopper SkillUtopper Skill
  • Programming
    • Programming Examples
  • Interview Questions
    • DevOps Interview Questions
    • Android Interview Questions
  • How to
  • Tools
  • Top 10
  • Book Summaries
Reading: Print Factorial in C
Share
Utopper SkillUtopper Skill
Search
  • Book Summaries
  • Programming Examples
  • C Programming Example
  • Interview Question
  • How to
  • Top 10
Follow US
Utopper Skill > Programming Examples > C Programming Example > Print Factorial in C
Programming ExamplesC Programming Example

Print Factorial in C

Utopper Skill Team
By Utopper Skill Team Last updated: November 15, 2023
Share
4 Min Read
SHARE
Table of Content
Print Factorial in CWhat is factorial?Approach to print factorial of a number in C languagePseudo code to print Factorial of a numberC program to print factorial of a number

Print Factorial in C

In C programming, the factorial of a non-negative integer can be calculated using a recursive function or a loop.

What is factorial?

The factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example, the factorial of 5 (written as 5!) is equal to 5 x 4 x 3 x 2 x 1 = 120. Factorials are commonly used in combinatorics, probability, and algebra. Factorial of 0 is defined as 1.

Approach to print factorial of a number in C language

The approach to print the factorial of a number in C language can be done using the following steps:

  1. First, include the stdio.h header file in the program, which is necessary for input/output operations.
  2. Define a function called print_factorial that takes in a single integer argument, n.
  3. Initialize a variable result to 1.
  4. Use a for loop to iterate over the integers from 1 to n.
  5. Within the for loop, multiply result by the current value of the loop variable at each iteration.
  6. After the for loop, use the printf function to print the final value of result, along with a message that describes the result.
  7. In the main function, prompt the user to enter a number using the printf function, and read the user’s input using the scanf function.
  8. Pass the user’s input as an argument to the print_factorial function.

Pseudo code to print Factorial of a number

Here is some sample pseudocode for a function that calculates and prints the factorial of a given number in C:

function print_factorial(n) {
    result = 1;
    for (i = 1; i <= n; i++) {
        result = result * i;
    }
    print(result);
}

This function, called print_factorial, takes in a single argument, n, and uses a for loop to iteratively multiply the variable result by the integers from 1 to n. The final value of result is then printed.

C program to print factorial of a number

#include <stdio.h>

int factorial(int n) {
    int result = 1;
    for (int i = 1; i <= n; i++) {
        result *= i;
    }
    return result;
}

int main() {
    int num, fact;
    printf("Enter a number: ");
    scanf("%d", &num);
    fact = factorial(num);
    printf("The factorial of %d is %d\n", num, fact);
    return 0;
}

When this program is run and user inputs number 5, for example, the output will be:

Enter a number: 5
The factorial of 5 is 120

In this program, I have defined a function factorial which takes an integer as input and returns the factorial of the number, this function uses a for loop to iterate over the integers from 1 to n and multiply the result with the current value of the loop variable at each iteration. In the main function, the program prompts the user to enter a number, reads that number using the scanf function, calls the factorial function and assigns the return value to a variable. Finally, the program prints the result using the printf function.

TAGGED:C Program Examplesfactorial
Share This Article
Facebook Twitter Whatsapp Whatsapp Telegram Copy Link Print
Previous Article Prime Number in C Prime Number in C
Next Article C++ Program To Print Prime Numbers From 1 To N Armstrong number in C
Most Popular
Book Summary of No Excuses by Brian Tracy
Book Summary of No Excuses by Brian Tracy
Utopper Skill Team By Utopper Skill Team
Learn C Programming (Basic to Advanced)
Scope Rules in C
Utopper Skill Author By Utopper Skill Author
SQL Interview Questions and Answers
Top 35 SQL Interview Questions and Answers 2024
Utopper Skill Author By Utopper Skill Author
Book Summary of The Power of Now By Eckhart Tolle
Book Summary of The Power of Now By Eckhart Tolle
Utopper Skill Author By Utopper Skill Author
Kubernetes Interview Questions and Answers
Top 40 Kubernetes Interview Questions and Answers 2024 Edition
Utopper Skill Team By Utopper Skill Team

You Might Also Like

C++ Program To Print Prime Numbers From 1 To N
C Programming ExampleProgramming Examples

C++ Program To Print Prime Numbers From 1 To N

11 Min Read
C Program to Print Your Own Name
Programming ExamplesC Programming Example

C Program to Print Your Own Name with Example

2 Min Read
C Program To Convert Fahrenheit To Celsius
C Programming ExampleProgramming Examples

C Program To Convert Fahrenheit To Celsius

5 Min Read
C Program To Find LCM of Two Numbers
Programming ExamplesC Programming Example

C Program To Find LCM of Two Numbers

6 Min Read

Mail : [email protected]

Privacy Policy | DISCLAIMER | Contact Us

Learn

  • learn HTML
  • learn CSS
  • learn JavaScript

Examples

  • C Examples
  • C++ Examples
  • Java Examples

Study Material

  • Interview Questions
  • How to
  • Hosting
  • SEO
  • Blogging

 

© 2024 : UTOPPER.COM | Owned By : GROWTH EDUCATION SOLUTIONS PRIVATE LIMITED
Welcome Back!

Sign in to your account

Lost your password?