🔒 Closed C program to find factorial of a number

Status
Not open for further replies.

beastmaster 64

Honorary Poster
Output

Enter an integer: 10
Factorial of 10 = 3628800

Code:
#include <stdio.h>
int main()
{
    int n, i;
    unsigned long long factorial = 1;

    printf("Enter an integer: ");
    scanf("%d",&n);

    // show error if the user enters a negative integer
    if (n < 0)
        printf("Error! Factorial of a negative number doesn't exist.");

    else
    {
        for(i=1; i<=n; ++i)
        {
            factorial *= i;              // factorial = factorial*i;
        }
        printf("Factorial of %d = %llu", n, factorial);
    }

    return 0;
}
 
Status
Not open for further replies.

About this Thread

  • 0
    Replies
  • 529
    Views
  • 1
    Participants
Last reply from:
beastmaster 64

Trending Topics

Online now

Members online
1,160
Guests online
1,639
Total visitors
2,799

Forum statistics

Threads
2,287,024
Posts
29,041,868
Members
1,216,346
Latest member
kncks_
Back
Top