Home > Backend Development > C#.Net Tutorial > Find factorial in c language

Find factorial in c language

王林
Release: 2020-05-08 09:59:11
Original
14821 people have browsed it

Find factorial in c language

Purpose:

Input a number from the keyboard and find the factorial of this number, that is, n!.

Definition of factorial:

The so-called factorial of n means starting from 1 and multiplying it by a number that is 1 larger than the previous number, until it is multiplied to n. The formula is expressed as: 1×2×3 ×4×…×(n-2)×(n-1)×n=n!.

Algorithmic idea:

You can use loops to solve the problem. Let the loop variable be i, the initial value is 1, and i changes from 1 to n; multiply i and sum in turn, and add the product Assign to sum.

Example code:

#include <stdio.h>
int main()
{
    int i,n;
    double sum=1;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
        sum=sum*i;
    printf("%d!=%lf",n,sum);
    printf("\n");
    return 0;
}
Copy after login

Run result:

Input 5, the corresponding factorial output is as follows:

5
5!=120.000000
Copy after login

Recommended tutorial: c language tutorial

The above is the detailed content of Find factorial in c language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template